简体   繁体   English

在rmarkdown / knitr文档中包含TeX注释(或pandoc - > TeX输出)

[英]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 . 我想在我的.Rmd文件中包含一个注释,该注释将包含在源.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. 最终的目标是在.tex源代码中创建“锚点”/“标记”,稍后我可以用grep来获取它以分割出输出的块以包含在其他文档中。

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. 建议在这里使用HTML风格的注释<!-- Comment -->看起来前途无量,但我认为pandoc转换之前将这个TeX ,因为源文件是由包括这样的评论不变。 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 (?). 这里提到的spin的使用看起来很有希望,但我对spin并不太熟悉,看起来我必须颠覆整个文档才能使用这种方法(?)。 Or at least start compiling with spin instead of knit (not ideal). 或者至少开始用spin编译而不是knit (不理想)。 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: 进入.tex文件:

\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. 借用https://tex.stackexchange.com/a/149847/93762的答案,我们可以定义一个新命令来设置对文档没有任何打印,但允许您在命令的大括号内放置任何内容。 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). 您必须对.tex文件进行一些后期处理,以便在% (在paste版本中)之前删除额外的\\或删除verbatim标记(在逐字版本中)。 Still thinking... 仍然在想...

Here's the rmarkdown file: 这是rmarkdown文件:

---
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: 这是tex输出文件:

\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}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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