當 \obeylines 處於活動狀態時在區塊之間插入自動垂直空間

當 \obeylines 處於活動狀態時在區塊之間插入自動垂直空間

有時,當我用 LaTeX 寫一篇演講稿時,我想將其部分格式設置為\obeylines\obeyspacesallowed,以便在每一行上緊湊地表達一個連貫的思想,並且可以通過前導縮進來表達行之間的關係,如下圖所示:

One thought introduced with a coherent phrase,
    followed by a subordinate phrase,
    and a coordinate subordinate phrase.

Then a second thought introduced ...

我遇到的問題是包括連續行組(“段落”)之間的額外垂直間距,就像上面最後一行之前一樣。到目前為止,我想出的最好的臨時措施是定義管道符號,它可以添加到每個群組的末尾,如下所示:... coordinate subordinate phrase.|

重新定義程式碼如下所示,我已將其放入外部包中。

\catcode`|=\active
\def|{\smallskip}

這或多或少符合預期,但整個來源文字中的管道字元令人惱火,顯然是一種解決方法。

因此,我一直在嘗試定義一個環境來完成我想要的任務,而不需要管道字元來獲得額外的間距。這就是我到目前為止所擁有的......

\newenvironment{linewise}{%
\let\@oldpar=\par \let\par=\newpar \obeylines \let\par=\@oldpar}
{}

如果我朝這個方向前進,我該如何\newpar定義?我嘗試過使用\\,但是當它自動插入到空行末尾(\par通常插入的位置)時,TeX 會猶豫,並且它不會結束上一段(因為\active我認為字符是 ),這就是我的意思我正在嘗試做。

據我所知,問題在於兩個或多個返回字元處的段落之間的劃分發生在 TeX 的嘴中,我無法進行任何調整來區分我想要插入的垂直空間,其中有兩個返回字元或多個返回字符,以及當只有一個時我想插入的換行符(或新段落)。

或者有更好的方法來實現這一點嗎?

答案1

您不應該重新定義\par,而是重新定義 後的活動行尾\obeylines

\documentclass{article}
\newenvironment{linewise}
  {\parindent=0pt
   \obeyspaces\obeylines
   \begingroup\lccode`~=`\^^M
   \lowercase{\endgroup\def~}{\par\leavevmode}}
  {\ignorespacesafterend}

\begin{document}
\noindent X\leaders\hrule\hfill X

\begin{linewise}
One thought introduced with a coherent phrase,
    followed by a subordinate phrase,
    and a coordinate subordinate phrase.

Then a second thought introduced ...
\end{linewise}
Something after
\end{document}

在此輸入影像描述

答案2

一段時間後,我找到了一個更好的解決方案來完成我想做的事情。它藉用了 eTeX,其中有命令\obeywhitespace.我已將所需的定義(顯然有效,儘管有些看起來相當複雜)放入一個包中,我可以在需要時使用。我將在下麵包含相關的包內容。

請注意,末尾有一個名為 的命令\setblankskip,它允許調整「段落」之間的間距,如原始問題所示。

另請注意,此行為是在名為 的環境中提供的linewise。唯一剩下的煩惱是linewise環境中的最終換行符號將反映在空白輸出行中。可以透過在最後一個換行符號前面加上 來避免這種情況%

\def\makeactive#1{\catcode`#1 = \active \ignorespaces}%
\def\gobble#1{}%
\newskip\blanklineskipamount
\blanklineskipamount = -.8\baselineskip
\begingroup
   \makeactive\^^M \makeactive\ % No spaces or ^^M's from here on.
\gdef\obeywhitespace{%
\makeactive\^^M\def^^M{\par\futurelet\next\@finishobeyedreturn}%
\makeactive\ \def {\ }%
\aftergroup\@removebox%
\futurelet\next\@finishobeywhitespace%
}%
\gdef\@finishobeywhitespace{{%
\ifx\next %
\aftergroup\@obeywhitespaceloop%
\else\ifx\next^^M%
\aftergroup\gobble%
\fi\fi}}%
\gdef\@finishobeyedreturn{%
\ifx\next^^M\vskip\blanklineskipamount\fi%
\indent%
}%
\endgroup
\def\@obeywhitespaceloop#1{\futurelet\next\@finishobeywhitespace}%
\def\@removebox{%
   \setbox0 = \lastbox
   \ifdim\wd0=\parindent
     \setbox2 = \hbox{\unhbox0}%
     \ifdim\wd2=0pt
       \ignorespaces
     \else
       \box2 % Put it back: it wasn't empty.
     \fi
   \else
      \box0 % Put it back: it wasn't the right width.
   \fi
}%
\newenvironment{linewise}{\begingroup\obeywhitespace}{\endgroup}%
% By default, a blank line will result in .2\baselineskip.  This allows
% other values to be set
\RequirePackage{calc}
\newcommand{\setblankskip}[1]{\setlength{\blanklineskipamount}{-\baselineskip+#1}}

相關內容