
我正在使用該listings
套件將原始程式碼放入我的文檔中,我注意到文字和lstlisting
環境之間的空間很小。所以我改變了\aboveskip
和\belowskip
屬性。
問題是,除了我提到的空間之外,它還影響了節標題和lstlisting
環境之間的空間,這是無用且煩人的。而且它也不會產生漂亮的文檔外觀。
另外,我不知道環境和分頁符號之間的空間或新頁面的開頭之間是否有相同的行為(是嗎?),或者 LaTeX 會自行刪除該空間(如果它實際上也插入到那裡)。
我的問題是如何防止插入空格\aboveskip
並\belowskip
影響節標題、分頁符號和頁首之間的空格?
這是沒有\aboveskip
:
這是\aboveskip=1.5cm
(過多的數字顯示章節標題下的「不必要的」空間):
\documentclass[12pt]{article}
\usepackage{parskip}
\setlength{\parindent}{0cm}
\usepackage{fontspec}
\usepackage[english,greek]{babel}
\usepackage[fleqn]{amsmath}
\usepackage{unicode-math}
\usepackage{listings}
\setmainfont
[
Ligatures=TeX,
Extension=.otf,
UprightFont=*,
BoldFont=*Bold,
ItalicFont=*It,
BoldItalicFont=*BoldIt,
Mapping=tex-text
]{GFSArtemisia}
\setsansfont[Mapping=tex-text]{GFSArtemisia.otf}
\setmathfont{latinmodern-math.otf}
\setmathfont[range=\varnothing]{Asana-Math.otf}
\setmathfont[range=\int]{latinmodern-math.otf}
\begin{document}
\lstset
{
aboveskip=1.5cm,
frame=tb,
rulecolor=\color{black},
basicstyle=\ttfamily\color{BrickRed},
columns=flexible,
morecomment=[s][\color{NavyBlue}]{/*}{*/}
}
\section*{Section Title}
\begin{lstlisting}
Source code here.
\end{lstlisting}
Text here!
\begin{lstlisting}
More source code here.
\end{lstlisting}
More text here!
\end{document}
答案1
這個要求似乎有點奇怪,但無論如何,你可以根據你想要測試的內容使谷擴展到任何維度,這裡我使用\if@nobreaktrue
“跟隨標題”的近似值,它是一個內部開關乳膠,用於避免標題後的分頁符。
\documentclass[12pt]{article}
\usepackage{parskip}
\setlength{\parindent}{0cm}
\usepackage{fontspec}
\usepackage[english,greek]{babel}
\usepackage[fleqn]{amsmath}
\usepackage{unicode-math}
\usepackage{listings}
\setmainfont
[
Ligatures=TeX,
Extension=.otf,
UprightFont=*,
BoldFont=*Bold,
ItalicFont=*It,
BoldItalicFont=*BoldIt,
Mapping=tex-text
]{GFSArtemisia}
\setsansfont[Mapping=tex-text]{GFSArtemisia.otf}
\setmathfont{latinmodern-math.otf}
\setmathfont[range=\varnothing]{Asana-Math.otf}
\setmathfont[range=\int]{latinmodern-math.otf}
\begin{document}
\makeatletter
\lstset
{
aboveskip={\if@nobreak 0pt\else 1.5cm\fi},
frame=tb,
rulecolor=\color{black},
basicstyle=\ttfamily\color{BrickRed},
columns=flexible,
morecomment=[s][\color{NavyBlue}]{/*}{*/}
}
\makeatother
\section*{Section Title}
\begin{lstlisting}
Source code here.
\end{lstlisting}
Text here!
\begin{lstlisting}
More source code here.
\end{lstlisting}
More text here!
\end{document}