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



我使用r markdown和knifer来生成tex文档。然而,有几次我遇到了一个问题,我用序言中的"mystyles.txt"文档加载的包与Knitter自动加载的包冲突。根据问题的不同,错误看起来有所不同,但其特征始终是"选项冲突"。

我相信我知道这个问题是从哪里来的(我想)——我只是不知道如何解决它。当我试图在mystyles.txt中使用一个包,如usepackage[pdftex]{graphicx}usepackage[table]{xcolor}时,我会收到选项冲突错误,因为默认情况下,这些包已经加载,没有选项。

然而,这带来了一个问题,因为我需要使用我指定的确切选项加载包。这意味着我需要以某种方式支持或修改r markdown/knitr自动完成的包裹加载。

例如,knifer会自动调用usepackage{graphicx}。如果有某种方法可以在序言中指定,使其成为usepackage[pdftex]{graphicx},那么我可以从mystyles.txt中删除相同的行,这将是很好的。不幸的是,我找不到任何关于这个问题的文档,以下参考指南也没有提供任何见解:http://rmarkdown.rstudio.com/RMarkdownReferenceGuide.pdf.

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

-Paul

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

本文档解释了PDF输出的各种选项,您将看到有一个用于设置自定义模板的选项,大约有三分之一的方法,示例为YAML标题:

---
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=smallttfamily}}{}
$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
defmaxwidth{ifdimGin@nat@width>linewidthlinewidthelseGin@nat@widthfi}
defmaxheight{ifdimGin@nat@height>textheighttextheightelseGin@nat@heightfi}
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]{#2footnote{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
ifxparagraphundefinedelse
letoldparagraphparagraph
renewcommand{paragraph}[1]{oldparagraph{#1}mbox{}}
fi
ifxsubparagraphundefinedelse
letoldsubparagraphsubparagraph
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)$
renewcommandbibname{$biblio-title$}
$else$
renewcommandrefname{$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}

最新更新