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 trying to type some report and I have a small problem with the twocolums document class and the line numbering in listings.

The document is on two columns :

\documentclass[8pt,[...],a4paper,twocolumn]{article}

The listings can end up either on the left or the right column. The problem is: if I put

\lstset{numbers=left,frame=tb,[...]}

the space between the columns is not sufficient when the listing is on the right column and the text from the first column is written over.

Is there any way to ask the listings to put the line numbers "outside" ?

share|improve this question

3 Answers 3

up vote 2 down vote accepted
\documentclass[a4paper,twocolumn]{article}
\usepackage{listings}
\lstset{numbers=left,frame=tb,numbersep=1em,xleftmargin=2em,
  basicstyle=\ttfamily\small}
\parindent=0pt

\begin{document}

\rule{\linewidth}{1pt}
\begin{lstlisting}
 \def\showDiff#1#2{}%
\end{lstlisting}

\newpage

\rule{\linewidth}{1pt}
\begin{lstlisting}
 \def\showDiff#1#2{}%
\end{lstlisting}

\end{document}
share|improve this answer
1  
Could you explain what your code is doing? –  Seamus Jan 23 '11 at 12:18
    
It produce two listings, one on each column, with the numbers on the left. But instead of increasing the space between the columns, it adds a margin for the numbers, so the listing is not as large as before without having narrower columns. Not quite the solution i was thinking about, but it produces a good result. –  Thomas Schwery Jan 23 '11 at 17:24
    
Even though it doesn't exactly answer the question, it's the solution i'm using now ... –  Thomas Schwery Jan 29 '11 at 21:53

Even if it were possible, for code listings I would find line numbers on the right hand side unclear and confusing. I suggest to increase the space between columns in your document:

\setlength{\columnsep}{25pt}

EDIT: Fixed embarassing typo in code sampling.

share|improve this answer
    
That's what i'm doing for now, but i would like to know if there was a way to change automatically the side of the numbering depending on the column the listing is in. –  Thomas Schwery Jan 23 '11 at 13:00
    
the numbers are always on the left. See my example for setting it correct. –  Herbert Jan 23 '11 at 15:02

You can try this:

\documentclass[a4paper,twocolumn]{article}
\usepackage[switch]{lineno}

\begin{document}
\linenumbers

\end{document}
share|improve this answer
    
But that only gives you line numbering, one of many features of the listings package. –  Chris H Jan 30 at 13:07

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.