如何使用多種語言的清單包?

如何使用多種語言的清單包?

我正在寫一本書,其中將包含兩種語言(R 和 SAS)的程式碼。我知道我可以使用\verbatim\alltt設定程式碼部分,但該listings套件有一些不錯的功能。

有沒有辦法同時使用listings兩種語言?

答案1

如果兩種語言有不同的選項,則可以定義兩種樣式,並使用適合style=...lstlisting環境的選項。

\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}

在此輸入影像描述

相關內容