使宏遵循间距或缺少间距

使宏遵循间距或缺少间距

我制作了这个简单的宏:

\documentclass{ltxdoc}
\newcommand\Q{\textbf{Q}}

\begin{document}
I like to type my \Q's in bold quickly, but sometimes I also end my line with \Q. 
This doesn't have it's intended effect.
\end{document}

是否可以告诉宏检查前面的空格(或字符)并像我\textbf{Q}正常输入一样遵守它?我希望它表现正常,无论前面是点 (.) 还是空格 ( )。

答案1

尽管最近这已经成为一种禁忌,但这个xspace包确实做到了:

在此处输入图片描述

代码:

\documentclass{ltxdoc}
\usepackage{xspace}
\newcommand\Q{\textbf{Q}\xspace}

\begin{document}
I like to type my \Q's in bold quickly, but sometimes I also end my line with \Q. 
This doesn't have it's intended effect.

In a sentence \Q works now.
\end{document}

相关内容