# Patching commands to remove surrounding whitespace

I'm using the todonotes package to add little margin notes to my text, however writing

Hello\todo{test} World!


removes the interword space as is already pointed out in this question. So I tried

\pretocmd{\todo}{\@bsphack}{}{}
\apptocmd{\todo}{\@esphack}{}{}


and this adds the interword space but simply outputs all parameters instead of passing them to \todo.

Full MWE:

\documentclass{article}

\usepackage{etoolbox}
\usepackage{todonotes}

\makeatletter
\pretocmd{\todo}{\@bsphack}{}{}
\apptocmd{\todo}{\@esphack}{}{}
\makeatother

\begin{document}
Hello\todo{test} World!
\end{document}

-

Since \todo is defined with \newcommand to have an optional argument, the macro to patch is really

\\todo


(with a backslash in its name), so it's difficult to specify it: you can patch it using \csname or do it with the xpatch package:

\documentclass{article}

\usepackage{todonotes}
\usepackage{xpatch}

\makeatletter
\xpretocmd{\todo}{\@bsphack}{}{}
\xapptocmd{\todo}{\@esphack}{}{}
\makeatother

\begin{document}
Hello\todo{test} World!
\end{document}


With etoolbox you'd have to write

\expandafter\pretocmd\csname\string\todo\endcsname{\@bsphack}{}{}
\expandafter\apptocmd\csname\string\todo\endcsname{\@esphack}{}{}

-
perfect, thank you! – Jonathan Aug 25 '12 at 14:43
Out of curiosity, what do \[be]sphack do (or perhaps more appropriately, which package are they from)? – Sean Allred Mar 3 '14 at 17:41
@SeanAllred They're defined in the LaTeX kernel – egreg Mar 3 '14 at 17:41

You can achieve this easier. Since the definition of \todo simply is

\newcommand{\todo}[2][]{\@todo[#1]{#2}}%


you can just redefine it (in a slightly safer way than the original definition, see Bruno Le Floch's comment to this answer (thanks!)):

\documentclass{article}
\usepackage{todonotes}

\makeatletter
\renewcommand{\todo}[2][]{\@bsphack\@todo[{#1}]{#2}\@esphack}%
\makeatother
\begin{document}

Hello\todo{test} World!

\end{document}

-
yes, but I was hoping to solve the problem without redefining the command explicitly so that (a) I'd learn more and (b) I'm less vulnerable to future changes to the structure of the command ;) – Jonathan Aug 25 '12 at 14:24
I wouldn't worry about (b) too much. \todo obviously is a simple wrapper for the actual notes command. So I guess \todo's definition is unlikely going to change. – clemens Aug 25 '12 at 14:40
Well, the definition of \todo is currently not very good since the first argument cannot contain ]. The normal technique to include those in optional arguments is to add braces around the first argument: \todo[{...[]...}]{...}, but \@todo will see \@todo[...[]...]{...}, and the first argument of \@todo will be [...[] (delimited by the first ]), which is not as expected. A better choice would be to use \@todo[{#1}]{#2} in the definition of \todo. – Bruno Le Floch Aug 25 '12 at 18:56
@BrunoLeFloch \todo has only one option where I think that could matter: the caption option, but I agree. If one chooses to redefine \todo one should do it right. I'll edit my answer. – clemens Aug 25 '12 at 19:03