[英]Rmarkdown & PDF Output: Evaluate Markdown inside Latex section
我怎樣才能使 Latex 代碼中使用的 markdown 代碼得到評估? (Rmarkdown 與 PDF(乳膠)輸出)
非常簡單的最小示例:
\begin{center}
**should be bold text**
\end{center}
用knitr編譯后的當前output in.tex文件:
\begin{center}
**should be bold text**
\end{center}
預期的:
\begin{center}
\textbf{should be bold text}
\end{center}
我很樂意找到一種方法來實現這一點,因為我試圖找到一種方法來通過 kable/kableExtra 傳遞 tibble/dataframe。 表格單元格已經可以包含 Latex 代碼,但沒有 markdown 代碼,因為 kable 將所有內容轉換為 Latex 結構。
在我看來,任何 Latex 代碼塊中的所有 Markdown 代碼都沒有得到評估。
我知道我可以通過使用 Latex 代碼來獲得相同的結果,但我更喜歡盡可能使用 Markdown 快捷方式。
編輯:
@duckmayr 好心地提出審查另一個最小示例,以了解如何自動更改由 R 函數生成的 Latex 代碼以使其工作(連同建議和接受的答案,謝謝)。 所以我正在尋找一種無論我使用什么 R function 都可以工作的包裝器(這里:一個基本的 R 示例,也可以是一個簡單的 kable 測試)
---
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 repo 上提出了一個關於這個問題的問題, 我們可以找到一個解決方法:Making LaTeX synonyms for \begin{}
和\end{}
。 因此,要在 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 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:
kable()
這樣的東西怎么樣? 為了處理在 R 塊中使用kable()
之類的東西,並帶有results='asis'
,我們需要修復kable()
的 output ; 即,我們需要將其\begin{}
和\end{}
標簽更改為\Begin{}
和\End{}
,並且我們還需要確保我們最終不會將\\
序列轉換為textbackslash{}
秒。 以下是我們的做法:
---
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}{\\}
在 header 中添加了一個新的 LaTeX 命令,以便我們可以安全地添加\\
而不會將它們轉換為\textbackslash{}
。 這是必要的,因為我們如何欺騙 pandoc 在\Begin
和\End
之間的環境中處理 markdown 。
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.
然后我們得到以下LaTeX和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}
If you only need a simple LaTeX environment, I'd recommend that you use fenced Div
blocks in Pandoc's Markdown ( see this section in the R Markdown Cookbook for more info), eg,
::: {.center}
**should be bold text**
```{r}
knitr::kable(head(iris))
```
:::
在圍欄的 Div 塊中,您可以編寫任意 Markdown 內容。 此外,這也適用於 HTML output。
請注意,此功能需要相對較新的 Pandoc 版本和rmarkdown的開發版本。 您可以嘗試RStudio 預覽版(如果您使用 RStudio)以及remotes::install_github('rstudio/rmarkdown')
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.