Generalnopagebreak problem

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
zeev
Posts: 1
Joined: Wed May 07, 2008 3:56 pm

nopagebreak problem

Post by zeev »

I'm trying to create a new environment for my own code samples. The idea is to make it a non-float, multi-page listing, and then number it just as if it was a float and put a caption underneath. I got this to work, except that in some cases the caption goes to the next page despite all the \nopagebreaks.

A common solution is to use \begin{samepage}...\end{samepage}; however, in my case this won't work as this is a multi-page listing.

For now I'm putting a \begin{verbatim}...\end{verbatim} inside the \begin{largecodesample} in the doc. How can I get LaTeX to prefer to do a page break inside the Verbatim environment, but not between Verbatim and the caption?

This is what I have. You can see how I get desperate with all the \nopagebreak...

Thanks

Code: Select all

\newenvironment{largecodesample}[1]%
{
    \stepcounter{CodeSampleCounter}
    \global\def\codetitle{#1}
    \global\def\mytitle{Code Sample \arabic{CodeSampleCounter}{}: {#1}}
    \begin{shaded}
    \renewcommand\label[1]{\@bsphack
    \protected@write\@auxout{}%
         {\string\newlabel{##1}{{\arabic{CodeSampleCounter}}{\thepage}}}%
    \@esphack}
}
{\nopagebreak
\end{shaded}
\nopagebreak
\begin{center}
\nopagebreak
\textbf{\mytitle}
\end{center}
    \addtocontents{spl}{\protect\contentsline {subsection}%
	{\mytitle\relax}{\thepage}}
}

Recommended reading 2024:

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

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

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

nopagebreak problem

Post by localghost »

For code listings you can use the listings package. Putting that into an own float environment is made possible by the float or trivfloat package. These packages also create the corresponding lists similar to LoF and LoT.


Best regards
Thorsten¹
Post Reply