我在编译 latex 文档时收到错误“缺少 \endcsname 插入。”

我在编译 latex 文档时收到错误“缺少 \endcsname 插入。”

我正在尝试创建一个 R Markdown 模板包,以便可以以一致的格式重复准备报告。我准备了 R Markdown 文件和 Latex 文件。我可以在 TeXStudio 中编译 Latex 文件而不会出现任何错误。但是,当我尝试在 RStudio 中编译同一个文件时,会出现错误。Latex 代码如下。

\documentclass[a4paper]{report}
%Preamble
\usepackage{amsfonts}
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{babel}
\usepackage{authblk}
\bibliography{references.bib}
\usepackage{booktabs}%for long tables
\usepackage{caption}
\usepackage{enumitem}
\usepackage{fancyhdr}
%\usepackage{fontspec}
\usepackage{geometry}
\usepackage{graphicx}
\usepackage{hyperref}
\usepackage{ifxetex}
\ifxetex
\usepackage{fontspec}
\setmainfont{Times New Roman}
\setsansfont{Arial}
\usepackage[a4paper=true,xetex]{hyperref}
\else
\usepackage{tgtermes}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[scaled=.90]{helvet}
\usepackage{hyperref}
\fi
\usepackage{lipsum}%to add random sentences
\usepackage{makeidx}
\usepackage{parskip}
\usepackage{titling}

\usepackage{totpages}
\usepackage{wrapfig}
\usepackage{xcolor}
\usepackage{biblatex}
\bibliography{references.bib}
\hypersetup{
    unicode=false,          % non-Latin characters in Acrobat’s bookmarks
    pdftoolbar=true,        % show Acrobat’s toolbar?
    pdfmenubar=true,        % show Acrobat’s menu?
    pdffitwindow=true,      % window fit to page when opened
    pdfstartview={FitH},    % fits the width of the page to the window
    pdfnewwindow=true,      % links in new window
    colorlinks=true,        % false: boxed links; true: colored links
    linkcolor=black,        % color of internal links (change box color with linkbordercolor)
    citecolor=black,        % color of links to bibliography
    filecolor=red,          % color of file links
    urlcolor=black          % color of external links
}
\begin{document}
\title{A Sample Title}

\date{\today}
% Title Page


\begin{titlepage}
    \newcommand{\HRule}{\rule{\linewidth}{0.5mm}}



    %----------------------------------------------------------------------------------------
    %   TITLE SECTION
    %----------------------------------------------------------------------------------------

    \makeatletter
    \HRule \\[0.4cm]
    \center
    { \huge \bfseries Sample Title}\\[0.4cm] % Title of your document
    \HRule \\[1.5cm]

    %----------------------------------------------------------------------------------------
    \textsc{\LARGE Sales Report}\\[1.5cm] %Type of project
    \textsc{\Large Author 1}\\[0.5cm]
    \textsc{\Large Author 2}\\[0.5cm]
    \textsc{\Large Author 3}\\[0.5cm]
    % Name of your organization
    \textsc{\Large ABC Company}\\[0.5cm] % Major heading such as course name
    \textsc{\large Quarterly Sales Report}\\[0.5cm] % Minor heading such as course title
\end{titlepage}

\printbibliography
\end{document}

当我在 TeXStudio 中编译它(使用 tinytex)时,没有错误。该过程正常退出。我还将 references.bib 文件保存在工作目录中。但是当我使用 RStudio Knit 按钮编译相同的 TeX 文件时,我收到以下错误:

tlmgr search --file --global "/biblatex-dm.cfg"
tlmgr search --file --global "/nil.lbx"
tlmgr search --file --global "/skeleton.bbl"
! Missing \endcsname inserted.
<to be read again> 
                   \def 
l.46 \newpage

Error: LaTeX failed to compile skeleton.tex. See https://yihui.org/tinytex/r/#debugging for debugging tips. See skeleton.log for more info.
In addition: Warning messages:
1: In parse_packages(logfile, quiet = !verbose) :
  Failed to find a package that contains biblatex-dm.cfg
2: In parse_packages(logfile, quiet = !verbose) :
  Failed to find a package that contains nil.lbx
3: In parse_packages(logfile, quiet = !verbose) :
  Failed to find a package that contains skeleton.bbl
Execution halted

我该如何解决这个问题?

答案1

在 knitr 按钮旁边有一个设置轮 > 输出选项 > 输出格式:PDF > 高级 > 保留 .tex 文件

因此,您可以调查 .tex 文件本身,并找出 LaTeX 问题,因为错误消息告诉您它位于第 46 行

(我刚刚花了一个小时试图弄清楚为什么我不能使用 Rmarkdown 中的 python 的 matplotlib,因为我使用了在 .tex 文件中knitr::opts_chunk$set(out.width = "80%")翻译成的而不是..)0,8\linewidth0.8\linewidth

相关内容