Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inline code format with knitr and .Rnw

Tags:

r

knitr

sweave

I use knitr to weave my .Rnw documents into .tex and then .pdf document (still using pdflatex).

I'm looking for a solution to have the exact same format for inline code and for code chunks. I can use the texttt{} function to get the same font, but I am looking for a way to get the exact same format (font, background color, syntax highlighting).

Somebody has an idea?

like image 528
J.P. Le Cavalier Avatar asked Nov 08 '22 11:11

J.P. Le Cavalier


1 Answers

I found the following approximate solution:

\documentclass{article}
\usepackage{helvet}
\renewcommand{\familydefault}{\sfdefault}
\usepackage{sansmath}
\begin{document}
<<>>=
options(digits = 3, scipen = 6)
some_small_number <- 0.000000000123456
@

  Default math font: \Sexpr{some_small_number}
  Sans-serif font: {\sansmath \Sexpr{some_small_number}}
\end{document}

Output:

enter image description here

I would prefer it matches to Helvetica, but at least is it somewhat similar.

UPDATE: Loading sansmath after setting default font, \sfdefault, does make the math font match the current default (code and image above is corrected for this).

like image 189
Vince Avatar answered Nov 14 '22 21:11

Vince