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

标签 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 .

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

-保罗

最佳答案

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

This document 解释了 PDF 输出的各种选项,您会看到有一个用于设置自定义模板大约 1/3 的示例 YAML header :

---
title: "My document"
output:
  pdf_document:
    template: my_template.tex
---

然后您需要创建 my_template.tex 模板文件。上一页链接到Knitr使用的default template;只需将其复制并粘贴到 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}

关于r - 如何控制 RMarkdown/knitr 加载的默认包以避免选项冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28776237/

相关文章:

r - 如何检查R中数据的凸性?

用于动态报告生成的 R2HTML 或 knitr?

r - knitr,pandoc:将SVG直接嵌入HTML文档

html - 遵循 HTML knit - RMarkdown 包括空白 block

r - 等待用户输入 5 秒,否则使用默认值

r - 使用 dplyr 总结自定义函数

r - 将具有未见字符串值的新记录 append 到数据帧时出现未见因子级别,导致警告并导致 NA

从 Knitr 输出中的打印中删除注释

R Markdown 水平规则也适用于 LaTeX pdf?

替换 kable/R markdown 中的列名