是否可以可靠地将段落的最后一个单词右对齐?
\hfill
大多数时候使用的作品,看起来像
Left stuff Right stuff.
但如果后面的其余部分\hfill
不适合该行,则失败。在这种情况下,结果看起来像
Left stuff. Bla bla bla bla bla bla bla bla bla bla
bla bla bla bla bla bla bla bla bla bla bla
Right stuff.
但我想得到
Left stuff. Bla bla bla bla bla bla bla bla bla bla
bla bla bla bla bla bla bla bla bla bla bla
Right stuff.
实际示例文档
\documentclass[12pt]{article}
\begin{document}
\noindent\textbf{In real document, I would use \texttt{\string\hfill} instead of \texttt{\string\hrulefill}. The latter just demonstrates it better in an example document.}
.
\noindent\textbf{The command \texttt{\string\hfill} is useful for aligning short text to left and right easily.}
\noindent Bla bla.
\hrulefill Yada yada.
.
\noindent\textbf{If the parts get too long, they are not correctly aligned.}
\noindent Bla Bla Bla Bla Bla Bla Bla Bla Bla Bla Bla.
\hrulefill Yada Yada Yada Yada Yada Yada Yada Yada Yada.
.
\noindent\textbf{Boxes don't help.}
\noindent \hbox{Bla Bla Bla Bla Bla Bla Bla Bla Bla Bla Bla.}
\hrulefill \hbox{Yada Yada Yada Yada Yada Yada Yada Yada Yada.}
.
\noindent\textbf{What I am actually trying to do.}
\noindent Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla
\hrulefill (statement:1)
.
\noindent Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla Bla Bla
\hrulefill (statement:1)
.
\noindent\textbf{An approximation of what it should look like, if the last line before ``(statement:1)'' is full.}
\noindent Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla Bla Bla
\\.\hrulefill (statement:1)
\end{document}
答案1
有一个很好的例子TeXbook执行此操作。请参阅第 14 章,第 106 页。
\documentclass{article}
\def\signed #1 (#2){{\unskip\nobreak\hfil\penalty50
\hskip2em\hbox{}\nobreak\hfil\sl#1\/ \rm(#2)
\parfillskip=0pt \finalhyphendemerits=0 \par}}
\begin{document}
\hsize 9cm
This is a case where the name and address fit in nicely with the review.
\signed A. Reviewer (Ann Arbor, Mich.)
But sometimes an extra line must be added. \signed N. Bourbaki (Paris)
\end{document}
你可以让它更符合 LaTeX 的标准:
\documentclass{article}
\newcommand{\signed}[1]{%
{\unskip\nobreak\hfil\penalty50
\hskip2em\hbox{}\nobreak\hfil#1
\parfillskip=0pt \finalhyphendemerits=0 \par}}
\begin{document}
\hsize 9cm
This is a case where the name and address fit in nicely with the review.
\signed{\emph{A. Reviewer} (Ann Arbor, Mich.)}
But sometimes an extra line must be added. \signed{\emph{N. Bourbaki} (Paris)}
\end{document}
以及它如何工作的解释TeXbook:
如果在 处发生换行
\penalty50
,将会消失,并且在行首会出现一个\hskip2em
空行,然后是粘连。这样会产生两行,其不良度为零;其中第一行的惩罚为。但是,如果 在 处没有发生换行,则评论和名称之间会有粘连;这样会产生一行不良度为零的行。TEX 会尝试这两种方案,看看哪一种方案导致的总缺点最少。如果可行,通常会优先选择一行解决方案。\hbox
\hfil
50
\penalty50
2 em plus 2 fil