Generallstlisting - behaviour like figure

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
mm
Posts: 4
Joined: Tue Mar 24, 2009 3:37 am

lstlisting - behaviour like figure

Post by mm »

Hello guys,

I can't find the solution for my problem:

I have some code in lstlisting. I want it to be on whole page (like it is in figures - not broken by new page). I have put it in figure tags, but then it is marked as figure, not listing.

So, how can i put listing that won't be break by new page but put on whole one / or how to name figure as listing.

Thanks,
Michal

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

lstlisting - behaviour like figure

Post by localghost »

Read Section 4.3 (Space and placement, p. 26) of the listings manual to make your listings behave like a float with an according caption label.


Best regards
Thorsten¹
User avatar
gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

lstlisting - behaviour like figure

Post by gmedina »

Hi,

if you don't want your code to float but still want to keep it from splitting, you can use a minipage environment, as the following code suggests:

Code: Select all

\noindent\begin{minipage}{\textwidth}
\begin{lstlisting}
...
(code)
...
\end{lstlisting}
\end{minipage}
1,1,2,3,5,8,13,21,34,55,89,144,233,...
eleanor
Posts: 19
Joined: Sat Oct 03, 2009 6:03 pm

lstlisting - behaviour like figure

Post by eleanor »

Hi. Sorry for brining up an old thread, but I have a question that completely correlates to this topic.

Ok, this solution works perfectly, but is there a way that I can make this permanent, so that I don't always have to write:

Code: Select all

\noindent\begin{minipage}{\textwidth}
\begin{lstlisting}
...
\end{lstlisting}
\end{minipage}
but to only write:

Code: Select all

\begin{lstlisting}
...
\end{lstlisting}
and the rest is automatically observed?
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Re: lstlisting - behaviour like figure

Post by localghost »

See my last reply and the link for the package manual.
oxon
Posts: 1
Joined: Tue Jan 05, 2010 1:49 pm

Re: lstlisting - behaviour like figure

Post by oxon »

Hi,

float option is actually works fine. But when I put a long code in the manuscript, it is forced to be in only one page, and then most of the code is truncated and outside of the page.

If I remove the float option, usual figures often "split" long codes. It looks ugly.

Does anyone know a workaround?

Akira
Post Reply