我想知道为什么在下面示例的 pdf 中,“function”和“sum”的字母间距似乎不同。这看起来有点丑。
除此之外,我想知道是否有人有“更好看”/“更容易阅读”的方式来使用该包显示R
代码。LaTeX
listings
\documentclass{scrartcl}
\usepackage[T1]{fontenc}
\usepackage[american]{babel}
\usepackage{graphicx}
\usepackage{fancyvrb}
\usepackage{listings}
\lstset{% setup listings
language=R,% set programming language
basicstyle=\small,% basic font style
keywordstyle=\bfseries,% keyword style
commentstyle=\ttfamily\itshape,% comment style
numbers=left,% display line numbers on the left side
numberstyle=\scriptsize,% use small line numbers
numbersep=10pt,% space between line numbers and code
tabsize=3,% sizes of tabs
showstringspaces=false,% do not replace spaces in strings by a certain character
captionpos=b,% positioning of the caption below
breaklines=true,% automatic line breaking
escapeinside={(*}{*)},% escaping to LaTeX
fancyvrb=true,% verbatim code is typset by listings
extendedchars=false,% prohibit extended chars (chars of codes 128--255)
literate={"}{{\texttt{"}}}1{<-}{{$\leftarrow$}}1{<<-}{{$\twoheadleftarrow$}}1
{~}{{$\sim$}}1{<=}{{$\le$}}1{>=}{{$\ge$}}1{!=}{{$\neq$}}1{^}{{$^\wedge$}}1,% item to replace, text, length of chars
alsoletter={.<-},% becomes a letter
alsoother={$},% becomes other
otherkeywords={!=, ~, $, *, \&, \%/\%, \%*\%, \%\%, <-, <<-, /},% other keywords
deletekeywords={c}% remove keywords
}
\begin{document}
\noindent Just some text; see Line \ref{foo}.
\begin{lstlisting}[caption={A first example}, label=list:ex]
x <- c(1, 3, 2)
id <- function(x){
x # just a dummy (*\label{foo}*)
}
3 <= 4
3 != 4
!TRUE
y <- "foo"
(pv <- sum(x*x^x))
y ~ x + a
\end{lstlisting}
\end{document}
答案1
您应该使用columns=flexible
、 或columns=fullflexible
选项。请参阅4.13 列对齐的包裹listings
文档。
以下是输出columns=flexible
: