使用了 parskip 的列表上方和下方空间不均匀

使用了 parskip 的列表上方和下方空间不均匀

以下代码在列表下方创建了一个更大的空间。我需要parskip在段落之间保留空间。

\documentclass{book}
\usepackage{listings}
\usepackage{xcolor}

\setlength{\parskip}{6pt} % this is the culprit

\lstnewenvironment{codesample}
{\lstset{backgroundcolor=\color{yellow}}}
{}

\begin{document}

Then load the file into your web browser using a some URL
like this one:

\begin{codesample}
    file:///Users/username/javascript/hello.html
\end{codesample}

Open the developer tools window to see the greeting in the console.

\end{document}
    

在此处输入图片描述

答案1

使用\showoutput,例如在 之前\begin{document},你可以看到在垂直模式下设置的以下材料,围绕着你的逐字文本:

...\glue 6.0 plus 2.0 minus 2.0
...\glue 0.0
...\glue(\parskip) 0.0
...\glue(\baselineskip) 1.6556
...\hbox(8.39996+3.60004)x345.0, glue set 56.99707fil

(...)   <--- verbatim text here

...\penalty -50
...\glue 6.0 plus 2.0 minus 2.0
...\glue 0.0
...\pdfcolorstack 0 pop
...\glue(\parskip) 6.0
...\glue(\baselineskip) 1.45552

第一个粘连项具有你的 的自然长度\parskip,即6pt。然后\parskip被临时修改(如 所示\glue(\parskip) 0.0),附加一个\baselineskip的粘连项,然后是逐字材料,在它之后1.6556pt粘合物品的自然长度与您的长度相同\parskip(即,,6pt并且您可以看到您的\parskip得到恢复),最后是一个\baselineskip粘合物品1.45552pt

“before” 和 “after” 的长度略有不同,\baselineskip很可能是由于使用的字体不同:我认为“before”\baselineskip来自正文的字体,而“after”\baselineskip来自逐字文本使用的字体。

但是,这两个\baselineskips 非常接近,因此它们之间的差异并不明显。因此,如果我们取消6pt逐字文本后面的两个高粘连块中的一个,它将被上下几乎相等的垂直空间包围。这可以在您的环境中使用此定义来完成codesample(我并不声称没有更好的方法):

\lstnewenvironment{codesample}
  {\lstset{backgroundcolor=\color{yellow}}}
  {\vspace{-\parskip}}

完整代码:

\documentclass{book}
\usepackage{listings}
\usepackage{xcolor}

\setlength{\parskip}{6pt} % this is the culprit

\lstnewenvironment{codesample}
  {\lstset{backgroundcolor=\color{yellow}}}
  {\vspace{-\parskip}}

\begin{document}

Then load the file into your web browser using a some URL
like this one:

\begin{codesample}
    file:///Users/username/javascript/hello.html
\end{codesample}

Open the developer tools window to see the greeting in the console.

\end{document}

在此处输入图片描述

相关内容