将乳胶转化为RMD

7gyucuyw  于 2023-03-20  发布在  其他
关注(0)|答案(1)|浏览(168)

当我尝试加载graphicxgeometry包时,我的Rmarkdown返回错误,错误如下:

---
output:
  bookdown::pdf_document2: 
    keep_tex: true
header-includes:
  - \usepackage[demo]{graphicx}
  - \usepackage{hyperref}
  - \usepackage{array}
  - \usepackage{lastpage}
  - \usepackage{lipsum}
  - \usepackage{fancyhdr}
  - \usepackage[hmargin=2cm,top=4cm,headheight=65pt,footskip=65pt]{geometry}
---

sometext

processing file: test.Rmd
output file: test.knit.md

! LaTeX Error: Option clash for package graphicx.

Error: LaTeX failed to compile test.tex. See https://yihui.org/tinytex/r/#debugging for debugging tips. See test.log for more info.
Execution halted

我正尝试将以下乳胶转换为rmarkdown,但没有成功:(

\documentclass[a4paper,12pt]{book}
\usepackage[utf8]{inputenc}
\usepackage[demo]{graphicx}
\usepackage{hyperref}
\usepackage{array}
\usepackage{lastpage}
\usepackage{lipsum}

\usepackage{fancyhdr}
\usepackage[hmargin=2cm,top=4cm,headheight=65pt,footskip=65pt]{geometry}

\setlength{\parindent}{0.95cm}

\pagestyle{fancy}
\renewcommand{\headrulewidth}{0pt}
\fancyhead[CE,CO,LE,LO,RE,RO]{} %% clear out all headers
\fancyhead[C]{%
          \begin{tabular}{|m{3.0cm}|m{10.0cm}|m{2.5cm}|}
          \hline
          \includegraphics[height=1.5cm,width=2.5cm]{logo.png} &
          \centering
          \Huge{TITLE} &
          \centering
          \tiny{P\'ag. \thepage\ de \pageref{LastPage}\\
          Data: 17/05/2013\\
          Rev. 0}\tabularnewline
          \hline
          \end{tabular}%
}
\fancyfoot[CE,CO,LE,LO,RE,RO]{} %% clear out all footers
\fancyfoot[C]{%
          \begin{tabular}{|m{3.0cm}|m{10.0cm}|m{2.5cm}|}
          \hline
          \includegraphics[height=1.5cm,width=2.5cm]{logo.png} &
          \centering
          \Huge{TITLE} &
          \centering
          \tiny{P\'ag. \thepage\ de \pageref{LastPage}\\
          Data: 17/05/2013\\
          Rev. 0}\tabularnewline
          \hline
          \end{tabular}%
}

\begin{document}
\chapter{Chapter title}
%% add this if you want the fancy style also on the first page of a chapter:
\thispagestyle{fancy}
\section{Section title}
\lipsum[1-10]
\end{document}

我在windows 10上,使用R 4.2.3和Texlive完整的发行版已经安装,我能够编译.tex的例子!
如果我直接在RMD中使用.tex,我将得到如下错误:

---
output:
  pdf_document: 
    keep_tex: true
header-includes:
  - \usepackage{array}
  - \usepackage{lastpage}
  - \usepackage{lipsum}
  - \usepackage{fancyhdr}
  - \geometry{hmargin=2cm,top=4cm,headheight=65pt,footskip=65pt}
  - \setlength{\parindent}{0.95cm}

  - \pagestyle{fancy}
  - \renewcommand{\headrulewidth}{0pt}
  - \fancyhead[CE,CO,LE,LO,RE,RO]{} %% clear out all headers
  - \fancyhead[C]{%
            \begin{tabular}{|m{3.0cm}|m{10.0cm}|m{2.5cm}|}
            \hline
            \includegraphics[height=1.5cm,width=2.5cm]{logo.png} &
            \centering
            \Huge{TITLE} &
            \centering
            \tiny{P\'ag. \thepage\ de \pageref{LastPage}\\
            Data: 17/05/2013\\
            Rev. 0}\tabularnewline
            \hline
            \end{tabular}%
}
  - \fancyfoot[CE,CO,LE,LO,RE,RO]{} %% clear out all footers
  - \fancyfoot[C]{%
            \begin{tabular}{|m{3.0cm}|m{10.0cm}|m{2.5cm}|}
            \hline
            \includegraphics[height=1.5cm,width=2.5cm]{logo.png} &
            \centering
            \Huge{TITLE} &
            \centering
            \tiny{P\'ag. \thepage\ de \pageref{LastPage}\\
            Data: 17/05/2013\\
            Rev. 0}\tabularnewline
            \hline
            \end{tabular}%
}
classoption: demo
---

\chapter{Chapter title}
%% add this if you want the fancy style also on the first page of a chapter:
\thispagestyle{fancy}
\section{Section title}
\lipsum[1-10]

Error in yaml::yaml.load(..., eval.expr = TRUE) : 
  Scanner error: mapping values are not allowed in this context at line 23, column 17
Calls: <Anonymous> ... parse_yaml_front_matter -> yaml_load -> <Anonymous>
Execution halted
3vpjnl9f

3vpjnl9f1#

问题是rmarkdown在插入你的头文件include之前就已经加载了一卡车的包,因此你不能再用你想要的选项加载它们了。
作为解决方法,您可以

  • 只是删除这hyperref软件包,因为它已经加载
  • \usepackage[...]{geometry}替换为\geometry{...}以修改已加载几何包的选项
  • demo选项设置为graphicx比较麻烦。你真的需要它吗?它会阻止所有的图像显示。如果你真的需要它,你可以使用classoption: demo强制所有的软件包。注意这会将该选项传递给所有加载的软件包。如果其他软件包也有该选项,结果可能会改变。
  • 如果你真的想复制你的latex文档,你可能还想改变文档类,因为bookdown::pdf_document2默认使用article类,这与你的book类有很大的不同。
---
output:
  bookdown::pdf_document2: 
    keep_tex: true
header-includes:
  - \usepackage{array}
  - \usepackage{lastpage}
  - \usepackage{lipsum}
  - \usepackage{fancyhdr}
  - \geometry{hmargin=2cm,top=4cm,headheight=65pt,footskip=65pt}
classoption: demo
---

sometext

完整文档:

---
output:
  bookdown::pdf_document2: 
    keep_tex: true
documentclass: book
classoption: demo   
header-includes:
  - \usepackage{array}
  - \usepackage{lastpage}
  - \usepackage{lipsum}
  - \usepackage{fancyhdr}
  - \usepackage{geometry}
  - \geometry{hmargin=2cm,top=4cm,headheight=65pt,footskip=65pt}
  - \setlength{\parindent}{0.95cm}
  - \pagestyle{fancy}
  - \renewcommand{\headrulewidth}{0pt}
  - \fancyhead[CE,CO,LE,LO,RE,RO]{}
  - \fancyhead[C]{\begin{tabular}{|m{3.0cm}|m{10.0cm}|m{2.5cm}|} \hline\includegraphics[height=1.5cm,width=2.5cm]{logo.png} &           \centering \Huge{TITLE} & \centering \tiny{P\'ag. \thepage\ de \pageref{LastPage}\\ Data{:} 17/05/2013\\  Rev. 0}\tabularnewline \hline \end{tabular}}
  - \fancyfoot[CE,CO,LE,LO,RE,RO]{}
  - \fancyfoot[C]{\begin{tabular}{|m{3.0cm}|m{10.0cm}|m{2.5cm}|} \hline \includegraphics[height=1.5cm,width=2.5cm]{logo.png} & \centering \Huge{TITLE} & \centering \tiny{P\'ag. \thepage\ de \pageref{LastPage}\\ Data{:} 17/05/2013\\ Rev. 0}\tabularnewline \hline \end{tabular}}
---

# Chapter title
\thispagestyle{fancy}
## Section title
\lipsum[1-10]

相关问题