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 a beginning latex user. I have some programming code in my document which I format using the lstlisting environment from the package listings.

However this still allows the code segments to be split into two pieces when placed near the end of a page.

To fix this I also use a figure environment for code. This causes an often recurring pattern:

\begin{figure}[H]
\begin{lstlisting}
..code..
\end{lstlisting}
\end{figure}

How can I define a new environment for this pattern? I tried:

\newenvironment{mylisting}
{
\begin{figure}[H]
\begin{lstlisting}
}
{
\end{lstlisting}
\end{figure}
}

However using this environment gave errors all over.

share|improve this question

migrated from stackoverflow.com Apr 5 '11 at 14:07

This question came from our site for professional and enthusiast programmers.

1 Answer 1

up vote 5 down vote accepted

The problem is that the listings package needs to make all kinds of changes to the internals of LaTeX in order to evaluate the contents of the lstlisting environment in a non-LaTeX way. In particular it shuts down expansion of control sequences starting with \ so the end code to your mylisting environment gets passed right by.

If all you want to do is have a listing in a float, you can do

\begin{lstlisting}[float=h]
...
\end{lstlisting}

You might find some of these questions about the listings package on the TeX SE useful as well.

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.