在外部LaTeX文件中将YAML参数作为宏访问 [英] Accessing YAML parameters as macros within external LaTeX files

查看:759
本文介绍了在外部LaTeX文件中将YAML参数作为宏访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找向YAML标头或之后添加变量(或LaTeX宏)的方法,以便可以在我的(模块化)报告的一部分外部.tex文件中使用它们.

I'm looking for ways to add variables (or LaTeX macros) to the YAML header or soon after such that they can be used in external .tex files that are a part of my (modularised) report.

我的.rmd文件

My .rmd file

---
output:
  pdf_document:
    latex_engine: xelatex
    includes:
      before_body: some.tex
params:
  cat: "Felix"
  numb: 14
---

# chapter
Oh my \textbf{`r params$cat`}. 
$x = `r 2*params$numb`^2$

<!-- Trying again to get the parameter -->
\input{some.tex}

我的some.tex文件:

My some.tex file:

`r params\$cat`

输出

希望输出

我希望能够以某种方式传递来自YAML标头(或什至略低于它)的变量以供LaTeX使用,以便可以在一个位置查看和更改所有重要且定期更新的参数.

I want to be able to somehow pass the variables from the YAML header (or even just below it) to be used by LaTeX so that all important and regularly updated parameters can be viewed and changed in one place.

推荐答案

如果您正在寻找最符合R Markdown工作流程的内容,则可以自定义用于构建LaTeX输出并添加的模板所有额外的LaTeX代码都直接用于此.

If you are looking for something which perhaps is most in keeping with the R Markdown workflow, you can customise the template which is used to build the LaTeX output and add all the extra LaTeX code directly to this.

1.复制模板

首先,我们必须复制

Firstly, we must make a copy of the template used by R Markdown. The following code will create this in your current working directory:

file.copy(system.file("rmd/latex/default-1.17.0.2.tex",
          package = "rmarkdown"), "template.tex")

2.添加变量

使用我们的副本,我们可以定义自己的 pandoc变量将被插入到输出文档中.这使我们可以在文档的YAML部分中指定参数,它们将以输出格式进行更新.完全相同的机制使我们可以添加titleauthordate并将它们添加到输出格式.

With our copy, we can define our own pandoc variables which will be inserted into the output document. This allows us to specify parameters in the YAML section of the document and they will be updated in the output format. It is exactly the same mechanism which allows us to add title, author, date and for them to be added to the output format.

我已经在第253-255行.确切的位置无关紧要,但是我也倾向于将自定义项放在\begin{document}参数之前:

I have added some code to the front matter of the document at lines 253-255. The exact location doesn't matter, but I also tend to put my customisations before the \begin{document} argument:

\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhead[LO, LE]{$params.value$}
\fancyhead[RO, RE]{$yourParam$}

3.从R Markdown调用模板

我们可以将自定义模板引用到我们的R Markdown文档在这里解释.这是我的最小示例:

We can reference the custom template to our R Markdown document as explained here. Here is my minimal example:

---
output:
  pdf_document:
    template: template.tex
params:
  value: Text
yourParam: "`r Sys.Date()`"
---

`r params$value`

这两个参数将替换$params.value$$yourParam$添加到输出中,并导致以下输出:

The two parameters will be added to the output replacing the $params.value$ and $yourParam$, and result in the output below:

该示例突出显示了如何按照原始问题的指定,不必将YAML参数嵌套在params参数中.如果要构建参数化报告

The example highlights how the YAML parameters don't have to be nested within the params argument, as specified in your original question. Having them specified within the parameters mainly has benefits if you want to build a parameterized report

注意:仅对于template选项下定义的主模板文件,才可以使用pandoc表示法$variable$替换变量.它不适用于任何includes参数或任何其他外部LaTeX文件.有关更多详细信息,请参见此处.

Note: the approach of replacing variables using the pandoc notation $variable$ is only possible for the main template file defined under the template option. It won't work for any of the includes arguments or any other external LaTeX files. See here for more details.

这篇关于在外部LaTeX文件中将YAML参数作为宏访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆