# Horizontal Spacing: double \hspace removal?

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

-

## migrated from stackoverflow.comApr 9 '11 at 20:21

This question came from our site for professional and enthusiast programmers.

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:

-

This is an adaptation of \addvspace:

\documentclass[a4paper]{article}

\makeatletter
\ifdim\lastskip<\@tempskipb
\unskip
\hskip\@tempskipb
\else
\ifdim\@tempskipb<\z@
\ifdim\lastskip<\z@
\else
\unskip
\hskip \@tempskipb
\fi
\fi
\fi}
\ifhmode
\ifdim \lastskip =\z@
\hskip #1\relax
\else
\@tempskipb#1\relax
\fi
\else