避免在 \lstlistings 中分页

避免在 \lstlistings 中分页

我试图避免在我的 中出现分页符lstlisting(见第 0.0.2 节),我知道我可以通过浮动我的 来解决这个问题lstlisting,但它会增加一些不需要的空白(见第 0.0.1 节)。

漂浮

有没有其他方法可以float使我的lstlisting遗嘱不跨页面?

或者

我可以配置float以消除空白,使其看起来与不在时完全相同吗?float

无浮动

*我对 MWE 中的杂乱之处表示歉意。我只是想在更大的文档中使用它,所以我宁愿让它尽可能地接近它。

\documentclass{article}

% New commands
\newcommand{\HRule}{\rule{\linewidth}{0.5mm}}
\newcommand{\superscript}[1]{\ensuremath{^{\textrm{#1}}}}

% Modified commands
\renewcommand*\abstractname{Revision History}

%%%%% Packages and configuration %%%%%
\usepackage[firstpage]{draftwatermark}
\SetWatermarkText{\textsc{Confidential}}
\SetWatermarkScale{4}
\SetWatermarkColor[gray]{0.9}

%\usepackage{fancyhdr}
%\pagestyle{fancy}
%\fancyhf{} % clear all fields
%\fancyfoot[LE,CO]{\thepage}           % page number in "outer" position of footer line
%\fancyfoot[RE,LO]{Message of the day} % other info in "inner" position of footer line

\usepackage{tabularx}

\usepackage[table]{xcolor}
\definecolor{dark-red}{rgb}{0.4,0.15,0.15}
\definecolor{dark-blue}{rgb}{0.15,0.15,0.4}
\definecolor{medium-blue}{rgb}{0,0,0.5}
\definecolor{light-grey}{gray}{0.8}

\usepackage[hidelinks]{hyperref}% keep at end
\hypersetup{
    colorlinks,
    linkcolor={black},
    citecolor={dark-blue}, 
    urlcolor={medium-blue}
}

%\usepackage[cm]{fullpage}
%\usepackage[letterpaper, top=0.7in, bottom=0.9in, left=0.7in, right=0.7in, showframe]{geometry}
\usepackage[margin=0.5in]{geometry}


%\usepackage{color}
\usepackage{listings}
\lstset{ %
language=C,                % choose the language of the code
basicstyle=\footnotesize,       % the size of the fonts that are used for the code
%numbers=left,                   % where to put the line-numbers
%numberstyle=\footnotesize,      % the size of the fonts that are used for the line-numbers
%stepnumber=1,                   % the step between two line-numbers. If it is 1 each line will be numbered
%numbersep=5pt,                  % how far the line-numbers are from the code
%backgroundcolor=\color{white},  % choose the background color. You must add \usepackage{color}
showspaces=false,               % show spaces adding particular underscores
showstringspaces=false,         % underline spaces within strings
showtabs=false,                 % show tabs within strings adding particular underscores
%frame=single,           % adds a frame around the code
tabsize=4,          % sets default tabsize to 4 spaces
captionpos=b,           % sets the caption-position to bottom
breaklines=true,        % sets automatic line breaking
breakatwhitespace=false,    % sets if automatic breaks should only happen at whitespace
%escapeinside={\%*}{*)}          % if you want to add a comment within your code
}

\usepackage{mdwlist}
\usepackage{underscore}
\usepackage{tikz}
\usepackage{float}
\usepackage{lipsum}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\usepackage{etex}
\usepackage{etoolbox}
\makeatletter
\patchcmd{\@afterheading}%
    {\clubpenalty \@M}{\clubpenalties 2 \@M \@M }{}{}
\patchcmd{\@afterheading}%
    {\clubpenalty \@clubpenalty}{\clubpenalties 1 \@clubpenalty }{}{}
\makeatother
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{document}

\lipsum[1-8]

\subsubsection{Lorem Ipsum Dolomite}\leavevmode
\begin{lstlisting}
void abc_defgh_ijklmn_stuff * const ( void );
\end{lstlisting}

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc id sapien eget magna tempus ornare vitae ut dolor. Donec pharetra diam vitae erat sollicitudin a blandit sapien blandit. Nam ligula metus, imperdiet ut accumsan non, tincidunt id a.

\subsubsection{abcde}
\begin{lstlisting}
static boolean abcde
(
abc_defg_hijk * const   lm
);
\end{lstlisting}

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc id sapien eget magna tempus ornare vitae ut dolor. Donec pharetra diam vitae erat sollicitudin a blandit sapien blandit. Nam ligula metus, imperdiet ut accumsan non, tincidunt id a.\\

\end{document}

答案1

将内容包裹在宽度为的盒子lstlisting内部,使其牢不可破,并避免跨越页面边界:minipage\linewidth

...
\begin{minipage}{\linewidth}
\begin{lstlisting}
static boolean abcde
(
abc_defg_hijk * const   lm
);
\end{lstlisting}
\end{minipage}
...

根据使用位置,您可能必须在环境前面添加以\noindent避免任何段落缩进。

如果您加载float包裹并使用

...
\begin{lstlisting}[float,floatplacement=H]
static boolean abcde
(
abc_defg_hijk * const   lm
);
\end{lstlisting}
...

有关浮动位置的更多信息,H请参阅如何影响 LaTeX 中图形和表格等浮动环境的位置?

答案2

我使用的是 1.5 版(2013)的listings软件包。我习惯于lstinputlisting从文件中包含代码,然后将其放入float环境选项中。它运行良好。

例如,我的序言是这样写的:

\lstdefinestyle{freefempp}{
  language=C++,
  basicstyle=\footnotesize\ttfamily,
  commentstyle=\itshape\color{violet},
  identifierstyle=\color{blue},
  morekeywords={ border, buildmesh, cos, dx, dy, fespace, fill, func,
    int2d, label, mesh, on, pi, plot, problem, sin, real, x, y},
  % float,
  frame=single,
  numbers=left
}

\lstset{style=freefempp}

并将其放入我的文档主体中。

\lstinputlisting
  [caption=Solution to question 1, float, linerange={1-21}, firstnumber=1]
  {listings/ex1-1.edp}

但是,float 选项仅在lstinputlisting环境定义中起作用,而不在样式定义中起作用。也许这个错误将在未来的版本中得到纠正。

答案3

我使用了与 werner 建议的类似的解决方案。不过我更喜欢用文本而不是线宽来定义小页面:

\begin{minipage}[c]{0.95\textwidth}
\begin{lstlisting}

\end{lstlisting}
\end{minipage}

“c”表示居中,“0.95\textwidth”使列表比其余文本稍微小一点。

相关内容