How to add some horizontal space unless it is the end of the line?

This question isn't really about removing spaces at the end of a line, but about removing them at the end of a paragraph. All skips at the end of a line are already ignored automatically.

A possible solution would be to define \customhspace as follows:

\newcommand*\customhspace[1]{\unskip\hspace{#1}\ignorespaces}

This doesn't quite behave like an ordinary \hspace in the middle of a line, however, since it removes a space preceding or following it (which includes ordinary spaces). Whether this is an advantage or a disadvantage depends on how you intend to use this macro.

Demonstration:

Here is a little sample document that demonstrates how this works. Note that it matters whether an \hspace{...} is surrounded by spaces, while it does not for \customhspace.

\documentclass{article}

\newcommand*\customhspace[1]{\unskip\hspace{#1}\ignorespaces}

\begin{document}

AAA \hspace{2em} BBB

AAA \hspace{2em}BBB

AAA\hspace{2em} BBB

AAA\hspace{2em}BBB

AAA \customhspace{2em} BBB

\hfill AAA \hspace{2em}

\hfill AAA\hspace{2em}% % <- see explanation below

\hfill AAA\customhspace{2em}

\hfill AAA

\end{document}

enter image description here

Explanation:

TeX removes the last item in a paragraph if it is glue (a skip/space), but only one piece of glue will be removed in this way. This means that if you have multiple consecutive spaces at the end of a paragraph, all but the last one will still be there.

A paragraph produced by \hfill AAA \hspace{2pt}, followed by a newline, actually ends with three pieces of glue:

  • The explicit space character preceding \hspace.
  • The \hspace{2pt} itself
  • The newline also produces a space.

If you remove the preceding space and comment out the newline (see What is the use of percent signs (%) at the end of lines?), \hspace{2pt} will be the last item in the paragraph and will be removed. The following paragraph will not have a space at the end:

\hfill AAA\hspace{2pt}%

You don't want to do this every time, of course. In the definition for \customhspace above, I instead included an \ignorespaces to make TeX ignore skips directly following this command, and \unskip to remove the preceding space, if there is one.


This would seem to do it except in one regard which may or may not be of concern to the OP. Spaces following \customhspace{}, if they exist, are gobbled. And I only mention this exception because the OP, in providing code snippets, had spaces surrounding the invocation of \customhspace. In general, I would not be adding extra spaces around an \hspace type command.

\documentclass{article}
\makeatletter
\newcommand\customhspace[1]{\@ifnextchar\par{}{\hspace{#1}}}
\makeatother
\begin{document}
    AAA\customhspace{2pt}BBB no extra spaces

    AAA\hspace{2pt}BBB

    \smallskip

    AAA \customhspace{2pt} BBB spaces before/after

    AAA \hspace{2pt} BBB

    \smallskip

    AAA\customhspace{2pt} BBB space after

    AAA\hspace{2pt} BBB

    \smallskip

    AAA \customhspace{2pt}BBB space before

    AAA \hspace{2pt}BBB

    \smallskip

    no space before\hfill AAA\customhspace{2pt} 

    \hfill AAA

    \smallskip

    space before\hfill AAA \customhspace{2pt} 

    \hfill AAA
\end{document}

enter image description here

Tags:

Spacing