繁体   English   中英

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

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

我想在我的.Rmd文件中包含一个注释,该注释将包含在源.tex 最终的目标是在.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}

暂无
暂无

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

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