我正在尝试创建一个用于文本右对齐的命令。如果文本适合一行,则必须保持在同一行上,否则它必须流到下一行但必须保持对齐(右对齐)。目前我可以使用两个单独的命令来执行这些操作,例如:
\renewcommand{\marks}[1]{\ \newline\hfill\textbf{[#1\hphantom{a}marks]}}
或者
\renewcommand{\marks}[1]{\ \hfill\textbf{[#1\hphantom{a}marks]}}
但是,我无法将它们合并。也就是说,
- 如果我使用第一个命令,那么即使同一行有空格,它也会添加一个新行。
- 如果我使用第二个命令,那么它
marks
会将左边如果同一行没有可用空间,则放在下一行。
谢谢。
答案1
下面的代码可以解决你的问题:
\renewcommand{\marks}[1]{%
\unskip\nobreak\hfil\penalty50\null\nobreak
\hfil\textbf{[#1\hphantom{a}marks]}%
\parfillskip 0pt\finalhyphendemerits0}
答案2
此实现允许\marks
将参数后面的任何文本推到新行,而不是附加到与\marks
参数相同的行。
\documentclass{article}
\renewcommand\marks[1]{\ \hfil\allowbreak\mbox{}\hfil\mbox{\bfseries #1}\break}
\begin{document}
Here is the first text. \marks{Here is the second text.} And here is the next text.
Here is the first text which is also quite longer. \marks{Here is the second text.} And here is the next text.
Here is the first text which is also quite longer.. \marks{Here is the second text.} And here is the next text.
Here is the first text which is more than quite a bit longer. \marks{Here is the second text.} And here is the next text.
\end{document}