答案1
在这种情况下,您可以使用\vadjust
让 TeX 在\vadjust
使用的任何行之后插入一些内容。此外,我使用\@bpshack
和\@esphack
(LaTeX 内核的空间黑客),它可以抑制在行内使用宏时引起的额外空格。
此外,我使用了带星号的形式\newcommand
(因此参数不能包含明确的\par
标记,这对于这种用法来说是错误的——并且无论如何都会在内部中断\textsc
)。
\documentclass{article}
\usepackage[T1]{fontenc}
\providecommand\bigstrut{\vrule width 0pt height 3ex depth 2ex\relax}
\newcommand*\slapR[1]{\par\hfill\textsl{\bigstrut #1}\par}
\makeatletter
\newcommand*\slapRmiddle[1]
{\@bsphack\vadjust{\null\hfill\textsl{\bigstrut #1}\par}\@esphack}
\makeatother
\begin{document}
Previous paragraph ends here.
\slapR{2021/07/06}
It is not trivial to add the date-line in the middle of a paragraph\dots
although you can mimic it by choosing the right point where to put the
command \slapRmiddle{And then ducks arrived.} manually by trials.
Clearly, a bit of vertical space makes things a bit better-looking.
\end{document}
无需猜测位置,连字符仍然有效,等等。
答案2
在段落之间执行此操作很容易。在段落内执行此操作要复杂得多,因此下面的解决方案依赖于手动选择将命令放在哪里以获得好看的行(它实际上会创建另一个段落)。不过,您可以在命令后立即设置分页符……但这是另一个问题!
\documentclass{article}
\usepackage[T1]{fontenc}
\newcommand{\bigstrut}{\vrule width 0pt height 3ex depth 2ex\relax}
\newcommand{\slapR}[1]{\par\hfill\textsl{\bigstrut #1}\par}
\newcommand{\slapRmiddle}[1]{{\parfillskip=0pt\par}%
\hfill\textsl{\bigstrut #1}\par\noindent\ignorespaces}
\begin{document}
Previous paragraph ends here.
\slapR{2021/07/06}
It is not trivial to add the date-line in the middle of a paragraph\dots
although you can mimic it by choosing the right point where to put the
command \slapRmiddle{And then ducks arrived.} manually by trials.
Clearly, a bit of vertical space makes things a bit better-looking.
\end{document}
...正如您所见,插入页上方的行间距很大,因此我建议不要将日期放在段落中间。