나는 두 가지 언어(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}