使用R Markdown与LaTeX制作论文模板时遇Missing \begin{document}错误求助
解决R Markdown编译LaTeX时的「Missing \begin{document}」错误
问题描述
编译基于R Markdown和LaTeX的论文模板时,出现如下错误:
! LaTeX Error: Missing \begin{document}. Error: LaTeX failed to compile paper_template.tex
用户提供的代码如下:
R Markdown 文件代码
--- title: "Paper Template" author: "AAA" abstract: "" output: pdf_document: latex_engine: xelatex includes: in_header: customized_paper_template.tex date: "2024-06-29" --- ```{r setup, include=FALSE} knitr::opts_chunk$set(echo = TRUE)
\newpage
R Markdown
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com. When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
(重复内容省略)
### customized_paper_template.tex 文件代码 ```latex \usepackage{fontspec} \usepackage{geometry} \geometry{a4paper, margin=1in} \usepackage{setspace} \renewcommand{\baselinestretch}{1.25} % Set body text font \setmainfont{Kepler Std Light Semicondensed} % Set title fonts \newfontfamily\titlefont{Tiempos Headline} % Set the font size for the document \AtBeginDocument{\fontsize{12pt}{14pt}\selectfont} % Define title formatting \usepackage{titlesec} \titleformat{\section} {\normalfont\LARGE\titlefont} % Title style {\thesection} % Section number {1em} % Space between number and title {} % No additional formatting before the title text [\titlerule[0.5pt]] % Add a line under the title with 0.5pt thickness \titleformat{\subsection} {\normalfont\Large\titlefont} % Title style {\thesubsection} % Section number {1em} % Space between number and title {} % No additional formatting before the title text [\titlerule[0.4pt]] % Add a line under the title with 0.4pt thickness \titleformat{\subsubsection} {\normalfont\normalsize\titlefont} % Title style {\thesubsubsection} % Section number {1em} % Space between number and title {} % No additional formatting before the title text [\titlerule[0.3pt]] % Add a line under the title with 0.3pt thickness \titleformat{\paragraph} {\normalfont\normalsize\titlefont} % Title style {\theparagraph} % Section number {1em} % Space between number and title {} % No additional formatting before the title text [\titlerule[0.2pt]] % Add a line under the title with 0.2pt thickness \titleformat{\subparagraph} {\normalfont\normalsize\titlefont} % Title style {\thesubparagraph} % Section number {1em} % Space between number and title {} % No additional formatting before the title text [\titlerule[0.2pt]] % Add a line under the title with 0.2pt thickness
错误原因
这个报错并非真的缺少\begin{document},而是前置代码(preamble)出现错误导致编译中断:
- 字体缺失:
Kepler Std Light Semicondensed和Tiempos Headline是商业字体,系统默认未安装,fontspec无法找到字体时会触发编译错误。 - 字体大小设置时机冲突:
\AtBeginDocument的代码会被插入到\begin{document}之后,但R Markdown的LaTeX模板已经有自己的字体大小设置,可能引发冲突。
解决方案
1. 替换为系统自带字体
将自定义字体替换为系统内置字体,比如:
- 英文环境:
Latin Modern Roman、Times New Roman - 中文环境:
SimSun(宋体)、Microsoft YaHei(微软雅黑)
2. 调整字体大小设置方式
直接在preamble中设置字体大小,无需使用\AtBeginDocument。
修改后的customized_paper_template.tex代码
\usepackage{fontspec} \usepackage{geometry} \geometry{a4paper, margin=1in} \usepackage{setspace} \renewcommand{\baselinestretch}{1.25} % 替换为系统自带字体 \setmainfont{Latin Modern Roman} % 标题字体也替换为系统可用字体 \newfontfamily\titlefont{Latin Modern Sans DemiCondensed} % 直接设置字体大小 \fontsize{12pt}{14pt}\selectfont % 标题格式定义 \usepackage{titlesec} \titleformat{\section} {\normalfont\LARGE\titlefont} {\thesection} {1em} {} [\titlerule[0.5pt]] \titleformat{\subsection} {\normalfont\Large\titlefont} {\thesubsection} {1em} {} [\titlerule[0.4pt]] \titleformat{\subsubsection} {\normalfont\normalsize\titlefont} {\thesubsubsection} {1em} {} [\titlerule[0.3pt]] \titleformat{\paragraph} {\normalfont\normalsize\titlefont} {\theparagraph} {1em} {} [\titlerule[0.2pt]] \titleformat{\subparagraph} {\normalfont\normalsize\titlefont} {\thesubparagraph} {1em} {} [\titlerule[0.2pt]]
额外检查项
- 确保已安装
titlesec宏包(TeX Live或MikTeX默认包含)。 - 若需使用自定义商业字体,需将字体文件放入系统字体目录,或通过
Path参数指定字体路径,例如:\setmainfont[Path=/path/to/fonts/]{KeplerStd-LightSemicondensed.otf}
内容的提问来源于stack exchange,提问作者mzkrc
相关产品推荐
相关产品推荐

