r - 当 R 生成 LaTeX 代码时 Knit 'asis' 选项不起作用

标签 r knitr r-markdown pandoc

我正在运行一个自动生成报告的进程。它已经发挥到了一定程度。但现在,当我将 LaTeX 代码cat 放入文档中时,knitr::opts_chunk$set(result='asis') 不起作用。当我 knit 文档时,它会将 \ 替换为 \textbackslash{}。即使您单独运行 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}{<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="72011d1f17171f131b1e32151f131b1e5c111d1f" rel="noreferrer noopener nofollow">[email protected]</a>}\\\\
    }
  \\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}{<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e5968a88808088848c89a58288848c89cb868a88" rel="noreferrer noopener nofollow">[email protected]</a>}\\
    } \textbackslash{}end\{tabular}\}

\end{document}

最佳答案

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

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

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

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

关于r - 当 R 生成 LaTeX 代码时 Knit 'asis' 选项不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46760321/

相关文章:

r-markdown - 批量渲染 Rmarkdown 文件以与 Hugo 一起使用的正确方法

r - 如何在 R Markdown 输出中创建链接以获取带有文本超链接的 pdf 或 HTML

r - 多元线性回归 : Plot a straight line with confidence intervals

r - arima.sim() 函数具有不同的 : sample sizes, phi 值和 sd 值

r - 无法使用include_graphics在Rmarkdown中插入png(错误: file is not in PNG format)

R Notebook/Markdown 不使用 "fig.path = " block 选项保存 block 图

r - 如何修复警告 "label `“多次定义”

返回 R 中 grep 的原始搜索项

r - 从命令行运行 R 文件时出错

r - R markdown 版本 2 中的 YAML 前端问题 : add an institution and logo