6

I am looking for a command \customhspace{} such that

  • AAA \customhspace{2pt} BBB is the same as AAA \hspace{2pt} BBB: it adds some space between AAA and BBB.

  • \hfill AAA \customhspace{2pt} is the same as \hfill AAA: in this case, there is no space added after AAA.

So, in a way \customhspace{} adds space only if we are not at the end of a line, "pushed" by a \hfill.

Is it possible to have such a command? If yes, how to do it?

Stephen
  • 14,890
Colas
  • 6,772
  • 4
  • 46
  • 96

2 Answers2

6

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.

Circumscribe
  • 10,856
  • Like my answer, this will not act exactly like \hspace if there are spaces present before or after the invocation of \customhspace. You, of course, have already noted this. – Steven B. Segletes Aug 30 '18 at 10:29
  • Yeah, perhaps I should have said that at the start of the answer, rather than hiding behind the explanation :). – Circumscribe Aug 30 '18 at 10:57
3

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