简体   繁体   中英

Including a TeX comment in rmarkdown/knitr document (or pandoc -> TeX output)

I'd like to include a comment in my .Rmd file that will be included in the source .tex . The ultimate goal is to create "anchors"/"tags" in the .tex source that I can pick up with grep later to split off chunks of the output for inclusion in other documents.

The suggestion here to use HTML-style comments <!-- Comment --> looked promising, but I think pandoc removes this before converting to TeX since the source file is unchanged by including such comments. The use of spin mentioned here looks promising, but I'm not too familiar with spin , and it looks like I'd have to upend my whole document for this approach to work (?). Or at least start compiling with spin instead of knit (not ideal). As noted elsewhere (eg, here ), simply including text with % will be sanitized.

Sample document and desired output:

---
output:
  pdf_document:
    keep_tex: yes
---

% [[BEGIN]]

Body of document

% [[END]]

Into .tex file:

\documentclass[]{article}
%Remainder of knitr/pandoc-produced preamble
\begin{document}

% [[BEGIN]]

Body of document

% [[END]]


\end{document}

Borrowing an answer from https://tex.stackexchange.com/a/149847/93762 , We can define a new command that sets prints nothing to the documents, but allows you to place anything within the braces of the command. This seems to work pretty smoothly.

---
output:
  pdf_document:
    keep_tex: yes
header-includes:
  - \usepackage{verbatim}
  - \newcommand{\comm}[1]{}
---

La la la

\comm{START OF BLOCK 1}

Here is the text that goes inside of the first block

\comm{END OF BLOCK 1}

Here is some text that is not between "comment" blocks.

Just messing around here. You'd have to do some post processing of the .tex file to get rid of the extra \\ before the % (in the paste version) or remove the verbatim tags (in the verbatim versions). Still thinking...

Here's the rmarkdown file:

---
output:
  pdf_document:
    keep_tex: yes
header-includes:
  - \usepackage{verbatim}
---

\begin{verbatim}
% Comment inside verbatim environment
\end{verbatim}

\verbatim{% Comment inside verbatim}

`r paste("% Commment inside paste")`

Body of document  

% [[END]]

Here's the tex output file:

\begin{document}

\begin{verbatim}
% Comment inside verbatim environment
\end{verbatim}

\verbatim{% Comment inside verbatim}

\% Commment inside paste

Body of document

\% {[}{[}END{]}{]}


\end{document}

In response to your comment: If you just need to be able to search for specific text (the "tags"), what about something like this:

---
output:
  pdf_document:
    keep_tex: yes
---

\phantom{BB} This is the first line of the body. Then there's a whole bunch of 
stuff, like the text in an SO question: I'd like to include a comment in my .Rmd
 file that will be included in the source .tex. The ultimate goal is to create 
"anchors"/"tags" in the .tex source that I can pick up with grep later to split 
off chunks of the output for inclusion in other documents.

The suggestion here to use HTML-style comments <!-- Comment --> looked 
promising, but I think pandoc removes this before converting to TeX since the 
source file is unchanged by including such comments. The use of spin mentioned 
here looks promising, but I'm not too familiar with spin, and it looks like I'd 
have to upend my whole document for this approach to work (?). Or at least start 
compiling with spin instead of knit (not ideal). As noted elsewhere (e.g., 
here), simply including text with % will be sanitized. Then, finally, we get to 
the very last line of the body.\phantom{EE}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM