マクロがスペースの有無に従うようにする

マクロがスペースの有無に従うようにする

次のような簡単なマクロを作成しました:

\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}

関連情報