21

When adding citations in the body of a document I'm used to write

... in some interesting paper~\cite{interesting}.

So that (if I'm using numbered references) the word “paper” and the citation number stick together and do not accidentally end up in two different lines.

It seems, however, that the \citet command from natbib (which should produce something like “Authors [4]”), does not use a ~ between the names of the authors and the citation number so that, on a few rare occasions, I end up with something like

                                                  ... Authors
[4].

Which is annoying. Is there an easy way to make natbib use a non-breaking space there?

5 Answers 5

3

lockstep and Stefan Kottwitz suggest redefining \NAT@spacechar. That may have worked at one time, but I find that it no longer works now, seven years later, using natbib 2010/09/13 8.31b (PWD, AO) from TeXLive 2016. A comment from @penelope also mentions that this approach was no longer working as of mid-2014.

The following strategy does work for me as of now. Add the following to your document preamble somewhere after \usepackage{natbib}:

\bibpunct{\nolinebreak{}[}{]}{,}{n}{}{,}

The key piece of this is the \nolinebreak{}[, which discourages LaTeX from breaking a line before the [ that starts a group of bracketed citation numbers. The remaining arguments to \bibpunct merely replicate the standard punctuation for bracketed, numbered citations.

Note that the {} immediately after \nolinebreak is not really an empty argument to \nolinebreak. Rather, it simply prevents \nolinebreak from mistaking the following [ for the start of an optional argument. That being said, \nolinebreak does accept an optional argument that determines how strongly a break should be discouraged. So you could instead use something like \bibpunct{\nolinebreak[0][}{]}{,}{n}{}{,} to make the no-break directive merely a mild request, all the way up to \bibpunct{\nolinebreak[4][}{]}{,}{n}{}{,} for a strict demand. The latter is equivalent to the default treatment you get if you use the \bibpunct{\nolinebreak{}[}{]}{,}{n}{}{,} form I suggested initially.

1
  • I get an error when using this in the xltabular environment: There's no line here to end. \end{xltabular} :( Even when commenting out all tables, it seems that the proposed solution doesn't prevent line breaks within the citation when citing pages, e.g., \citep[p.~12]{example2023}. Any clue? Sep 25, 2023 at 9:35
14

Looks like \NAT@spacechar needs to be redefined:

\documentclass{article}

\usepackage[numbers]{natbib}

\makeatletter
% \def\NAT@spacechar{\ }% OLD
\def\NAT@spacechar{~}% NEW
\makeatother

\usepackage{filecontents}

\begin{filecontents}{\jobname.bib}
@misc{a01,
  author = {Author, A.},
  year = {2001},
  title = {Alpha},
}
\end{filecontents}

\begin{document}

Let's test if author and citation number end up in different lines: \citet{a01}.

\bibliographystyle{plainnat}
\bibliography{\jobname}

\end{document}
3
  • Thanks! It does the trick. I wonder why this isn't the default behavior of natbib or, at least, there is an option to more easily set this. Oct 25, 2010 at 15:49
  • 1
    I guess the default behavior was chosen with author-year-citations in mind, where allowing a line break between author and year is acceptable.
    – lockstep
    Oct 25, 2010 at 15:58
  • 3
    Hey! I realize this is an almost 4 year old question, but I just tried to solve exactly the same line-breaking \citet{} problem, and trying your suggestion did not make any difference. (I inserted the re-definition in the document preamble). Do you have any idea why that might be, or what I might try?
    – penelope
    Jun 24, 2014 at 12:48
7

Since \NAT@spacechar is used, this redefinition prevents such breaks:

\makeatletter
\renewcommand*{\NAT@spacechar}{~}
\makeatother

This could affect also places where a break could be desired. Thus, an easy way for your rare occasions would be to use just \mbox around the citation:

... in the paper \mbox{\citet{interesting}}.
3
  • 1
    Also thanks for the suggestion. However note that the \mbox wont always produce the desired result if the author name contains several words (or there are several authors), as there is nothing wrong with breaking the line between those names. Oct 25, 2010 at 15:52
  • Stefans suggestion will also prevent hyphenation of author names.
    – lockstep
    Oct 25, 2010 at 15:55
  • @Juan @lockstep: that's right! It's just intended to fix the line breaking for such occasions like in the question, where the author isn't hyphenated. Of course, such adjustment should be done at last, when the final version is being produced. This local correction avoids the global effect of changing \NAT@spacechar to be unbreakable.
    – Stefan Kottwitz
    Oct 25, 2010 at 16:37
1

@JanJones answer worked for me too when using biblatex. I applied it as follows also fixing multicitedelim

\renewcommand*{\namelabeldelim}{\nobreakspace}
\renewcommand*{\multicitedelim}{\addcomma\nobreakspace}
-1

Redefining namelabeldelim to use non-breaking space (\addnbspace) worked for me:

\DeclareDelimFormat{namelabeldelim}{\addnbspace}

You must log in to answer this question.

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