6

Can anyone explain how to get rid of spaces that are automatically added around arguments provided in curly braces in the main part of a text? Here's a minimal example:

\documentclass{article}
\usepackage[normalem]{ulem}

\newcommand{\oldtext}[1]{}
% \newcommand{\oldtext}[1]{\sout{#1}}

\begin{document}
There is absolute no doubt {} {} {} {} that the difference between $x=3$ and $x=4$ shall 
not be ignored. After all, $3$ and $4$ are different numbers. 
So why in the world would \oldtext{anybody} \oldtext{somebody} anyone argue for that?

\end{document}

The result is:

Example output

As it can be seen, the repeated occurrences of "{}" add spaces. The "\oldtext" macro shows why this can be problematic in practice. It is used here to denote old text for which it should be possible to be made visible when needed later (by exchanging the definition of the oldtext macro). Of course, the last line of the main part of the text could be written as:

So why in the world would\oldtext{anybody}\oldtext{somebody} anyone argue for that?

but this is hard to read in the code, confuses the version control software, and would require to add a space inside the macro oldtext to avoid the old texts being stitched together, which is kind-of-ugly. Also, spaces before or after \oldtext can easily be overlooked during proof-reading of a document.

Ideally, I would like to replace the command "\newcommand{\oldtext}[1]{}" by some expression that behaves like "\relax" rather than "{\relax}".

  • The design is actually that {} should provide for extra space, if not prevented by {\ignorespaces} or as in Bordaigorl's answer with \@bsphack\@esphack way. – user31729 Feb 12 '15 at 10:54
  • {} does not add any space. The spaces are the explicit spaces in your source file. – David Carlisle Feb 12 '15 at 12:32
  • @DavidCarlisle but when I simply write "no doubt <many spaces> that the" instead of "no doubt {} {} {} {} that the", the words "doubt" and "that" are correctly spaced. So while the "{}" themselves may not add the spaces, their presence adds them (to the visual output in the compiled PDF/PS file). – DCTLib Feb 12 '15 at 12:41
  • @DCTLib yes but doubt {} that has two spaces in the output for exactly the same reason that doubt ? that has two spaces. The extra space is nothing to do with {} two adjacent space characters produce a single space token in tex's scanner, and in neither case are there any adjacent spaces, while in doubt that there are two adjacent space characters which are tokenised to a single space token. – David Carlisle Feb 12 '15 at 12:56
  • @DCTLib you have to think of {} as an (empty) word, so LaTeX processes it as needing separation from the surrounding words. Does this help? In fact bla{}bla does not insert any space between the two blas – Bordaigorl Feb 12 '15 at 12:57
9

Another general answer that solves the problem:

\makeatletter
\newcommand{\oldtext}[1]{\@bsphack\@esphack}
\makeatother

while \ignorespaces always eats up all the spaces \@bsphack\@esphack check if a space is needed in some cases: when there is a space after the command but not before

hi\oldtext{bla} there.

In this case the \ignorespaces solution would give

hithere.

while the \@bsphack one would correctly insert the space between the two words:

hi there.

This is the solution used by LaTeX's macros that do not typeset stuff like \label.

6

Use \ignorespaces inside of the empty {} pair. The difference between using and not using it is shown by the two macros \oldtext and \evenoldertext

\documentclass{article}

\usepackage[normalem]{ulem}


\newcommand{\oldtext}[1]{\ignorespaces}

\newcommand{\evenoldertext}[1]{}


% \newcommand{\oldtext}[1]{\sout{#1}}

\begin{document}
There is absolute no doubt {} {} {} {} that the difference between $x=3$ and $x=4$ shall 
not be ignored. After all, $3$ and $4$ are different numbers. 
So why in the world would \oldtext{anybody} \oldtext{somebody} anyone \evenoldertext{Hello} argue for that?

\end{document}

enter image description here

  • Thanks. Will mark as solved if nobody points of a drawback of this approach in the next 24 hours or so. – DCTLib Feb 12 '15 at 10:53
  • @DCTLib in my solution I give a different answer and its advantages over \ignorespaces – Bordaigorl Feb 12 '15 at 11:03
  • @Bordaigorl I've seen it - it does require the definition of oldtext to be inside a custom package, however (due to the @s in the code), unless I am mistaken here. – DCTLib Feb 12 '15 at 11:08
  • @DCTLib no that's not necessary, just use \makeatletter and makeatother as I did, it can stay in your preamble. – Bordaigorl Feb 12 '15 at 11:36
  • there's extra space showing between "anyone" and "argue". the example would be more convincing if \evenoldertext were used consistently in the second string. – barbara beeton Feb 12 '15 at 13:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.