[Tex/LaTex] Horizontal Spacing: double \hspace removal

spacing

\addvspace can be used to create commands which can be stacked together without creating unnecessary padding. This is useful for creating commands which start and end with \addvspace so that they can be placed adjacent to each other without using up twice as much padding space as they need. Does anyone know of a similar command for horizontal space? I have a number of commands of the form

\newcommand{\fooAnd}{\hspace{1ex}\texttt{and}\hspace{1ex}}

that I would like to string together, but I only want them separated from each other by 1ex each. (And I want to use commands in the interim that don't have \hspace in them, so just cutting the space down to 0.5ex is not a solution.) Any thoughts?

Best Answer

I don't know about an \addvspace version for horizontal spacing but in your case you could add an \unskip at the begin of the macro to remove any prior \hspace or other skips caused by normal spaces:

\newcommand{\fooAnd}{\unskip\hspace{1ex}\texttt{and}\hspace{1ex}}

Example:

\documentclass{article}

\newcommand{\oldfooAnd}{\hspace{1ex}\texttt{and}\hspace{1ex}}
\newcommand{\fooAnd}{\unskip\hspace{1ex}\texttt{and}\hspace{1ex}}

\begin{document}

Text \oldfooAnd\oldfooAnd\oldfooAnd text

Text \fooAnd\fooAnd\fooAnd text

\end{document}

Compare the results:

Result