正如标题所述。我在我的 中使用了以下内容lstset
:
framerule=1pt,
frame=tblr
有些地方的分页符出现在我的中间\lstlisting
。很好。但有时列表的整个代码出现在一页上,但底部框架出现在下一页的顶部!
我知道我可以float
列出特定的列表,但我有一个包含许多列表的长文档,我不想让所有列表都浮动;其中大多数列表也需要准确地位于声明的位置。它们分散在一页上并不困扰我那很多。
有什么方法可以阻止这种情况发生,而我不需要调整每个单独的列表?
这家伙遇到了同样的问题,但通过删除自定义语言定义中关键字前面的空格解决了该问题。我也有一个自定义语言定义,但他的解决方案对我没有帮助。我设法在没有自定义语言的情况下重现了这种情况。
编辑和 MWE
我设法在一个最小的工作示例中重现了它。这里如果有帮助的话,这是一个 pastebin。
\documentclass[UKenglish,a4paper,11pt]{report}
\usepackage{babel}
%% Adjustments
\addtolength{\topmargin}{-1\baselineskip}
\addtolength{\textheight}{4\baselineskip}
\pretolerance = 2000
\tolerance = 5000 \hbadness = \tolerance
%% Smaller font
\usepackage{mathptmx}
%% LISTINGS
\usepackage[final]{listings}
\lstset{
xleftmargin=1.5em,
framexleftmargin=1em,
framextopmargin=0.5ex,
%% THE FRAME:
framerule=1pt,
frame=tblr
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\usepackage{lipsum} % for demo
\begin{document}
\subsubsection{Lorem} % some fiddling to get the "wrong spacing just right":
\lipsum[1-3]
\subsubsection{Ipsum}
\lipsum[1] and then some text
\begin{lstlisting}
Here is a listing with
three lines of code;
The bottom border is placed on the next page!
\end{lstlisting}
Can you believe it..?!
\end{document}
这个 MWE 相当长。我的文档输入了其他人编写的文件中的大部分列表内容,所以我不确定它到底是做什么的。如果有人看到一些行,他们当然无关紧要,请随意将其从 MWE 中取出。
“有趣的事实”
如果删除,也会出现同样愚蠢的情况\usepackage{mathptmx}
:整个代码列表将出现在新页面上,除了留在第一页底部的顶部框架!
答案1
已知带包装的框架listings
是有问题的。
我已经创建了一个新的环境mylisting
,它tcolorbox
可以重现您的问题\lstset
,但没有这些问题。
平均能量损失
\documentclass[UKenglish,a4paper,11pt]{report}
\usepackage{babel}
%% Adjustments
\addtolength{\topmargin}{-1\baselineskip}
\addtolength{\textheight}{4\baselineskip}
\pretolerance = 2000
\tolerance = 5000 \hbadness = \tolerance
%% Smaller font
\usepackage{mathptmx}
%% LISTINGS
\usepackage[final]{listings}
\usepackage{tcolorbox}
\tcbuselibrary{listings,breakable}
\newtcblisting{mylisting}{
arc=0pt,
top=0mm,
bottom=0mm,
left=0mm,
right=0mm,
colback=white,
breakable,
boxrule=1pt,
listing only,
listing options={xleftmargin=1.5em,framexleftmargin=1em,framextopmargin=0.5ex}
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\usepackage{lipsum} % for demo
\begin{document}
\subsubsection{Lorem} % some fiddling to get the "wrong spacing just right":
\lipsum[1-3]
\subsubsection{Ipsum}
\lipsum[1]
\begin{mylisting}
Here is a listing with
three lines of code;
The bottom border is placed on the next page!
\end{mylisting}
Can you believe it..?!
\end{document}
答案2
tcolorbox
您可以使用支持的非常强大的软件包listings
。
这是一个例子,框架可以通过各种方式进行定制。我使用danlisting
环境的可选参数只是为了表明你使用它来将选项传递给lstlisting
。环境可跨页面中断。
\documentclass[UKenglish,a4paper,11pt]{report}
\usepackage{babel}
%% Adjustments
\addtolength{\topmargin}{-1\baselineskip}
\addtolength{\textheight}{4\baselineskip}
\pretolerance = 2000
\tolerance = 5000 \hbadness = \tolerance
%% Smaller font
\usepackage{mathptmx}
%% LISTINGS
\usepackage{tcolorbox}
\tcbuselibrary{listings,breakable}
\newtcblisting{danlisting}[1][]{%
breakable,
listing only,
listing options={#1},
}
%\lstset{
% xleftmargin=1.5em,
%}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\usepackage{lipsum} % for demo
\begin{document}
\subsubsection{Lorem} % some fiddling to get the "wrong spacing just right":
\lipsum[1-3]
\subsubsection{Ipsum}
\lipsum[1] and then some text
\begin{danlisting}[xleftmargin=1.5em,basicstyle=\ttfamily]
Here is a listing with
three lines of code;
The bottom border is placed on the next page!
\end{danlisting}
Can you believe it..?!
\begin{danlisting}[xleftmargin=1.5em,basicstyle=\ttfamily]
Here is a listing with
three lines of code;
The bottom border is placed on the next page!
Here is a listing with
three lines of code;
The bottom border is placed on the next page!
Here is a listing with
three lines of code;
The bottom border is placed on the next page!
Here is a listing with
three lines of code;
The bottom border is placed on the next page!
Here is a listing with
three lines of code;
The bottom border is placed on the next page!
Here is a listing with
three lines of code;
The bottom border is placed on the next page!
Here is a listing with
three lines of code;
The bottom border is placed on the next page!
Here is a listing with
three lines of code;
The bottom border is placed on the next page!
Here is a listing with
three lines of code;
The bottom border is placed on the next page!
Here is a listing with
three lines of code;
The bottom border is placed on the next page!
Here is a listing with
three lines of code;
The bottom border is placed on the next page!
Here is a listing with
three lines of code;
The bottom border is placed on the next page!
Here is a listing with
three lines of code;
The bottom border is placed on the next page!
Here is a listing with
three lines of code;
The bottom border is placed on the next page!
Here is a listing with
three lines of code;
The bottom border is placed on the next page!
Here is a listing with
three lines of code;
The bottom border is placed on the next page!
Here is a listing with
three lines of code;
The bottom border is placed on the next page!
Here is a listing with
three lines of code;
The bottom border is placed on the next page!
Here is a listing with
three lines of code;
The bottom border is placed on the next page!
Here is a listing with
three lines of code;
The bottom border is placed on the next page!
Here is a listing with
three lines of code;
The bottom border is placed on the next page!
Here is a listing with
three lines of code;
The bottom border is placed on the next page!
Here is a listing with
three lines of code;
The bottom border is placed on the next page!
Here is a listing with
three lines of code;
The bottom border is placed on the next page!
Here is a listing with
three lines of code;
The bottom border is placed on the next page!
Here is a listing with
three lines of code;
The bottom border is placed on the next page!
Here is a listing with
three lines of code;
The bottom border is placed on the next page!
Here is a listing with
three lines of code;
The bottom border is placed on the next page!
\end{danlisting}
\end{document}