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 was wondering whether it was possible to align a lstlisting environment to a certain point within an enumerate . The purpose of my doing this is to fit as much code sample on the page as possible, while maintaining a two-column environment.

In the following code sample:

\documentclass[twocolumn]{article}

\usepackage{enumitem, listings}

\begin{document}
\begin{enumerate}
\item First enumerate level
\begin{lstlisting}[frame = single]
First code sample
\end{lstlisting}
\begin{enumerate}
\item Text that is stationary
\begin{lstlisting}[frame = single, breaklines = true, breakindent = 0pt]
Code sample that has the same alignment as the First code sample
\end{lstlisting}
\end{enumerate}
\end{enumerate}
\end{document}

The code sample produces:

Code Sample Alignment

How do I move the second code sample to the left, so that its alignment is the same as the first code sample?

Note that I have tried using \end{enumerate} and \begin{enumerate}[resume]. However, this only seems to suspend the level of enumeration 1 level.

Also, I have tried exiting the enumerate environment altogether by doing \end{enumerate} before the lstlisting environment. However, sometimes I have items following the lstlisting environment.

share|improve this question
    
May I ask: Do you think this would be a good typography? –  Kurt Sep 6 '12 at 23:08
    
@Kurt: Well, it's not perfect, since suppose at the fourth level, there was a code sample that was moved to the first level. It would appear to `come out of nowhere.' I would really like to know a good way include code samples at the fourth level, though. –  jrand Sep 6 '12 at 23:16
add comment

1 Answer

up vote 4 down vote accepted

If you take a look at section 4.10 of the listings manual you can find a few keys that manipulate the horziontal alignment of a lstlisting environment.

  • The option resetmargins reset the margins of environment like itemize and enumerate.
  • The option xleftmargin=\leftmargin pushes the listing back to the first level.

Code:

\documentclass[twocolumn]{article}

\usepackage{enumitem, listings, calc}
\begin{document}
\begin{enumerate}
    \item First enumerate level
    \begin{lstlisting}[frame = single]
First code sample
    \end{lstlisting}
        \begin{enumerate}
        \item Second level
            \begin{enumerate}
            \item Third level
            \begin{lstlisting}[
                resetmargins=true,
                xleftmargin=\leftmargin,
                frame = single, breaklines = true, breakindent = 0pt]
Code sample that has the same alignment as the First code sample
            \end{lstlisting}
            \item Last point in inner level
        \end{enumerate}
        \item Last point in second level
    \end{enumerate}
    \item And again first level
\end{enumerate}
\end{document}

Output

Output of code

share|improve this answer
    
Yes, resume works at one level. But if I have nested enumerate levels, the resume will only go back 1 level. For example, if I am at the 4th level of enumerate, and I \end{enumerate} and \begin{enumerate}[resume], the block that is between those two commands is at the 3rd level. (So, level 4 with resume -> level 3, and lvl. 3 with res. -> lvl. 2, and lvl. 2 with res. -> lvl. 1) <=> (4 -> 3, 3 -> 2, 2 -> 1). I was wondering whether there was a way to move the block to the 1st level. –  jrand Sep 6 '12 at 23:04
    
@jrand I've updated my answer. If this is typographically a good idea is another topic, though. –  Qrrbrbirlbel Sep 6 '12 at 23:50
    
Yes, the answer has to do with xleftmargin and careful use (linear combination) of \framesep, \leftmargini, and some other margin values. –  jrand Sep 6 '12 at 23:54
1  
For the purpose of exactness, the left margins of the code samples will be perfectly aligned if \leftmargin is replaced with \leftmargini . –  jrand Sep 7 '12 at 0:08
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.