Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop knitr from adding tex packages based on documentClass?

For example, I have a simple minimal .Rnw file, which is the following :

<<setup, include=FALSE>>=
test = "test"
@
\documentclass{article}
\begin{document}

Test value is \Sexpr{test}

\end{document}

All I want it knit to replace the Sexpr variables, but it is adding a whole bunch of preamble based on the documentclass.

I'm working on a limited docker environment so I don't have those packages in tex.

How can I make knitr not add all that pre code?

This is output from knitr:

\documentclass{article}\usepackage[]{graphicx}\usepackage[]{color}
% maxwidth is the original width if it is less than linewidth
% otherwise use linewidth (to make sure the graphics do not exceed the margin)
\makeatletter
\def\maxwidth{ %
  \ifdim\Gin@nat@width>\linewidth
    \linewidth
  \else
    \Gin@nat@width
  \fi
}
\makeatother

\definecolor{fgcolor}{rgb}{0.345, 0.345, 0.345}
\newcommand{\hlnum}[1]{\textcolor[rgb]{0.686,0.059,0.569}{#1}}%
\newcommand{\hlstr}[1]{\textcolor[rgb]{0.192,0.494,0.8}{#1}}%
\newcommand{\hlcom}[1]{\textcolor[rgb]{0.678,0.584,0.686}{\textit{#1}}}%
\newcommand{\hlopt}[1]{\textcolor[rgb]{0,0,0}{#1}}%
\newcommand{\hlstd}[1]{\textcolor[rgb]{0.345,0.345,0.345}{#1}}%
\newcommand{\hlkwa}[1]{\textcolor[rgb]{0.161,0.373,0.58}{\textbf{#1}}}%
\newcommand{\hlkwb}[1]{\textcolor[rgb]{0.69,0.353,0.396}{#1}}%
\newcommand{\hlkwc}[1]{\textcolor[rgb]{0.333,0.667,0.333}{#1}}%
\newcommand{\hlkwd}[1]{\textcolor[rgb]{0.737,0.353,0.396}{\textbf{#1}}}%
\let\hlipl\hlkwb

\usepackage{framed}
\makeatletter
\newenvironment{kframe}{%
 \def\at@end@of@kframe{}%
 \ifinner\ifhmode%
  \def\at@end@of@kframe{\end{minipage}}%
  \begin{minipage}{\columnwidth}%
 \fi\fi%
 \def\FrameCommand##1{\hskip\@totalleftmargin \hskip-\fboxsep
 \colorbox{shadecolor}{##1}\hskip-\fboxsep
     % There is no \\@totalrightmargin, so:
     \hskip-\linewidth \hskip-\@totalleftmargin \hskip\columnwidth}%
 \MakeFramed {\advance\hsize-\width
   \@totalleftmargin\z@ \linewidth\hsize
   \@setminipage}}%
 {\par\unskip\endMakeFramed%
 \at@end@of@kframe}
\makeatother

\definecolor{shadecolor}{rgb}{.97, .97, .97}
\definecolor{messagecolor}{rgb}{0, 0, 0}
\definecolor{warningcolor}{rgb}{1, 0, 1}
\definecolor{errorcolor}{rgb}{1, 0, 0}
\newenvironment{knitrout}{}{} % an empty environment to be redefined in TeX

\usepackage{alltt}
\IfFileExists{upquote.sty}{\usepackage{upquote}}{}
\documentclass{article}
\begin{document}

Test value is test

\end{document}
like image 944
Mojimi Avatar asked Oct 22 '25 10:10

Mojimi


1 Answers

You can't really stop this without modifying the knitr code. Some extra packages are hard coded into the knitr:::make_header_latex function, which you can see in https://github.com/yihui/knitr/blob/dccdad769b67a23d9cf8b8414f844e2a6a74f21e/R/header.R#L16 .

However, there are ways to modify that function on the fly, using the fairly dangerous assignInNamespace function:

assignInNamespace("make_header_latex", function(...) "", "knitr")

After this,

knit("minimal.Rnw")

produces this output:

\documentclass{article}
\IfFileExists{upquote.sty}{\usepackage{upquote}}{}
\begin{document}

Test value is test

\end{document}

(The upquote line comes from a different function, but it should be safe because of \IfFileExists.)

Be careful if you do this, because the change I made will last for the rest of your R session.

Edited to add: If you want, you can put that assignInNamespace line into a code chunk in the document. If you're using RStudio and knit the document by clicking on "Compile PDF", it will start a new R session, do the knitting (which will replace the function), and then run the patched-in function: so you get the effect you want without messing up knitr in your current session.

like image 104
user2554330 Avatar answered Oct 23 '25 23:10

user2554330



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!