如何将一个段落设置为特定的绝对行数?
期望的结果
情况 1:文本长度较短
剩余空间应以空行填充
案例 2:文本长度准确
没事做
情况 3:文本长度较长
下面的段落应该从指定的位置开始,就好像文本的长度正好是指定的长度一样
例子
\documentclass{article}
\begin{document}
Let's say the text should be 5 lines long
\section{Short}
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.
\vspace{2\baselineskip}
Following paragraph
\section{Long}
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.
\vspace{-1\baselineskip}
Following paragraph
\end{document}
编辑:或者更好的是,如果文本太长,则抛出错误
答案1
您可以测量段落的长度。需要进行一些调整以保持\if@nobreak
和的当前状态。\everypar
\documentclass{article}
\usepackage{environ}
\makeatletter
\NewEnviron{parlines}[1]{%
\par
\if@nobreak\def\kept@nobreak{\@nobreaktrue}\else\def\kept@nobreak{}\fi
\edef\kept@everypar{\the\everypar}%
\setbox0=\vbox{\BODY\@@par\xdef\parlinesprevgraf{\the\prevgraf}}%
\kept@nobreak
\everypar=\expandafter{\kept@everypar}%
\ifnum\parlinesprevgraf>#1\relax
ERROR, too long,\par\vspace{#1\baselineskip}%
\else
\BODY\par
\vspace{\numexpr#1-\parlinesprevgraf\relax\baselineskip}%
\fi
}
\makeatother
\begin{document}
Let's say the text should be 5 lines long
\section{Short}
\begin{parlines}{5}
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.
\end{parlines}
Following paragraph
\section{Long}
\begin{parlines}{5}
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.
\end{parlines}
Following paragraph
\end{document}