You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将R-Markdown生成的.tex文件整合到现有LaTeX文档中?

问题

我有一份现有LaTeX文档,已经用R完成数据分析并创建了对应的.Rmd文件,通过以下代码生成了.Rmd对应的.tex文件:

knitr::knit("test.Rmd")
markdown::pandoc_convert("test.md", to = "latex", output = "R_code.tex")

尝试将生成的R_code.tex文件导入现有LaTeX文档,代码如下:

\documentclass{article} 

    \usepackage{
        float,              
        graphicx,   
        mathtools,  
        microtype,  
        paralist,   
        xcolor,     
        framed
    }

\begin{document}
\definecolor{shadecolor}{gray}{0.9}

Hello World.
\input{R_code}

\end{document}

但编译LaTeX脚本时,R_code.tex文件引发大量错误,包括shaded、highlighting等未定义环境,以及\NormalTok、\OtherTok等未定义控制序列。请问如何让R/Markdown生成的.tex文件顺利整合到现有LaTeX文档中?是缺少额外LaTeX包,还是需要修改R脚本?亦或是有更简便的方法?

附.Rmd文件代码:

---
title: "test"
output: html_document
date: "2024-09-08"
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)

R Markdown

Lorem Ipsum...

summary(cars)

Including Plots

You can also embed plots, for example:

plot(pressure)

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.

# 解决方案

## 最简便的方法:让knitr直接生成可导入的LaTeX片段
别先转成MD再用pandoc转TeX,直接让knitr输出兼容的LaTeX代码片段就行。把你的R脚本改成:
```r
knitr::knit("test.Rmd", output = "R_code.tex", tangle = FALSE)

或者直接修改你的.Rmd文件头部,指定输出为LaTeX片段(不带完整文档结构):

---
title: "test"
output: 
  latex_fragment: default
date: "2024-09-08"
---

之后用knitr::knit("test.Rmd")生成的.tex文件,直接用\input{}导入现有LaTeX文档就没问题,不需要额外处理格式。

补全LaTeX包解决兼容问题(适合坚持原流程的情况)

如果一定要用原来的方式生成TeX文件,那得在现有LaTeX文档的导言区补上缺失的包,才能支持代码高亮和shaded环境:

\documentclass{article} 

\usepackage{
    float,              
    graphicx,   
    mathtools,  
    microtype,  
    paralist,   
    xcolor,     
    framed,
    fancyvrb,    % 支持代码高亮环境
    upquote,     % 修正引号显示
    listings     % 可选,替代fancyvrb的代码高亮工具
}
% 定义缺失的代码高亮控制序列
\providecommand{\NormalTok}[1]{#1}
\providecommand{\OtherTok}[1]{#1}
% 确保shaded环境可用
\newenvironment{shaded}{\begin{snugshade}}{\end{snugshade}}

\begin{document}
\definecolor{shadecolor}{gray}{0.9}

Hello World.
\input{R_code}

\end{document}

不过这种方法容易遇到更多格式适配问题,不如第一种方法省心。

最高整合度:在LaTeX里直接嵌R代码

直接把R代码块写到现有LaTeX文档里,用knitr编译整个文档,省得单独生成R_code.tex。修改你的LaTeX文档:

\documentclass{article} 

\usepackage{
    float,              
    graphicx,   
    mathtools,  
    microtype,  
    paralist,   
    xcolor,     
    framed
}

\begin{document}
\definecolor{shadecolor}{gray}{0.9}

Hello World.

% 嵌入R设置块
<<setup, include=FALSE>>=
knitr::opts_chunk$set(echo = TRUE)
@

\section{R Markdown}
Lorem Ipsum...

% 嵌入数据分析代码块
<<cars>>=
summary(cars)
@

\section{Including Plots}
You can also embed plots, for example:

% 嵌入绘图代码块(不显示代码)
<<pressure, echo=FALSE>>=
plot(pressure)
@

Note that the \texttt{echo = FALSE} parameter was added to the code chunk to prevent printing of the R code that generated the plot.

\end{document}

之后用R运行knitr::knit("你的主LaTeX文件名.tex"),生成的.tex文件直接用LaTeX编译器编译就行,全程不用单独处理Rmd文件。

内容的提问来源于stack exchange,提问作者Maximilian

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.18 16:03:23