当R生成LaTeX代码时,Knitr'asis'选项不起作用

时间:2017-10-15 21:46:38

标签: r knitr r-markdown pandoc

我正在运行一个自动生成报告的流程。它已经工作到一定程度。但是当knitr::opts_chunk$set(result='asis') LaTeX代码进入文档时,cat现在不起作用。当我\文档时,它会将\textbackslash{}替换为knit。即使您自己运行cat命令,它也会生成正确的LaTeX代码:

---
title: "Untitled"
date: "October 15, 2017"
output: pdf_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE, comment = NA, background = NA, results='asis')
```

```{r}
cat("
  \\begin{tabular*}{1\\textwidth}{c c}
  \\parbox[c]{0.4\\linewidth}{
    {\\HUGE{\\textcolor{blue}{\\textbf{\\uppercase{Headline}}} }}\\\\
    \\Large{{\\textsc{Example}}}\\\\
  }&
    \\parbox{0.25\\textwidth}{
      \\icontext{MapMarker}{12}{Global}\\\\
      \\icontext{MobilePhone}{12}{999 666 23}\\\\
      \\icontext{Send}{12}{someemail@gmail.com}\\\\
    }
  \\end{tabular*}")
```

产生错误:

enter image description here

我尝试过使用knitr::asis_output(),但会导致同样的错误

正在制作的Tex如下:

\begin{document}
\maketitle

\textbackslash{}begin\{tabular\emph{\}\{1\textwidth\}\{c c\}
\parbox[c]{0.4\linewidth}{
    {\HUGE{\textcolor{blue}{\textbf{\uppercase{Headline}}} }}\\
    \Large{{\textsc{Example}}}\\
  }\& \parbox{0.25\textwidth}{
      \icontext{MapMarker}{12}{Glogab}\\
      \icontext{MobilePhone}{12}{999 666 23}\\
      \icontext{Send}{12}{someemail@gmail.com}\\
    } \textbackslash{}end\{tabular}\}

\end{document}

1 个答案:

答案 0 :(得分:1)

终于有了解决这些问题的方法:

knitr::raw_latex("here all your tex content") 

instead of cat() + results='asis'.

https://github.com/yihui/knitr/issues/1622