5

I'm having a problem with a command with empty output adding an extra space.

I have a large document that needed a number of corrections. To highlight the text added and removed for easier review, I decided to define to commands \xcut{} and \xadd{} that I could later easily redefine to produce the final version.

The problem is that the empty \xcut{} is now adding an extra space to the output. I'm sure some will tell me there was a better way to do it, but I've already made all the changes this way and would prefer not to go through the whole thing again. Thanks.

Example:

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

\def\Corrections{2}     % Highlight or Final - 1 or 2

\ifnum\Corrections=1    % 1 - Highlight
    \newcommand{\xcut}[1]{{\color{red}{\sout{#1}}}}%
    \newcommand{\xadd}[1]{{\color{green}{\uline{#1}}}}%
\fi

\ifnum\Corrections=2    % 2 - Final
    \newcommand{\xcut}[1]{}%
    \newcommand{\xadd}[1]{#1}%
\fi

\begin{document}

CUT: Shall I compare thee \xcut{thee} to a summer's day?

ADD: Thou art more \xadd{lovely} and more temperate:

BOTH: Rough winds do \xcut{snake} \xadd{shake} the darling buds of May,

\end{document}
  • It's not producing a space, you have a space before and after the command \xcut{thee} so since the command disappears you have two consecutive spaces. – Manuel Oct 22 '14 at 16:43
  • LaTeX usually squishes multiple space characters together, but not in this case because of the empty command in the middle. – El Bee Oct 22 '14 at 16:56
  • No, LaTeX squishes multiple space characters toguether when tokenizing, there you have a thing in the middle (that just happens to disappear after one expansion, but that's another story), so the two spaces are separated. – Manuel Oct 22 '14 at 16:58
5

Without putting much thought into it the first to try would be

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

The standard way to make an “invisible” command is to use \@bsphack at its beginning and \@esphack at the end.

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

\def\Corrections{2}     % Highlight or Final - 1 or 2

\ifnum\Corrections=1    % 1 - Highlight
    \newcommand{\xcut}[1]{{\color{red}{\sout{#1}}}}%
    \newcommand{\xadd}[1]{{\color{green}{\uline{#1}}}}%
\fi

\makeatletter
\ifnum\Corrections=2    % 2 - Final
    \newcommand{\xcut}[1]{\@bsphack\@esphack}%
    \newcommand{\xadd}[1]{#1}%
\fi

\begin{document}

CUT: Shall I compare thee \xcut{thee} to a summer's day?

ADD: Thou art more \xadd{lovely} and more temperate:

BOTH: Rough winds do \xcut{snake} \xadd{shake} the darling buds of May,

\end{document}

enter image description here

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.