\verbatim
私は 2 つの言語 (R と SAS) のコードを含む本を書いています。または を使用し\alltt
てコード セクションを設定できることはわかっていますが、listings
パッケージには便利な機能がいくつかあります。
listings
2つの言語で使用する方法はありますか?
答え1
2 つの言語に異なるオプションがある場合は、2 つのスタイルを定義し、環境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}