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.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.




\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)
        {~}{{$\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


\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
y <- "foo"
(pv <- sum(x*x^x))
y ~ x + a

share|improve this question
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
up vote 6 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

Your Answer


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.