显示的是 MWE(实际上来自 M. Scharrer 的包),突出显示了我的问题。
newcommand
有时我会在句子中间调用,有时在句子末尾调用。newcommand
输出一个“句号”,如果它在句子末尾结束,我想自动避免出现两个句号。
\documentclass{article}
\usepackage{mwe}
\newcommand{\aname}{Some Co.}
\begin{document}
I enjoy working at \aname{}.
She worked at \aname{} for 5 years.
\vspace{4mm}
\blindtext
\end{document}
输出以下内容:
该 lorem 文本无关紧要......
在网上搜索后发现了这一点:如果定理没有名称,如何避免定理头规范中的双点?但我猜它“总会在最后”。
寻找“案例”类型的选择让我如何通过条件案例仅选择一种情况但对我而言这似乎有点小题大做……
也许是ifthen
包裹……?
我忘了提到这一点,它看起来很有希望,但我仍在尝试理解它。如何查看并测试下一个字符是否是数字?
答案1
您只想在没有句点后面跟着时({}
中间可能存在 )附加句点,并且如果没有句点后面跟着,那么也应该有\@
。
\documentclass{article}
\makeatletter
\DeclareRobustCommand{\appendperiod}{%
\futurelet\next\append@period
}
\newcommand\append@period{%
\if\noexpand\next\bgroup
\expandafter\@firstoftwo
\else
\expandafter\@secondoftwo
\fi
\append@period@after@gobble
\append@@period
}
\newcommand\append@@period{%
\if\noexpand\next.\else.\@\fi
}
\newcommand\append@period@after@gobble[1]{%
\if\relax\detokenize{#1}\relax
\expandafter\appendperiod
\else
.\@{#1}%
\fi
}
\makeatother
\newcommand{\aname}{Some Co\appendperiod}
\begin{document}
I enjoy working at \aname{}. Good.
I enjoy working at \aname. Good
Do I enjoy working at \aname{}?
Do I enjoy working at \aname?
She worked at Some Co.\@ for 5 years. % to check the space after the period
She worked at \aname{} for 5 years.
She used to work at \aname, but now she's retired.
\end{document}
答案2
当宏出现在未考虑的情况(似乎总是至少有一个)时,这种事情很容易出错。不过……
\documentclass{article}
\makeatletter
\newcommand\aname{Some Co\@ifnextchar{.}{}{.\ }}
\makeatother
\begin{document}
I enjoy working at \aname. I really do.
She worked at \aname for 5 years.
\end{document}
这里的想法是,当\@ifnextchar
看到句号时它什么也不做;否则它会产生一个句号后跟一个空格。