我想在我的.Rmd
文件中添加一条评论,该评论将包含在来源.tex
中。最终的目标是在.tex源代码中创建“锚点”/“标记”,我可以稍后使用grep
来分析输出的块以包含在其他文档中。
使用HTML样式评论<!-- Comment -->
的建议here看起来很有希望,但我认为pandoc
在转换为TeX
之前删除了这个,因为源文件未更改这样的评论。使用spin
提到的here看起来很有希望,但我对spin
并不太熟悉,看起来我不得不颠覆整个文档来实现这种方法( ?)。或者至少开始使用spin
而不是knit
进行编译(不理想)。如其他地方所述(例如,here),只需将包含%
的文字包含在内即可进行清理。
示例文档和所需的输出:
---
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}
答案 0 :(得分:2)
借用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.
答案 1 :(得分:1)
在这里乱搞。您必须对\
文件进行一些后期处理,以便在%
(paste
版本)之前删除额外的verbatim
或删除{{ 1}}标签(在逐字版本中)。还在想......
以下是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}