当包含文件中的源代码(以便自动保持最新)时,无法通过编辑源代码来影响格式。当文档中的段落发生变化时,列表可能会变得难以阅读。
这是因为源代码通常被组织成“段落”(例如下面的函数签名及其注释),我不希望打断它们。有没有办法强制 lstlisting 仅在空行处分页?下面是一个简单的示例lstinputlisting
:
\RequirePackage{filecontents}
\begin{filecontents*}{stack.mli}
(***********************************************************************)
(* *)
(* OCaml *)
(* *)
(* Xavier Leroy, projet Cristal, INRIA Rocquencourt *)
(* *)
(* Copyright 1996 Institut National de Recherche en Informatique et *)
(* en Automatique. *)
(* *)
(* All rights reserved. This file is distributed under the terms of *)
(* the GNU Lesser General Public License version 2.1, with the *)
(* special exception on linking described in the file LICENSE. *)
(* *)
(***********************************************************************)
(** Last-in first-out stacks.
This module implements stacks (LIFOs), with in-place modification.
*)
type 'a t
(** The type of stacks containing elements of type ['a]. *)
exception Empty
(** Raised when {!Stack.pop} or {!Stack.top} is applied to an empty stack. *)
val create : unit -> 'a t
(** Return a new stack, initially empty. *)
val push : 'a -> 'a t -> unit
(** [push x s] adds the element [x] at the top of stack [s]. *)
val pop : 'a t -> 'a
(** [pop s] removes and returns the topmost element in stack [s],
or raises {!Empty} if the stack is empty. *)
val top : 'a t -> 'a
(** [top s] returns the topmost element in stack [s],
or raises {!Empty} if the stack is empty. *)
val clear : 'a t -> unit
(** Discard all elements from a stack. *)
val copy : 'a t -> 'a t
(** Return a copy of the given stack. *)
val is_empty : 'a t -> bool
(** Return [true] if the given stack is empty, [false] otherwise. *)
val length : 'a t -> int
(** Return the number of elements in a stack. Time complexity O(1) *)
val iter : ('a -> unit) -> 'a t -> unit
(** [iter f s] applies [f] in turn to all elements of [s],
from the element at the top of the stack to the element at the
bottom of the stack. The stack itself is unchanged. *)
val fold : ('b -> 'a -> 'b) -> 'b -> 'a t -> 'b
(** [fold f accu s] is [(f (... (f (f accu x1) x2) ...) xn)]
where [x1] is the top of the stack, [x2] the second element,
and [xn] the bottom element. The stack is unchanged.
@since 4.03 *)
\end{filecontents*}
\documentclass{article}
\usepackage{listings}
\begin{document}
\section{Stack}
\lstset{
basicstyle={\ttfamily\lst@ifdisplaystyle\footnotesize\fi},
language=[Objective]Caml
}
\lstinputlisting{stack.mli}
\end{document}
在上面的例子中,我在copy
和它的注释之间得到了一个中断,尽管早断一行会更好。
答案1
根据https://tex.stackexchange.com/a/73305/89417防止列表中出现分页符的解决方案是使用minipage
。将每个代码段落放在单独的小页面中,因此会在段落之间中断。您可以使用任何脚本语言轻松自动执行此操作,例如perl
:
print "\\noindent\\begin{minipage}{\\linewidth}\\begin{lstlisting}\n";
while(<>){
print $_ eq "\n" ? "\\end{lstlisting}\\end{minipage}\n\n\\noindent\\begin{minipage}{\\linewidth}\\begin{lstlisting}\n" : $_;
}
print "\\end{lstlisting}\\end{minipage}\n";
该脚本将空行转换为小页面的结束和开始,并添加额外的开始和结束以匹配第一段和最后一段。
然后,在 LaTeX 中,您可以调用此脚本(假设在 Linux 中,使用 运行--shell-escape
):
\documentclass{article}
\usepackage{listings}
\usepackage[margin=1.5in]{geometry} % make OCaml comment block fit on a line
\begin{document}
\section{Stack}
\lstset{
basicstyle={\ttfamily\lst@ifdisplaystyle\footnotesize\fi},
language=[Objective]Caml
}
\input{|"perl format_listing.pl < stack.mli"}
\end{document}
结果:
答案2
如果只有一个分页符,一个简单的方法是使用\enlargethispage
。
这会将该val copy
行移至下一页。
\documentclass{article}
\usepackage{listings}
\begin{document}
\section{Stack}
\lstset{
basicstyle={\ttfamily\lst@ifdisplaystyle\footnotesize\fi},
language=[Objective]Caml
}
\enlargethispage{-\baselineskip}
\lstinputlisting{stack.mli}
\end{document}