Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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

share|improve this question

migrated from stackoverflow.com Apr 9 '11 at 20:21

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

2 Answers 2

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

share|improve this answer

This is an adaptation of \addvspace:

\documentclass[a4paper]{article}

\makeatletter
\def\@xaddhskip{%
  \ifdim\lastskip<\@tempskipb
    \unskip
    \hskip\@tempskipb
  \else
    \ifdim\@tempskipb<\z@
      \ifdim\lastskip<\z@
      \else
        \advance\@tempskipb\lastskip
        \unskip
        \hskip \@tempskipb
      \fi
    \fi
  \fi}
\def\addhspace#1{%
  \ifhmode
    \ifdim \lastskip =\z@
      \hskip #1\relax
    \else
      \@tempskipb#1\relax
      \@xaddhskip
    \fi
  \else
    \errmessage{\noexpand\addhspace not in horizontal mode}%
  \fi}
\makeatother

\begin{document}

\addhspace{2cm}

x\addhspace{1cm}\addhspace{2cm}x

x\addhspace{2cm}x
\end{document}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.