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.

This happens when I use programming code and enumeration, how can I add a gap between the numbering and the line numbers of the code. enter image description here

I'm using this in LaTeX Preamble:

\usepackage{listings}
\lstset{language=java,numbers=left,tabsize=2,showstringspaces=false,showspaces=false}
share|improve this question
3  
Add an appropriate value for xleftmargin in your key-value settings. For example, xleftmargin=2em. The default is 0pt, putting the numbers flush against the left margin (and overlaying with your enumeration). –  Werner Feb 23 '12 at 21:17
    
I tried \lstset{language=java,numbers=left,tabsize=2,showstringspaces=false,showspaces=‌​false,xleftmargin=2em} it moves the numbered bullet as well. –  orange Feb 23 '12 at 21:27
    
I'll have to get back to you on that, since it seems like you're using LyX, and therefore things may be different (more difficult) compared to regular LaTeX - not all LyX users enjoy using ERTs. –  Werner Feb 23 '12 at 21:30
    
Ok, Thank you.. –  orange Feb 23 '12 at 21:32
    
A not-optimal fix is to change the paragraph setting of the list as well as setting the xleftmargin. Right-click the number and choose Paragraph settings, uncheck the "Indent paragraph" box. You won't get the overlap, but the program listing starts below the list number. –  Torbjørn T. Feb 24 '12 at 0:43

1 Answer 1

up vote 6 down vote accepted

Besides addin xleftmargin=<size> to shift the listings over, you also need resetmargins=true which resets the indention from list environments like enumerate or itemize.

The frame here is from the geometry package to show the alignment with the margins.

enter image description here

Code:

Commenting out the resetmargins=true below will illustrate the problem.

\documentclass{article}
\usepackage[showframe]{geometry}
\usepackage{listings}

\lstset{
    language=java,
    numbers=left,
    tabsize=2,
    showstringspaces=false,
    showspaces=false,
    numberstyle=\tiny,
    xleftmargin=4em,
    resetmargins=true,
}


\begin{document}
\begin{enumerate}
\item
\begin{lstlisting}
 public int nextInt(int n) {
     if (n<=0)
     return val;
 }
\end{lstlisting}
%
\item abcd
%
\item
\begin{lstlisting}
 public int nextInt(int n) {
     if (n<=0)
     return val;
 }
\end{lstlisting}
\end{enumerate}
\end{document}
share|improve this answer

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.