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

Pau*_*aul 6 r knitr r-markdown

我使用r markdown和knitr来制作tex文档.但是,有几次我遇到了一个问题,我在前言中加载了"mystyles.txt"文件的软件包与knitr自动加载的软件包冲突.根据问题,错误看起来不同,但它始终以"选项冲突"为特征.

我相信我明白这个问题的来源(我认为) - 我只是不知道如何解决它.当我尝试使用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.

有谁知道如何处理这个问题?

- 保罗

jke*_*ead 5

我今天早上刚刚遇到这个问题,我的解决方案是使用自定义模板。

本文档解释了 PDF 输出的各种选项,您将看到有一个用于设置自定义模板的大约 1/3 的方式,并带有示例 YAML 标头:

---
title: "My document"
output:
  pdf_document:
    template: my_template.tex
---
Run Code Online (Sandbox Code Playgroud)

然后您需要创建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}
Run Code Online (Sandbox Code Playgroud)