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'm using the listings package for my sourcecodes, and among other things to control the display of those, I'm using \lstset, obviously.

Now, When setting numberstyle, I've set them to \sffamily\tiny\color{gray}. Now this all works alright, because those all works as switches inside an environment. But what If I need the numbers inside an environment?

Namely, I'd like to raise the numbers a little with \raisebox{}{}. But how do I put the line number in question into that command? Basically, they have to go into the second set of curly braces.

How do I do that?

As requested, here's the example:

\documentclass[a4paper, 11pt]{scrartcl}
\usepackage{listings}

\lstset{
    numbers=left,
    numberstyle=\sffamily\tiny,
}

\begin{document}
\begin{lstlisting}[title=test]
for i:=maxint to 0 do
begin
    { do nothing }
end;

Write("Case insensitive")
WritE("Pascal keywords.")

a = 'a' -- comment
\end{lstlisting}
\end{document}

As you can see, I've set numberstyle to the switches I want to, but how am I supposed to put an environment in there, where the number is supposed to be put into?

share|improve this question
2  
It's much easier if you can include a small MWE rather than describing it :) –  percusse Nov 16 '12 at 9:42
add comment

1 Answer 1

up vote 3 down vote accepted

listings has been designed so that when outputting line numbers, the code stored in the numberstyle key is essentially applied with the line number as an argument. The same is true for many of the other ...style keys in listings.

I'll explain some of the TeXnical details in my answer, but don't worry if you don't understand it, just try to follow the examples. The value you assign to the key numberstyle is stored in \lst@numberstyle and when listings come to write the numbers, it invokes it as \lst@numberstyle{\thelstnumber}. If TeX is not expecting to read an argument after processing the \lst@numberstyle, it sees those braces as denoting a group rather than an argument, and is harmless.

There are essentially two ways of doing something with that argument (first one suggested by alexis after my original answer mentioned only the second), depending on what macro you want to call:

  1. You could define a new macro to absorb that argument, and put it back on the input stream together with other code of choice, and then place this macro as the last thing in the value of the numberstyle key. This is perhaps the most intuitive method, and has the advantage of being most general (it can be used even when the line number is not to be passed as the last argument to an existing macro). As common with these things, it is best illustrated by example. In this example, \mynumberstyle is such a macro:

    \documentclass[a4paper, 11pt]{scrartcl}
    \usepackage{listings}
    
    \newcommand*{\mynumberstyle}[1]{\raisebox{0.3em}{#1}}
    \lstset{
        numbers=left,
        numberstyle=\sffamily\tiny\mynumberstyle,
    }
    
    \begin{document}
    \begin{lstlisting}[title=test]
    for i:=maxint to 0 do
    begin
        { do nothing }
    end;
    
    Write("Case insensitive")
    Write("Pascal keywords.")
    
    a = 'a' -- comment
    \end{lstlisting}
    \end{document}
    
  2. When all you want to do is invoke another macro and use the line number (or rather macro \thelstnumber eventually expanding to the line number) as the last argument to that macro (as is your case with \raisebox), you can avoid defining a new command and also avoid reading the argument and putting it back only to be read again (but now with the original catcodes, though this is not a problem for the use case here)...

    In particular, you can just append \raisebox{first argument} to the end of the value you assign to the numberstyle key, and it will pick up the line number as its second argument (just as \mynumberstyle picked it up as its only argument in the above example). The following example illustrates this:

    \documentclass[a4paper, 11pt]{scrartcl}
    \usepackage{listings}
    
    \lstset{
        numbers=left,
        numberstyle=\sffamily\tiny\raisebox{0.3em},
    }
    
    \begin{document}
    \begin{lstlisting}[title=test]
    for i:=maxint to 0 do
    begin
        { do nothing }
    end;
    
    Write("Case insensitive")
    Write("Pascal keywords.")
    
    a = 'a' -- comment
    \end{lstlisting}
    \end{document}
    
share|improve this answer
    
Awesome! Many thanks! –  polemon Nov 16 '12 at 17:14
    
This is nicely economical, but it only works because the number is the very last thing needed by the style expansion. What if someone needs to insert something after the number? A more general (if less elegant) solution is to define a macro that takes an argument, e.g. \newcommand\mystyle[1]{\tiny#1:}, and use it with numberstyle=\mystyle. (@cyber, perhaps you can extend your answer to discuss this case) –  alexis Nov 16 '12 at 21:27
    
@alexis: Thanks for your suggestion. Indeed it would have been best to provide a general solution also. I have now done this. –  cyberSingularity Nov 16 '12 at 23:37
2  
@polemon: I have extended my answer to illustrate how to do things more generally, as per the suggestion of alexis. The original answer though is in some sense more economical and there is nothing wrong with the solution if you wish to continue using it! I will delete this comment in a few days. –  cyberSingularity Nov 16 '12 at 23:44
    
@cyberSingularity nice, thanks! –  polemon Nov 17 '12 at 13:58
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.