Search code examples
rfontslatexsweave

Sweave-LaTeX file: use a new font in text but keep original font for R code


Using Sweave I wrote a file a little test file, which shown below. As I want to use different fonts, I load the package fontspec. In the .pdf output also the R code chunks show a new font, however.

\documentclass[a4paper, 12pt]{scrartcl}
\usepackage[a4paper, left=2.5cm, right=2.5cm, top=2.5cm, bottom=2cm]{geometry}
\usepackage{fontspec} % fonts

\begin{document}
\SweaveOpts{concordance=TRUE}

% new font
\fontspec{Cambria}    
Some text. 

% R chunk
<< >>=
x <- 1:10
plot(x)
@

\end{document}

My question: Is it possible, (maybe with \DefineVerbatimEnvironment{}?), to return to the default font for the R code chunks, keeping the font as in \fontspec{ } for the text?


Solution

  • This was harder than I had expected. I found the answer here. The magic line seems to be \usepackage[noae]{Sweave}.

    I've made a few changes because \usepackage{fontspec} seems to expect something other than pdflatex.

    \documentclass[a4paper, 12pt]{scrartcl}
    \usepackage[a4paper, left=2.5cm, right=2.5cm, top=2.5cm, bottom=2cm]{geometry}
    \usepackage[noae]{Sweave}
    \usepackage[light,math]{anttor} % Obvious and ugly roman font. 
    \usepackage[T1]{fontenc}
    
    \begin{document}
    \SweaveOpts{concordance=TRUE}
    
    
    % new font
    Some text. 
    
    
    % R chunk
    << >>=
    x <- 1:10
    plot(x)
    @
    
    \end{document}