放在段落末尾的 \vspace 是一個錯誤的 LaTeX 編碼嗎?

放在段落末尾的 \vspace 是一個錯誤的 LaTeX 編碼嗎?

發表評論後@大衛卡萊爾在一個問題關於\vspace用法,我試圖找出是否、何時以及為何\vspace應避免將命令添加為段落的最後一個命令,以便添加或刪除段落之間的空格,例如:

This is the text of a paragraph here.\vspace{1cm}

This is the text of the next paragraph.

@DavidCarlisle 在我的評論中回答:

“空白行應該在 vspace 之前。在水平模式下使用 vspace 定義明確,但很奇怪,你幾乎總是想避免這種情況。”

因此,建議的“更正”(據我所知)是:

This is the text of a paragraph here.

\vspace{1cm}This is the text of the next paragraph.

但我嘗試了很多範例,但沒有找到一個會給出意外輸出的範例(按垂直間距的順序,但也是一般情況)。另外,在我的第一個 LaTeX 步驟中,我使用了這種樣式:

This is the text of a paragraph here.

\vspace{1cm}

This is the text of the next paragraph.

現在對我來說似乎很糟糕,但可以認為是我的第一個範例(我目前使用的樣式)中更好的 LaTeX 編碼。

所以,我的問題是:

  1. 由於某種原因,我應該避免第一個範例的編碼風格嗎?
  2. 有沒有什麼例子顯示我的風格會在預期的間距內失敗? (或者這只是關於程式碼風格)

微量元素:

\documentclass{article}
\usepackage{parskip}
\usepackage{tikz}
\usetikzlibrary{calc}
\usetikzlibrary{positioning}
\setlength{\parskip}{1cm plus 0cm minus 0cm}
\setlength{\parindent}{0pt}

\newcommand{\expectedVSkip}[2][2.2cm]{\begin{tikzpicture}[overlay,remember picture,baseline=0pt]\node[use as bounding box,inner sep=0,outer sep=0] at (0,0) (A){\vphantom{\texttt{p}}};\draw[->,blue] (A.south)--node[midway,right]{$#2$}($(A.south)+(0,-{#2})$);\draw[->,thin,blue] ($(A.south)+(-{#1},0)$)--($(A.south)+(0,0)$);\draw[->,thin,blue] ($(A.south)+(-{#1},-{#2})$)--($(A.south)+(0,-{#2})$);\end{tikzpicture}}

\begin{document}

This is the first paragraph that will have a space of the \verb|\parskip|$=1\;cm$ length from the following command since no \verb|\vspace|\expectedVSkip{1cm} command is added here.

This is the second paragraph that will have a space of $0.5\;cm$ from the folowing paragraph since a \verb|\vspace{-0.5cm}|\expectedVSkip{0.5cm} command is added just in its end.\vspace{-0.5cm}

This\vspace{2cm} is the third paragraph with a \verb|\vspace{2cm}|\expectedVSkip{2cm} command before the end of its first line. The paragpaph have enough text following, in order to let us discover if the \verb|\vspace| will act from the point of the first linebreak or from the end of this paragraph (Since the command is placed in its first line, the command is supposed to act just at the place that \LaTeX{} will deside to break the line and the paragraph will be an ugly broken paragraph with a strange added vertical space of exactly $2\;cm$). After this paragraph the following paragraph will be in distance of 2cm since an additional (just one) $cm$ have been added through a \verb|\vspace{1cm}|\expectedVSkip{2cm} to the \verb|\parskip|.\vspace{1cm}

This is just the fourth paragraph.


\end{document}

和 MWE 的輸出:

在此輸入影像描述

答案1

如果您\vspace在段落末尾使用它,它可能會提供與在以下垂直列表中使用它相同的視覺結果,但通過完全不同且更複雜的程式碼路徑。如果您在 vmode 中使用 vspace,它只會直接將黏合節點新增至目前垂直清單。如果在 h 模式下使用它,則垂直粘合會添加到當前水平列表中的 vadjust 節點,在換行後,該節點將遷移到當前垂直列表,並在包含 vadjust 的行之後重新插入到垂直列表中節點。

您幾乎應該始終在前面添加\vspace一個空行或\par.

實際上,這種差異不應該成為問題,因為文件中幾乎不應該有明確的垂直空間命令。如果有,通常表示該類別設定的全域間距不適合當前文檔,最好在原始程式碼中修復該問題,而不是調整每個單獨段落中的間距。

相關內容