如何防止列表跨越两页

如何防止列表跨越两页

如果我有一个小列表,恰好位于页面末尾,我该如何强制 LaTeX 将其放在新页面上,以便它不会跨越两页,这对于小列表来说看起来很丑陋。

答案1

使用可选参数float,它将在一页上:

\documentclass{scrreprt}

\usepackage{blindtext}% only for demo
\usepackage{listings}

\textheight=10cm% only for demo
\begin{document}

\blindtext
\vspace{2cm}% only for demo
Now comes the listing which is a float.

\begin{lstlisting}[basicstyle=\ttfamily,float]
foo
 bar
   baz
   baz
 bar
foo
foo
 bar
   baz
   baz
 bar
foo
\end{lstlisting}

\end{document}

也可以尝试不使用可选参数。

答案2

对于简单的事情,你可以尝试needspace包裹。

另一方面,我编写了一个名为的样式numberedblock.sty,但我从未将其上传到 CTAN。我已使用\makeatletterand\makeatother语法将其包含在此示例中。它将确保您的代码片段不会跨页面,因为它将内容(内部)放入框中。您可能会发现它很有用:

\documentclass[10pt]{article}
%\usepackage{numberedblock}
\makeatletter
%\ProvidesPackage {numberedblock} [2007/03/16 v1.01]
%
% by Steven B. Segletes, for the public domain.
%
% numberedblock provides several routines 
% intended to print out a block of code with a unique,
% sequentially indexed label.  The code block is printed to the left,
% with the block label printed centered vertically on the code block,
% horizontally right-justified.  Each line of code is not numbered... 
% rather, the block of code gets a single label.
%
% There are three parameters with which the user may freely alter:
% 1) maxblocklabelsize is a length set aside for the code-block labels.
%    If it is too small, the label will run off the right margin; too large
%    and it limits the width of the code block itself.  However, by setting
%    this parameter to a negative value, it will force the label to be
%    offset into the right margin, which may be a desirable feature.
% 2) blockindent is a length defining the left-side indent to be used
%    for the code block to be printed.
% 3) blocklabel is the command that actually formats the block label to
%    the desired appearance.  Currently, italicized numbers are used within
%    non-italicized square brackets to comprise the block label.
%
% There is a command-line version called numblock, which cannot
% handle verbatim input.  And there is an environment called numVblock
% which handles cases invloving verbatim input
%
% An example of how to use numberedblock is given below.  The
% double-backslash is used for linebreaks in the code. Tildes need to
% be used for hard spaces under two conditions: when the code contains
% multiple sequential spaces; and if a space follows a period (which
% would otherwise invoke end-of-sentence spacing considerations).  In
% addition, special LaTeX characters need to be quoted in the
% appropriate manner of LaTeX.
%
%   \numblock{Line 1 of code\\Line 2 of code\\Line 3...}
%
% In contrast, the numVblock environment is a verbatim environment:
%
%   \begin{numVblock}{
%         program test
%         implicit none
%         integer a, x
%         a = 0
%         x = 1
%      10 a = a + x
%         if (a .eq. 100) stop
%         goto 10
%         end
%   \end{numVblock}
%
% The font used for the code block itself is the ttfamily (typewriter)
% of fixed-width characters.  The counter named blocknum is used to
% index the code block sequence.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\NeedsTeXFormat{LaTeX2e}
\@ifundefined{verbatim@processline}{\RequirePackage{verbatim}}{}
\usepackage{verbatimbox}

\newsavebox{\@savedverbbox}
\newlength\maxblocklabelsize
\newlength\blockindent
                                  \setlength\maxblocklabelsize{-0.4in}
                                         \setlength\blockindent{0.2in}
                     \newcommand\blocklabel[1]{[\textit{\arabic{#1}}]}
\newcounter{blocknum}
\setcounter{blocknum}{0}
\newlength\codeblockwidth
\newlength\parindentsave
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\def\numVblock{%
  \sbox\@savedverbbox{\usebox{\savedverbbox}}
  \verbbox
}
\def\endnumVblock{
  \endverbbox
  \numblock{\theverbbox[t]}
  \global\sbox{\savedverbbox}{\usebox{\@savedverbbox}}
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcommand\numblock[1]{
  \setlength\codeblockwidth{\textwidth}
  \addtolength\codeblockwidth{-\maxblocklabelsize}
  \addtolength\codeblockwidth{-\blockindent}
  \setlength\parindentsave{\parindent}
  \parindent 0in
  \addtocounter{blocknum}{1}
  \vspace{\abovecaptionskip}
  \begin{tabular}{@{\hspace{\blockindent}} l @{} r @{}}
     {\tt
      \begin{tabular*}{\codeblockwidth}{@{} l @{}}
        #1
      \end{tabular*}%
     }
     & \makebox[\maxblocklabelsize]{\hfill\blocklabel{blocknum}}\\
  \end{tabular}
  \vspace{\belowcaptionskip}
  \setlength{\parindent}{\parindentsave}
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%\endinput
\makeatother
\begin{document}

\parindent 0.3in
%\setlength\maxblocklabelsize{-.4in}
\setlength\blockindent{0.0in}

This is a test of the \textsf{numberedblock} style packcage, which is
specially designed to produce sequentially numbered BLOCKS of code (note
the individual code lines are not numbered, but the whole block gets a
single number, for later reference (much in the same way that equations
can get numbered in a document).  While specialized for numbering code
blocks, the commands can actually number other items, as well, in fact
anything that fits in a \LaTeX{} box.

If the code block contains no special characters, one can simply use the
command form, called \verb,\numblock,.  It cannot handle verbatim text,
but must use standard \LaTeX{} escape sequences (for line breaks,
contiguous spaces, special characters, etc.).  It puts the output in a
tt font , which is the same used in the verbatim environment:

\numblock{This text is the\\argument to the command\\where double
slashes have been\\used for line breaks}

Most useful, however, there is also the \verb,numVblock, environment,
which handles verbatim text, as seen in the next example:

\begin{numVblock}
This is the numVblock 
environment, which         (<--see contiguous spaces here)
succeeds in
incorporating verbatim text like
@##$%*$%$()||}{?><\\    \end{numVblock}

As envisioned the \verb,numVblock, environment would be ideally suited
for displaying small code blocks as part of documentation.  The code can
contain contiguous spaces and special characters:

\begin{numVblock}
      program test
      implicit none
      integer a, x
c$$$$$$$$$$$$$$$$$$$$$$$$$
      a = 0
      x = 1
   10 a = a + x
      if (a .eq. 100) stop
      goto 10
      end
\end{numVblock}

Below, I test the \verb,\numblock, command with the argument as a
box, rather than as formatted text.

\numblock{\fbox{Testing, 1,2,3 testing a box}}

Don't forget, there are settable parameters to define the block
left-indent, the format of the label, and (if needed) the labels' max
width/placement.

\end{document}

在此处输入图片描述

相关内容