Rmarkdown& PDF输出:在“乳胶"部分中评估Markdown [英] Rmarkdown & PDF Output: Evaluate Markdown inside Latex section

查看:136
本文介绍了Rmarkdown& PDF输出:在“乳胶"部分中评估Markdown的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使Latex代码中使用的降价代码得到评估? (带有PDF(Latex)输出的Rmarkdown)

非常简单的最小示例:

\begin{center}
**should be bold text**
\end{center}

使用knitr编译后的.tex文件中的当前输出:

\begin{center}
**should be bold text**
\end{center}

预期:

\begin{center}
\textbf{should be bold text}
\end{center}

我很乐于找到一种方法来使此工作正常进行,因为我试图找到一种方法,可以通过kable/kableExtra传递小节/数据帧.表格单元格已经可以包含Latex代码,但不能包含降价代码,因为kable会将所有内容都转换为Latex结构.

在我看来,任何Latex代码块中的所有Markdown代码都不会得到评估.

我知道仅使用Latex代码也可以达到相同的结果,但是我更喜欢尽可能使用Markdown快捷方式.

@ puck @ duckmayr愿意提供另一个最小的示例,以查看如何自动更改R函数产生的Latex代码以使其起作用(连同建议的并被接受的答案thx).所以我正在寻找一种无论我使用什么R函数都可以工作的包装器(这里:一个基本的R示例和一个简单的kable测试;也可以是Stargazer或其他东西)

---
title: "Untitled"
output: 
  pdf_document:
    keep_tex: true
    df_print: kable
header-includes:
    - \let\Begin\begin
    - \let\End\end
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE, results='hide', message=FALSE, warning=FALSE)
library(kableExtra)
```


```{r test1, results='asis'}
test = function(x=1){
  cat('\\begin{center}\n**test**\n\\end{center}')
}
test()
```

```{r test2, results='asis'}
kable(data.frame(x=c("**bold text**")),"latex")
```

解决方案

有人打开一个问题几年前的Pandoc GitHub存储库,并且我们可以找到一种解决方法:为\begin{}\end{}制作LaTeX同义词.因此,要在R Markdown中使用它,我们只需将它们放在header-includes中:

---
title: "Stack Overflow Answer"
author: "duckmayr"
date: "5/9/2020"
output:
    pdf_document:
        keep_tex: true
header-includes:
    - \let\Begin\begin
    - \let\End\end
---

\Begin{center}

**should be bold text**

\End{center}

LaTeX输出:

... Many initial lines skipped ...
\let\Begin\begin
\let\End\end

\title{Stack Overflow Answer}
\author{duckmayr}
\date{5/9/2020}

\begin{document}
\maketitle

\begin{center}

\textbf{should be bold text}

\end{center}

\end{document}

PDF输出:

更新:使用kable()之类的东西怎么办?

要处理在带有results='asis'的R块中使用kable()之类的东西,我们需要修复kable()的输出;也就是说,我们需要将其\begin{}\end{}标记更改为\Begin{}\End{},并且还需要确保不要最终将\\序列转换为textbackslash{} s.这是我们的处理方式:

---
title: "Untitled"
output: 
  pdf_document:
    keep_tex: true
    df_print: kable
header-includes:
    - \let\Begin\begin
    - \let\End\end
    - \newcommand{\Newrow}{\\}
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE, results='hide', message=FALSE, warning=FALSE)
library(kableExtra)
allow_markdown <- function(tex) {
    tex <- gsub("begin", "Begin", tex) ## fix \begin{} tags
    tex <- gsub("\\\\end", "\n\\\\End", tex) ## fix \end{} tags
    tex <- gsub("\\\\\\\\", "\\\\Newrow\n", tex) ## fix new row \\
    return(tex)
}
```

```{r test2, results='asis'}
kable(data.frame(x=c("**bold text**")),"latex")
```

```{r test3, results='asis'}
allow_markdown(kable(data.frame(x=c("**bold text**")), "latex"))
```

我们在\newcommand{\Newrow}{\\}的标头中添加了新的LaTeX命令,以便我们可以安全地添加\\,而无需将其转换为\textbackslash{}.这是必要的,因为我们如何诱使pandoc在\Begin\End之间的环境中处理markdown.

我们还添加了一个R函数,用于修复kable()的LaTeX输出,该函数修复了开始和结束标签以及新的行\\字符.

然后,我们将获得以下LaTeX和PDF输出:

[header omitted]
\begin{document}
\maketitle

\begin{tabular}{l}
\hline
x\\
\hline
**bold text**\\
\hline
\end{tabular}

\begin{tabular}{l}
\hline

x\\

\hline

\textbf{bold text}\\

\hline

\end{tabular}

\end{document}

How can I make it possible that markdown code used inside Latex Code gets evaluated? (Rmarkdown with PDF (Latex) Output)

Very simple minimal example:

\begin{center}
**should be bold text**
\end{center}

Current output in .tex file after compiling with knitr:

\begin{center}
**should be bold text**
\end{center}

Expected:

\begin{center}
\textbf{should be bold text}
\end{center}

I would be happy to find a way to get this working, because I try to find a way in which I can pass a tibble/dataframe through kable/kableExtra. Table cells can already contain Latex code, but no markdown code, since kable converts everything into a Latex structure.

It seems to me like all Markdown code inside any Latex code block doesn't get evaluated.

I know that I can achieve the same result by just using Latex Code, but I prefer using the Markdown shortcuts wherever possible.

Edit:

@duckmayr kindly offered to review another minimal example to see how it's possible to automatically change Latex code produced by R functions in order to make it work (together with the proposed & accepted answer, thx). So I'm looking for some kind of wrapper that would work regardless what R function I use (here: one basic R example and a simple kable test; could also be Stargazer, or something)

---
title: "Untitled"
output: 
  pdf_document:
    keep_tex: true
    df_print: kable
header-includes:
    - \let\Begin\begin
    - \let\End\end
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE, results='hide', message=FALSE, warning=FALSE)
library(kableExtra)
```


```{r test1, results='asis'}
test = function(x=1){
  cat('\\begin{center}\n**test**\n\\end{center}')
}
test()
```

```{r test2, results='asis'}
kable(data.frame(x=c("**bold text**")),"latex")
```

解决方案

Someone opened an issue on the pandoc GitHub repo about this a couple years ago, and we can find there a workaround: Making LaTeX synonyms for \begin{} and \end{}. So, to use this in R Markdown, we just put them in header-includes:

---
title: "Stack Overflow Answer"
author: "duckmayr"
date: "5/9/2020"
output:
    pdf_document:
        keep_tex: true
header-includes:
    - \let\Begin\begin
    - \let\End\end
---

\Begin{center}

**should be bold text**

\End{center}

LaTeX output:

... Many initial lines skipped ...
\let\Begin\begin
\let\End\end

\title{Stack Overflow Answer}
\author{duckmayr}
\date{5/9/2020}

\begin{document}
\maketitle

\begin{center}

\textbf{should be bold text}

\end{center}

\end{document}

PDF output:

Update: What about using things like kable()?

To deal with using things like kable() in R chunks with results='asis', we'll need to fix the output of kable(); namely, we'll need to change its \begin{} and \end{} tags to \Begin{} and \End{}, and we'll also need to make sure we don't end up converting \\ sequences to textbackslash{}s. Here's how we'd do that:

---
title: "Untitled"
output: 
  pdf_document:
    keep_tex: true
    df_print: kable
header-includes:
    - \let\Begin\begin
    - \let\End\end
    - \newcommand{\Newrow}{\\}
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE, results='hide', message=FALSE, warning=FALSE)
library(kableExtra)
allow_markdown <- function(tex) {
    tex <- gsub("begin", "Begin", tex) ## fix \begin{} tags
    tex <- gsub("\\\\end", "\n\\\\End", tex) ## fix \end{} tags
    tex <- gsub("\\\\\\\\", "\\\\Newrow\n", tex) ## fix new row \\
    return(tex)
}
```

```{r test2, results='asis'}
kable(data.frame(x=c("**bold text**")),"latex")
```

```{r test3, results='asis'}
allow_markdown(kable(data.frame(x=c("**bold text**")), "latex"))
```

We added a new LaTeX command in the header with \newcommand{\Newrow}{\\} so that we can safely add \\ without them being converted to \textbackslash{}. This is necessary because of how we're tricking pandoc into processing the markdown in the environment between \Begin and \End.

We also added an R function for fixing up the LaTeX output of kable() that fixes the begin and end tags and the new row \\ characters.

Then we get the following LaTeX and PDF output:

[header omitted]
\begin{document}
\maketitle

\begin{tabular}{l}
\hline
x\\
\hline
**bold text**\\
\hline
\end{tabular}

\begin{tabular}{l}
\hline

x\\

\hline

\textbf{bold text}\\

\hline

\end{tabular}

\end{document}

这篇关于Rmarkdown&amp; PDF输出:在“乳胶"部分中评估Markdown的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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