【问题标题】:Including a TeX comment in rmarkdown/knitr document (or pandoc -> TeX output)在 rmarkdown/knitr 文档(或 pandoc -> TeX 输出)中包含 TeX 注释
【发布时间】:2017-09-30 15:51:40
【问题描述】:

我想在我的 .Rmd 文件中包含一条评论,该评论将包含在源 .tex 中。最终目标是在 .tex 源代码中创建“锚点”/“标签”,稍后我可以使用 grep 获取这些源代码,以拆分输出块以包含在其他文档中。

建议 here 使用 HTML 样式的 cmets <!-- Comment --> 看起来很有希望,但我认为 pandoc 在转换为 TeX 之前将其删除,因为包含此类 cmets 不会更改源文件。使用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}

【问题讨论】:

  • 也许把 cat ("% [[BEGIN]]")results = 'asis' 放在一块? (未经测试)
  • @Benjamin 我不相信这会奏效——这基本上就是xtable 评论块所发生的事情,如此处引用:tex.stackexchange.com/questions/201820/…
  • 现在我在电脑前,我明白我为什么这么想了。 %tabular 环境中调用评论,这是我最常遇到的地方。

标签: r latex knitr r-markdown pandoc


【解决方案1】:

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

【讨论】:

  • 完美。就像更改我的grep 以查找\comm{BEGIN}(实际上我将使用\TAG)而不是% BEGIN 一样简单。
【解决方案2】:

只是在这里胡闹。您必须对.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}

【讨论】:

  • 哦,这实际上是一种灵感。关键是要在 .tex 源文件中不可见地显示某些内容。我只是尝试了几种使用\phantom{BEGIN}的方法;问题是 1)它仍然“显示”,因为它会留下空白(所以它不能在自己的行上)和 2)我无法让它在部分标题中编译:# TEST $\phantom{TEST}$ 给出一个错误。有什么相关的想法吗?
  • 见我的回答的附录。
  • 是的,我认为它会...不理想(如果这是 knitr pdf 输出的优先级,可能会产生样式问题,并且使用具有有意义名称的标签会很好匹配而不是缩写)......现在坚持使用更“官方”的方法
  • AFK 所以不能再测试了......在那些地方包括一个未引用的标签怎么样?
猜你喜欢
  • 2019-10-14
  • 1970-01-01
  • 2013-12-29
  • 2019-11-25
  • 2016-05-13
  • 2016-10-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多