I am using the listings package to display R code. I would like a monospaced font, bold keywords, and I would like code that works when the user cuts and pastes from the pdf file into rstudio. The default listings font works correctly when pasted, but does not display bold keywords. Some fonts display bold keywords, but the caret symbol is incorrect when pasted.

Here is sample code that illustrates the problem (cut and paste the fifth line in the listing to see what happens---the caret is wrong, i.e., it displays this character: ˆ, which is not recognized by R, rather than this: ^)

%\usepackage{pxfonts} % bold but wrong caret
\usepackage{courier} % bold but wrong caret
%\usepackage{ascii} % no bold



\begin{lstlisting}[float,frame=single,caption={A first example},label=list:ex1]
r <- 0.06
g <- 0.02
tt <- 1:5
cf0 <- 100
pv <- sum(cf0*(1+g)^(tt-1)/(1+r)^tt) 
print(pv, digits = 8)
[1] 437.41649

  • Could you be a bit more specific about what you mean by "the caret is wrong" (or "incorrect")? – Mico Jun 14 '12 at 18:06
  • @Mico: Thank you, I edited the question to clarify. – Robert McDonald Jun 14 '12 at 20:29

instead of courier use

  • Nice. I can attest that beramono provides the correct caret. One question: does including this package modify all/some of the mono font used in other parts of the document? (can there be a downside) Thanks. – PatrickT Nov 5 '17 at 17:17
  • 1
    Yes, the complete document has as mono font bera. But it is a very good mono font, much better than courier! – user2478 Nov 5 '17 at 17:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.