# Can you employ \unskip to automatically place a non-breaking space before \cite or \ref?

It is recommended to place a non-breaking space before \cite, \ref, inline math and perhaps at few other locations.

The question is whether you can design an improved version of \cite (say) which would gobble all the preceding spaces, insert a non-breaking space, and then proceed to calling the original \cite?

-
@Yossi: Why did you tag your question with {tex-core}? –  lockstep Feb 20 '11 at 23:50
Oh, because I thought it had to do with the way TeX consumes characters. If it ain't so, I will gladly fix the tagging. –  Yossi Gil Feb 21 '11 at 0:09
Well, I tried fixing that, but could not change the tags. Perhaps the high ranking members of this friendly and wise community have more privileges than I do? –  Yossi Gil Feb 21 '11 at 6:26
@Yossi: Fixed for you. –  lockstep Feb 21 '11 at 6:43
@Yossi: And once you have 500 rep (shouldn't be too long now), you may retag questions yourself. –  lockstep Feb 21 '11 at 6:49
show 1 more comment

Yes you can. The varioref package does this already. Have a look at the source code of it. For example:

\@vpageref
More parsing. . .
596 \def\@vpageref[#1]{\@ifnextchar[%
The default for the second optional argument is a space which is prexed by
597 {\@@vpageref{#1}}{\@@vpageref{#1}[\unskip\vref@space]}}


You should be able to define:

\def\mycite#1{\unskip~\cite{#1}}


or save \cite away using \let:

\let\origcite\cite
\def\cite#1{\unskip~\origcite{#1}}

-
Neat! Thanks. I did not think this would be as easy, since I thought TeX has consumed the preceding spaces already. I think this would be useful to many. I will surely start using this right away. –  Yossi Gil Feb 21 '11 at 0:15
@YossiGil: Well TeX has consumed the space indeed, but stored it as a skip which can be removed using the \unskip. –  Martin Scharrer Jan 26 '12 at 13:41

Combining Martin's answer with this redefinition of \cite answer, I was able to use the following in my preamble, without needing the varioref package:

\let\originalcite\cite
\renewcommand{\cite}[1]{\unskip~\originalcite{#1}}


A good explanation of what's going on with \let and \renewcommand was found in the Make your own style section of a LaTeX tutorial:

We interfere with LaTeX using the \let command which defines a pointer to the current definition of another command; thus

\let\LaTeXtitle\title


defines \LaTeXtitle to point to the original LaTeX definition of \title. Follow this with

\renewcommand{\title}[1]{\LaTeXtitle{\color{magenta}\textsf{#1}}}


to define a new version of \title that colours it magenta and puts it into a sans serif font. Neat!

-
Another way is to say \robustify\cite\preto\cite{\unskip~} after having loaded the etoolbox package; \robustify is needed because \cite is defined with \DeclareRobustCommand. Otherwise the more obscure \expandafter\preto\csname cite \endcsname{\unskip~} is needed (there's a space between cite and \endcsname). –  egreg Jun 4 '11 at 16:11
Of course one has to be very careful not to start a sentence with \cite, so I prefer always to put a ~ before \cite so that I can delete it in troublesome cases; and the ~ is probably wrong when an author-date system is used. –  egreg Jun 4 '11 at 17:06
Done this way: \let\originalcite\cite \def\cite{\unskip~\originalcite}, it works with the optional argument too, e.g., \cite[p. 10]{Foo}. Want to edit your answer? –  andrew Jun 20 '12 at 22:33