I'm trying to document source code using LaTeX.
Documentation of a method consists of two blocks: One is a fancyvrb
Verbatim environment that contains the function prototype. This
environment uses the option frame=bottomline to draw a line below the
prototype. The second block is a quote environment that contains text,
describing the method.
Unfortunately, sometimes LaTeX inserts a page break between the
prototype and its description which confuses the reader.
I tried to avoid the pagebreak using a samepage environment around both
blocks but this doesn't work. It results in a page break being inserted
between the prototype and the bottomline of the Verbatim environment.
(See the example below.)
\nopagebreak between the two blocks also doesn't work.
Any hints?
Thanks in advance,
--leo
-------------------- 8< --------------------
\documentclass[a4paper,12pt]{article}
\usepackage{parskip}
\usepackage{fancyvrb}
\DefineVerbatimEnvironment{TEcode}{Verbatim}{frame=bottomline,gobble=2,commandchars=\\\{\}}
\newenvironment{TEcodedesc}
{\begin{quote}\par\vspace{-\parskip}}
{\end{quote}}
\DefineShortVerb[commandchars=\\\{\}]{\|}
\begin{document}
\section{Section 1}
\LaTeX{} is a document preparation system for the \TeX{}
typesetting program. It offers programmable desktop publishing
features and extensive facilities for automating most aspects of
typesetting and desktop publishing, including numbering and
cross-referencing, tables and figures, page layout, bibliographies,
and much more. \LaTeX{} was originally written in 1984 by Leslie
Lamport and has become the dominant method for using \TeX; few
people write in plain \TeX{} anymore. The current version is
\LaTeXe.
\LaTeX{} is a document preparation system for the \TeX{}
typesetting program. It offers programmable desktop publishing
features and extensive facilities for automating most aspects of
typesetting and desktop publishing, including numbering and
cross-referencing, tables and figures, page layout, bibliographies,
and much more. \LaTeX{} was originally written in 1984 by Leslie
Lamport and has become the dominant method for using \TeX; few
people write in plain \TeX{} anymore. The current version is
\LaTeXe.
\LaTeX{} is a document preparation system for the \TeX{}
typesetting program. It offers programmable desktop publishing
features and extensive facilities for automating most aspects of
typesetting and desktop publishing, including numbering and
cross-referencing, tables and figures, page layout, bibliographies,
and much more. \LaTeX{} was originally written in 1984 by Leslie
Lamport and has become the dominant method for using \TeX; few
people write in plain \TeX{} anymore. The current version is
\LaTeXe.
\LaTeX{} is a document preparation system for the \TeX{}
typesetting program. It offers programmable desktop publishing
features and extensive facilities for automating most aspects of
typesetting and desktop publishing, including numbering and
cross-referencing, tables and figures, page layout, bibliographies,
and much more. \LaTeX{} was originally written in 1984 by Leslie
Lamport and has become the dominant method for using \TeX; few
people write in plain \TeX{} anymore. The current version is
\LaTeXe.
\section{API documentation}
API documentation follows\dots
\begin{samepage}
\begin{TEcode}
__init__(\emph{environment}=\{\})
\end{TEcode}
\begin{TEcodedesc}
\LaTeX{} is a document preparation system for the \TeX{}
typesetting program. It offers programmable desktop publishing
features and extensive facilities for automating most aspects of
typesetting and desktop publishing, including numbering and
cross-referencing, tables and figures, page layout, bibliographies,
and much more. \LaTeX{} was originally written in 1984 by Leslie
Lamport and has become the dominant method for using \TeX; few
people write in plain \TeX{} anymore. The current version is
\LaTeXe.
\end{TEcodedesc}
\end{samepage}
\end{document}
-------------------- 8< --------------------
Reread the documentation for \samepage. It is critical to know what
it
does and how to use it. (In particular, it does not prevent breaks
between
paragraphs.)
That said, fancyverb is written strangely, using kerns in vertical
mode,
which allow zero-penalty page breaks in places.
Donald Arseneau as...@triumf.ca
> Hi!
>
> I'm trying to document source code using LaTeX.
> Documentation of a method consists of two blocks: One is a fancyvrb
> Verbatim environment that contains the function prototype. This
> environment uses the option frame=bottomline to draw a line below the
> prototype. The second block is a quote environment that contains text,
> describing the method.
>
> Unfortunately, sometimes LaTeX inserts a page break between the
> prototype and its description which confuses the reader.
>
> I tried to avoid the pagebreak using a samepage environment around both
> blocks but this doesn't work. It results in a page break being inserted
> between the prototype and the bottomline of the Verbatim environment.
> (See the example below.)
You could try needspace.sty
http://www.tex.ac.uk/cgi-bin/texfaq2html?label=nopagebrk
--
Ulrike Fischer
Thanks. I've read about needspace and it sounds promising because maybe
it would make sense to force only the first few lines of the descripion
to be on the same page as the prototype rather than the whole block of
text. But obviously I couldn't find the right place to insert it. If I
put it between the two blocks, it is ignored and a pagebreak is inserted
between the blocks anyway. And inserting it at the end of the TEcode
block produces unwanted space and doesn't suppress the pagebreak either.
I've also tried messing around with \@beginparpenalty but I didn't get
better results than with using a samepage environment directly.
Maybe the fancyvrb Verbatim environment is causing all the problems and
I should try to get rid of it?
Any other suggestions?
Thanks,
--leo
>> You could try needspace.sty
>> http://www.tex.ac.uk/cgi-bin/texfaq2html?label=nopagebrk
>
> Thanks. I've read about needspace and it sounds promising because maybe
> it would make sense to force only the first few lines of the descripion
> to be on the same page as the prototype rather than the whole block of
> text. But obviously I couldn't find the right place to insert it. If I
> put it between the two blocks, it is ignored and a pagebreak is inserted
> between the blocks anyway. And inserting it at the end of the TEcode
> block produces unwanted space and doesn't suppress the pagebreak either.
You will probably have to add it before the TEcode.
>
> I've also tried messing around with \@beginparpenalty but I didn't get
> better results than with using a samepage environment directly.
>
> Maybe the fancyvrb Verbatim environment is causing all the problems and
> I should try to get rid of it?
The quote environment too adds breakpoints.
--
Ulrike Fischer
Adding \needsspace before TEcode works quite fine, thanks.
Now I'd like to automatically insert it before each Verbatim-block...
Is there any hook in fancyvrb Verbatim environments, that I could use to
automatically add a command before each TEcode block? (I couldn't find
such an option for \DefineVerbatimEnvironment and the fancyvrb sources
are too heavy stuff for me... ;-))
Cheers,
--leo
> Adding \needsspace before TEcode works quite fine, thanks.
> Now I'd like to automatically insert it before each Verbatim-block...
>
> Is there any hook in fancyvrb Verbatim environments, that I could use to
> automatically add a command before each TEcode block? (I couldn't find
> such an option for \DefineVerbatimEnvironment and the fancyvrb sources
> are too heavy stuff for me... ;-))
Try the formatcom-key. E.g. formatcom=\needspace{5cm}
--
Ulrike Fischer