I sometimes cite two or three references. When the citation is at the end of the line, latex inserts a line break. How can I prevent this from happening?
I already tried what was suggested here
Note: I use the package natbib
, if I do not use cite[nobreak]
instead of natbib
, I get the desired result.
However, I would like to continue using natbib
, if possible.
Here is a MWE (if the citation does not get broken at your machine insert random gibberish of various length):
\documentclass{article}
\usepackage[numbers,sort]{natbib}
\makeatletter
\renewcommand*{\NAT@spacechar}{~}
\makeatother
\begin{document}
A really really really really really really really really long sentence, foo \cite{lamport94,foo,bar}.
\begin{thebibliography}{9}
\bibitem{lamport94}
Leslie Lamport,
\emph{\LaTeX: a document preparation system},
Addison Wesley, Massachusetts,
2nd edition,
1994.
\bibitem{foo}
Mr. Foo,
\emph{\LaTeX: a document preparation system},
Addison Wesley, Massachusetts,
2nd edition,
1994.
\bibitem{bar}
Mr. Bar,
\emph{\LaTeX: a document preparation system},
Addison Wesley, Massachusetts,
2nd edition,
1994.
\end{thebibliography}
\end{document}
Best Answer
Both approaches described here are taken from Stefan Kottwitz's answer to Non-breaking space in \citet using natbib?.
The
\mbox
approachYou can redefine
\cite
to be wrapped in a\mbox
withOr, even better, do so with the
letltxmacro
package (See When to use \LetLtxMacro?)MWE
The
\NAT@spacechar
approachInstead of redefining
\NAT@spacechar
to an unbreakable space, we should increase the penalty in the\NAT@separator
macro (which is defined as\penalty\@m
, a lower penalty than the one applied with\nolinebreak
[\@M
]). So we should redefine\NAT@separator
toMWE
The output is the same as in the
\mbox
approach.