3

I'm using docutils to generate LaTeX files, with custom roles for using latex specific commands in the rst files.

My problem is, that docutils automatically escapes underscores in the parameters of the custom commands. E.g.

:citep:`test_2014`

becomes (after expanding the internal macros)

\citep{test\_2014}

This generates an error.

I want to avoid patching docutils. Is there a way to 'unescape' the underscore in latex? Also, is there a general way to do this for other escaped characters?

  • \edef\_{\string_} would work, if you also use \_ to typeset an _ make sure you have \usepackage[T1]{fontenc} – David Carlisle Oct 5 '14 at 18:50
4

You can patch \@citex (which is executed in a group) to say \def\_{_}. In this way other usages of \_ won't be affected. Doing it \AtBeginDocument is necessary, because it's there that natbib redefines \@citex.

\begin{filecontents*}{\jobname.bib}
@article{test_2014,
  author={A. Uthor},
  title={Title},
  journal={Journal},
  year={2014},
}
\end{filecontents*}
\documentclass{article}
\usepackage{etoolbox,natbib}

\makeatletter
\AtBeginDocument{%
  \pretocmd{\@citex}{\def\_{_}}{}{}%
}
\makeatother

\begin{document}
\citep{test\_2014}

\bibliographystyle{plainnat}
\bibliography{\jobname}
\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.