r语言 - 在 rmarkdown/knitr 文档中包含 TeX 注释(或 pandoC语言 > TeX 输出)



我想在源.tex中包含的.Rmd文件中包括一个注释。最终目标是在.tex源中创建"锚"/"标签",稍后我可以使用grep拾取,以将输出的块分开以包含在其他文档中。

在这里使用HTML式注释<!-- Comment -->的建议看起来很有希望,但是我认为pandoc在转换为TeX之前将其删除,因为通过包括此类注释,源文件没有变化。这里提到的spin的使用看起来很有希望,但是我对spin并不熟悉,看来我必须颠覆整个文档以进行这种工作方法(?(。或至少用spin而不是knit开始编译(不是理想的(。如其他地方所述(例如,这里(,只需将文本与%一起进行消毒。

样本文档和所需的输出:

---
output:
  pdf_document:
    keep_tex: yes
---
% [[BEGIN]]
Body of document
% [[END]]

进入.tex文件:

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

end{document}

从https://tex.stackexchange.com/a/149847/93762借用答案我们可以定义一个新命令,该命令将文档设置为没有打印的新命令,但允许您将任何内容放置在命令的括号内。这似乎很顺利。

---
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.

只是在这里搞砸。您必须对.tex文件进行一些帖子处理,以在%之前(在paste版本中(中删除额外的或删除verbatim标签(在逐字版本中(。仍在思考...

这是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]]

这是tex输出文件:

begin{document}
begin{verbatim}
% Comment inside verbatim environment
end{verbatim}
verbatim{% Comment inside verbatim}
% Commment inside paste
Body of document
% {[}{[}END{]}{]}

end{document}

回答您的评论:如果您只需要搜索特定的文本("标签"(,那么这样的事情:

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

最新更新