9

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}
2
  • You could put the whole \cite command in an \mbox.
    – Alan Munn
    Commented Aug 18, 2015 at 2:10
  • I would prefer a solution that involves editing only the preamble. wccounts 503 occurances of \cite in my document's sources.
    – Dohn Joe
    Commented Aug 18, 2015 at 6:25

1 Answer 1

5

Both approaches described here are taken from Stefan Kottwitz's answer to Non-breaking space in \citet using natbib?.

The \mbox approach

You can redefine \cite to be wrapped in a \mbox with

\let\oldcite\cite
\renewcommand{\cite}[1]{\mbox{\oldcite{#1}}}

Or, even better, do so with the letltxmacro package (See When to use \LetLtxMacro?)

\usepackage{letltxmacro}
\LetLtxMacro{\oldcite}{\cite}
\renewcommand{\cite}[1]{\mbox{\oldcite{#1}}}

MWE

\documentclass{article}

\usepackage[numbers,sort]{natbib}

\usepackage{letltxmacro}
\LetLtxMacro{\oldcite}{\cite}
\renewcommand{\cite}[1]{\mbox{\oldcite{#1}}}

\begin{document}

A really really really really really really really really long sentence, foo \cite{lamport94,foo,bar}.

A shorter 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}

minimal working example screenshot

The \NAT@spacechar approach

Instead 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 to

\renewcommand{\NAT@separator}{\NAT@sep\nolinebreak}

MWE

\documentclass{article}

\usepackage[numbers,sort]{natbib}

\makeatletter
\renewcommand{\NAT@separator}{\NAT@sep\nolinebreak}
\makeatother

\begin{document}

A really really really really really really really really long sentence, foo \cite{lamport94,foo,bar}.

A shorter sentence, foo \cite{lamport94,foo,bar}.

\begin{thebibliography}{9}

\bibitem{lamport94}
  Leslie Lamport,
  \emph{\LaTeX: a document preparation system},
gg  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}

The output is the same as in the \mbox approach.

4
  • Thanks for this extensive answer. Both approaches work well.
    – Dohn Joe
    Commented Aug 18, 2015 at 16:43
  • Nice answer, I was just coming back to say the same thing, but you've done it perfectly.
    – Alan Munn
    Commented Aug 18, 2015 at 18:47
  • For the \mbox approach it is also helpful to redefine the \cite command to work also with addtional arguments, so you can use something like \cite[page 15]{lamport94}. So better use \renewcommand{\cite}[2][]{\mbox{\oldcite[#1]{#2}}}
    – Mathias
    Commented Jan 9, 2023 at 12:49
  • For me the \mbox approach did not work well. I am using natbib package with abbrvnat bibliography style in a double column article. And when I cite e.g. 3 papers in a multi citation, they end up sticking out of the column.
    – Paloha
    Commented Apr 4, 2023 at 0:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .