Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I am wondering why in the pdf of the example below the letters of the word "function" and the word "sum" seem to have different spacing. This somehow looks ugly.

Apart from that, I am wondering if someone has a "nicer-looking"/"easier-to-read" way of displaying R code with the LaTeX package 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}
share|improve this question
3  
If you specify either columns=flexible, or columns=fullflexible you will better results. –  Peter Grill Nov 2 '12 at 2:24
    
Thanks, Peter. I played with it, either columns=flexible or columns=fixed with basewidth=0.5 and spacing 2 (instead of 1) for the literate entries looks good! –  Marius Hofert Nov 2 '12 at 5:58
    
Does @PeterGrill's comment answer your question? –  Werner Nov 7 '12 at 5:44
    
Yes, it solves it. –  Marius Hofert Nov 7 '12 at 18:30
add comment

1 Answer

up vote 4 down vote accepted

You should use columns=flexible, or columns=fullflexible options. Refer to Section 4.13 Column Alignment of the listings package documentation.

Here is the output with columns=flexible:

enter image description here

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.