1

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.

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}
Community
  • 1
  • 1
MichaelChirico
  • 33,841
  • 14
  • 113
  • 198
  • Maybe put `cat ("% [[BEGIN]]")` in a chunk with `results = 'asis'` ? (Untested) – Benjamin May 03 '17 at 02:39
  • @Benjamin I don't believe that will work -- that's essentially what's happening with `xtable` comment chunks, as cited here: https://tex.stackexchange.com/questions/201820/knitr-sanitises-the-marker-for-latex-comments-and-they-show-up-in-the-final-do – MichaelChirico May 03 '17 at 02:40
  • Now that I'm at a computer, I see why I thought that. The `%` invokes a comment in the `tabular` environment, which is where I most frequently encounter it. – Benjamin May 03 '17 at 10:39

2 Answers2

2

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.
Benjamin
  • 16,897
  • 6
  • 45
  • 65
1

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}
eipi10
  • 91,525
  • 24
  • 209
  • 285
  • Ooh, that's an inspiration actually. The key is to have something show up invisibly in the .tex source. I just tried a few ways of using `\phantom{BEGIN}`; the problem is 1) it still "shows up" in that it will leave blank space (so it can't be on its own line) and 2) I couldn't get it to compile in a section title: `# TEST $\phantom{TEST}$` gives an error. Any ideas related thereto? – MichaelChirico May 03 '17 at 03:56
  • See addendum to my answer. – eipi10 May 03 '17 at 04:32
  • yea, I think it'll do... not ideal (could create style issues if that's a priority for the knitr pdf output, and it'd be nice to use tags that have meaningful names to match instead of abbreviations)... holding out for a more "official" approach for now – MichaelChirico May 03 '17 at 04:52
  • AFK so can't test anymore... what about including an unreferenced label at those places? – MichaelChirico May 03 '17 at 04:53