6

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?

J.P. Le Cavalier
  • 1,315
  • 7
  • 16
  • 1
    You may find some inspiration from https://stackoverflow.com/q/40252885/559676. It was for R Markdown. I don't have time to write the solution for Rnw documents, but the idea is the same: redefine the `inline` hook (https://yihui.name/knitr/hooks/). – Yihui Xie Jan 18 '18 at 21:18

2 Answers2

1

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).

Vince
  • 3,325
  • 2
  • 23
  • 41
0

So looking around it does not seem to be possible to change the formatting in inline code, see here and here. It seems like if you really want them to look the same you will have to figure out the right latex options if they exist.

Walker in the City
  • 527
  • 1
  • 9
  • 22
  • Thanks for answering. Your first reference is for .Rmd documents while I am looking for .Rnw documents. I know I can set the format of the code chunks and of the inline code, I just want to know if there is a way to only define one format and use it both for the chunks and inline code. – J.P. Le Cavalier Jan 18 '18 at 19:28
  • _Can_ you set the format of the inline code? I was not able to find any way to do that, and everything I can see says that it does not take options and matches the output format (except for things like displaying decimals) – Walker in the City Jan 18 '18 at 19:38