3

How can I patch the natbib commands \citep, \citet and the like? I have tried xparse (MWE below), etoolbox (the \pretocmd and \apptocmd commands) and xpatch (the \xpretocmd and \xapptocmd commands) without any measureable success.

This is an attempt to find an automatic workaround for the error \pdfendlink ended up in different nesting level than \pdfstartlink that occurs when a link spans two pages. (Even with newest hyperref, I'm seeing the error in twocolumn format.) The intention is to wrap all occurrences of \citep...{...} and the like into an \mbox{}. Note: This is not for publishing, this is just for making sure that intermediate states of my LaTeX document in development compile cleanly. I don't want to fix things that probably disappear anyway when the document is released.

EDIT: Originally, I was thinking that this would be a very simple thing to do, open-and-shut. What makes LaTeX behave so oddly in this particular case?

Another way to solve the practical issue behind this is to use Non-breaking space in \citet using natbib? However, this doesn't work for some reason in my case (natbib + hyperref + babel; sorry, no MWE here...) Yet another way would be to restrict the link to a single word in the reference (e.g., to the year), or to add a Wikipedia-style link icon instead of marking the entire reference as clickable.

MWE (also on GitHub)

The following code gives me the error shown below:

\documentclass{scrartcl}
\pagestyle{empty}
\usepackage{xparse}
\usepackage[authoryear,round]{natbib}

\let\oldcitep=\citep
\RenewDocumentCommand{\citep}{O{} O{} m}{\oldcitep{#3}}

\NewDocumentCommand{\citex}{O{} O{} m}{\oldcitep{#3}}

\begin{document}
  \citep{SomeRef}
\end{document}

Error message

Runaway text?
\citep code {}{}{SomeRef}{SomeRef}{SomeRef}{SomeRef}{SomeRef}{SomeRef\ETC.
! TeX capacity exceeded, sorry [main memory size=5000000].
\l__xparse_args_tl ...omeRef}{SomeRef}{SomeRef}{So
                                                  meRef}{SomeRef}{SomeRef}{S...
l.12   \citep{SomeRef}

What baffles me most: When I replace \citep by \citex in the document body, the document compiles.

3
  • I haven't done any testing, but does the hyperlink stuff also occur with the modern package biblatex? For your own documents, biblatex is far better and flexible. For publication, the chance is high that they will simply ignore your fix.
    – Johannes_B
    Mar 29, 2015 at 9:13
  • citep can take a starred form, internally there is some stuff going on, very different macros for the open ( and the closing ) and the cite in between.
    – Johannes_B
    Mar 29, 2015 at 9:18
  • @Johannes_B: I still need to take a closer look at BibLaTeX. -- This is more for "continuous integration" (aka compiling "unstable" states of the document for preview). During development of a document, I really don't mind somewhat sloppy typesetting as long as the document compiles properly, which it doesn't in this case. I'll add some clarification and add another more precise question about non-breaking references.
    – krlmlr
    Mar 29, 2015 at 9:46

1 Answer 1

2

You have to use \LetLtxMacro: the documentation of the letltxmacro package tells why. See also When to use \LetLtxMacro?

\documentclass{scrartcl}
\pagestyle{empty}
\usepackage{xparse}
\usepackage[authoryear,round]{natbib}
\usepackage{letltxmacro}

\LetLtxMacro\oldcitep\citep
\RenewDocumentCommand{\citep}{O{} O{} m}{\oldcitep{#3}}

\NewDocumentCommand{\citex}{O{} O{} m}{\oldcitep{#3}}

\begin{document}
  \citep{SomeRef}
\end{document}
0

You must log in to answer this question.

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