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.

So I have this LaTeX snippet:

\section{Appendix}
\listoffigures
...
\begin{center}
\begin{figure}
\caption{MacroCell with 8 AND Gate Arrays}
\lstinputlisting[language=VHDL,numbers=left,breaklines=true,basicstyle=\footnotesize, float=h]{macrocell8.vhd}
\end{figure}
\end{center}
\pagebreak

I use this template to display my code all the time, but sometimes it will not continue to the next page and centered. I tried many different ways, and sometimes if I move the \end{figure} this it works, but today neither worked...:

\section{Appendix}
\listoffigures
...
\begin{center}
\begin{figure}
\caption{MacroCell with 8 AND Gate Arrays}
\end{figure}
\lstinputlisting[language=VHDL,numbers=left,breaklines=true,basicstyle=\footnotesize, float=h]{macrocell8.vhd}
\end{center}
\pagebreak
share|improve this question
add comment

2 Answers

up vote 8 down vote accepted

The lstlisting environment (whether used directly or via \lstinputlisting) together with the float specifier does not break across pages. The float key boxes the entire listing into a single unit or block that allows it to float. It does not break the listing into separate floats the with move as a sequential unit. This is similar to the restriction imposed by the minipage environment. Even though the listings package documentation mentions breaking (and a host of breaking options), they all refer to line breaking, but not page breaking.

So, in order for the listing to break across pages, you could remove the float=<specifier> key-value. However, this does not accommodate an automatic (continued) caption for subsequent page-broken listings. This may not be that important (or as big-a-deal) if the listing spans only two pages, since a reader would easily be able to follow the broken listing. However, a listing spanning multiple pages might not be such a good idea.

Alternatively, if you want you could manually break the listing into (at least) two blocks so that neither block is larger than a single page. For this you can use a combination of the firstline and lastline option:

% First part of listing
\lstinputlisting[...,lastline=<x>]{<filename>}

% Second part of listing
\lstinputlisting[...,firstline=<x+1>,firstnumber=<x+1>]{<filename>}

where <x> is the last line of the first part of the lstlisting, and <x+1> the first line of the second part of the lstlisting. If you are using line numbers in your listing, you may need to specify the line number for the second part, as this may default to starting from 1. Hence, the addition of firstnumber=<x+1> as part of the lstlisting key-value sequence. You may also be required to modify the listing caption and specify that the listing continues from some previous one rather than having a number that does not match the former. If your listing has to be split across more than two pages, the same configuration as above holds, with the use of linerange=<first>-<last>,firstnumber=<first> for the middle listings.

There is no need to place the lstinputlisting environment in a float (like figure), since it the package provides it's own form of floating listing (via the key-value float=<specifier>).

Lastly, an alternative that should work but is not easy to accomplish, is to actually shorten your code so that the listing can fit on a single page.

On a side-note, wrapping the center environment around a floating environment (like figure) won't help you either and cause problems. In any case, the outer-most environment should be the float (table, or figure, or...), with the formatting environments contained within.

share|improve this answer
add comment

instead of

\begin{center}
\begin{figure}
\caption{MacroCell with 8 AND Gate Arrays}
\lstinputlisting[language=VHDL,numbers=left,breaklines=true,
                 basicstyle=\footnotesize, float=h]{macrocell8.vhd}
\end{figure}
\end{center}

use

\lstinputlisting[language=VHDL,numbers=left,breaklines=true,
                 basicstyle=\footnotesize, 
                 caption={MacroCell with 8 AND Gate Arrays}]{macrocell8.vhd}

However, if you need it named as a "Figure ..." then use it as

\captionof{figure}{MacroCell with 8 AND Gate Arrays}
\lstinputlisting[language=VHDL,numbers=left,breaklines=true,
                 basicstyle=\footnotesize]{macrocell8.vhd}

which needs package caption for \captionof

share|improve this answer
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.