如何控制RMarkdown/knitr加载的默认包以避免选项冲突。

8
我使用r markdown和knitr生成tex文档。然而,在多次运行中,我遇到了一个问题,即我在导言部分“mystyles.txt”文档中加载的包与由knitr自动加载的包发生冲突。错误因问题而异,但它总是被“option clash”所标识。
我相信我知道这个问题来自哪里(我认为) - 我只是不知道如何解决它。当我尝试在mystyles.txt中使用一个包,比如\usepackage[pdftex]{graphicx}\usepackage[table]{xcolor}, 我会得到选项冲突的错误,因为这些包已经默认不带选项被加载了。
然而,这带来了一个问题,因为我需要按照我指定的确切选项加载包。这意味着,我需要抑制或修改由r markdown/knitr自动进行的包加载。
例如,knitr自动调用\usepackage{graphicx}。如果有一种方法可以在导言部分中指定,这应该改为\usepackage[pdftex]{graphicx},那么我就可以从mystyles.txt中删除完全相同的行,并且一切都会变好。不幸的是,我没有找到任何关于这个问题的文档,而下面的参考指南也没有提供任何见解:http://rmarkdown.rstudio.com/RMarkdownReferenceGuide.pdf
有人知道如何解决这个问题吗?
-Paul

有什么我可以做的,使问题更清晰或更相关吗? - Paul
1个回答

5
我今天早上遇到了这个问题,我的解决方案是使用自定义模板。 这篇文档介绍了PDF输出的各种选项,你会看到大约三分之一的位置有一个用于设置自定义模板的选项,其中包括一个YAML头文件示例。请注意保留HTML标记。
---
title: "My document"
output:
  pdf_document:
    template: my_template.tex
---

您需要创建my_template.tex模板文件。上一页链接到Knitr使用的默认模板;只需将其复制并粘贴到my_template.tex文件中,并删除冲突的代码部分。

这是一个我发现适用于我的最小模板。

\documentclass[$if(fontsize)$$fontsize$,$endif$$if(lang)$$babel-lang$,$endif$$if(papersize)$$papersize$,$endif$$for(classoption)$$classoption$$sep$,$endfor$]{$documentclass$}

\usepackage{hyperref}
\hypersetup{unicode=true,
$if(title-meta)$
            pdftitle={$title-meta$},
$endif$
$if(author-meta)$
            pdfauthor={$author-meta$},
$endif$
$if(subtitle)$
            pdfsubject={$subtitle$},
$endif$
$if(keywords)$
            pdfkeywords={$for(keywords)$$keywords$$sep$; $endfor$},
$endif$
$if(colorlinks)$
            colorlinks=true,
            linkcolor=$if(linkcolor)$$linkcolor$$else$Maroon$endif$,
            citecolor=$if(citecolor)$$citecolor$$else$Blue$endif$,
            urlcolor=$if(urlcolor)$$urlcolor$$else$Blue$endif$,
$else$
            pdfborder={0 0 0},
$endif$
            breaklinks=true}
\urlstyle{same}  % don't use monospace font for urls
$if(listings)$
\usepackage{listings}
$endif$
$if(lhs)$
\lstnewenvironment{code}{\lstset{language=Haskell,basicstyle=\small\ttfamily}}{}
$endif$
$if(highlighting-macros)$
$highlighting-macros$
$endif$
$if(verbatim-in-note)$
\usepackage{fancyvrb}
\VerbatimFootnotes % allows verbatim text in footnotes
$endif$
$if(tables)$
\usepackage{longtable,booktabs}
$endif$
$if(graphics)$
\usepackage{graphicx,grffile}
\makeatletter
\def\maxwidth{\ifdim\Gin@nat@width>\linewidth\linewidth\else\Gin@nat@width\fi}
\def\maxheight{\ifdim\Gin@nat@height>\textheight\textheight\else\Gin@nat@height\fi}
\makeatother
% Scale images if necessary, so that they will not overflow the page
% margins by default, and it is still possible to overwrite the defaults
% using explicit options in \includegraphics[width, height, ...]{}
\setkeys{Gin}{width=\maxwidth,height=\maxheight,keepaspectratio}
$endif$
$if(links-as-notes)$
% Make links footnotes instead of hotlinks:
\renewcommand{\href}[2]{#2\footnote{\url{#1}}}
$endif$
$if(strikeout)$
\usepackage[normalem]{ulem}
% avoid problems with \sout in headers with hyperref:
\pdfstringdefDisableCommands{\renewcommand{\sout}{}}
$endif$

$if(title)$
\title{$title$$if(subtitle)$\\\vspace{0.5em}{\large $subtitle$}$endif$}
$endif$
$if(author)$
\author{$for(author)$$author$$sep$ \and $endfor$}
$endif$
\date{$date$}
$for(header-includes)$
$header-includes$
$endfor$

$if(subparagraph)$
$else$
% Redefines (sub)paragraphs to behave more like sections
\ifx\paragraph\undefined\else
\let\oldparagraph\paragraph
\renewcommand{\paragraph}[1]{\oldparagraph{#1}\mbox{}}
\fi
\ifx\subparagraph\undefined\else
\let\oldsubparagraph\subparagraph
\renewcommand{\subparagraph}[1]{\oldsubparagraph{#1}\mbox{}}
\fi
$endif$

\begin{document}
$if(title)$
\maketitle
$endif$
$if(abstract)$
\begin{abstract}
$abstract$
\end{abstract}
$endif$

$for(include-before)$
$include-before$

$endfor$
$if(toc)$
{
$if(colorlinks)$
\hypersetup{linkcolor=$if(toccolor)$$toccolor$$else$black$endif$}
$endif$
\setcounter{tocdepth}{$toc-depth$}
\tableofcontents
}
$endif$
$if(lot)$
\listoftables
$endif$
$if(lof)$
\listoffigures
$endif$
$body$

$if(natbib)$
$if(bibliography)$
$if(biblio-title)$
$if(book-class)$
\renewcommand\bibname{$biblio-title$}
$else$
\renewcommand\refname{$biblio-title$}
$endif$
$endif$
\bibliography{$for(bibliography)$$bibliography$$sep$,$endfor$}

$endif$
$endif$
$if(biblatex)$
\printbibliography$if(biblio-title)$[title=$biblio-title$]$endif$

$endif$
$for(include-after)$
$include-after$

$endfor$
\end{document}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接