1

I am writing a book that will include code in two languages (R and SAS). I know I could use \verbatim or \alltt to set the code sections, but the listings package has some nice features.

Is there a way to use listings with two languages?

  • 1
    Sure, why not. You only need to set the language in the optional argument. – Ulrike Fischer Mar 7 at 19:38
5

If you have different options for the two languages, you can define two styles, and use the appropriate option style=... of your lstlisting environments.

\documentclass{book}
\usepackage{xcolor}
\usepackage{listings}
\lstdefinestyle{SAS}{
  language=SAS,  
  basicstyle=\ttfamily,
  } 
\lstdefinestyle{R}{
  language=R,
  breaklines=true,  
  basicstyle=\ttfamily
}
\begin{document} 
Example of SAS code:
\begin{lstlisting}[style=SAS]
data myoutput;
set myinput;
if myvar = "OK" then output;
run;
\end{lstlisting}
Example of R code:
\begin{lstlisting}[style=R]
# I don't know R, code copied from http://www.rexamples.com/2/Functions
Square <- function(x) {
  return(x^2)
}

print(Square(4))
print(Square(x=4)) # same thing
\end{lstlisting}
\end{document}

enter image description here

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.