10

When a reference is undefined, LaTeX shows simply [??] which doesn't give much information. I would like it to show the label instead. This is useful when more collaborators work on the same preprint and the references are yet to be done.

I'm using neither biblatex nor natbib. I do use bibtex but it is irrelevant for this question.

MWE:

\documentclass{article}

\begin{document}

This: \cite{undef}
should look like: [{\bfseries ?? undef}]

\end{document}
2
  • There's always the showkeys package.
    – egreg
    Aug 24, 2013 at 15:12
  • @egreg I know, but that's an overkill here ;)
    – yo'
    Aug 24, 2013 at 15:33

2 Answers 2

13

The definition of \cite calls an internal \@citex that checks the citation and adds an \hbox{\reset@font\bfseries ?} for undefined citations. Changing it to call the argument to \cite instead of the question mark works fine.

\makeatletter
% latex.ltx, line 6121:
\def\@citex[#1]#2{\leavevmode
  \let\@citea\@empty
  \@cite{\@for\@citeb:=#2\do
    {\@citea\def\@citea{,\penalty\@m\ }%
     \edef\@citeb{\expandafter\@firstofone\@citeb\@empty}%
     \if@filesw\immediate\write\@auxout{\string\citation{\@citeb}}\fi
     \@ifundefined{b@\@citeb}{\hbox{\reset@font\bfseries 
     \expandafter\strip@prefix\meaning\@citeb}%
       \G@refundefinedtrue
       \@latex@warning
         {Citation `\@citeb' on page \thepage \space undefined}}%
       {\@cite@ofmt{\csname b@\@citeb\endcsname}}}}{#1}}
\makeatother

A much cleaner way to redefine \@citex is by patching it with xpatch:

\usepackage{xpatch}
\makeatletter
\xpatchcmd{\@citex}{\bfseries ?}{\bfseries\expandafter\strip@prefix\meaning\@citeb}{}{}
\makeatother

MWE:

\documentclass{article}
% \usepackage{lmodern}
\usepackage[T1]{fontenc}
\makeatletter
% latex.ltx, line 6121:
\def\@citex[#1]#2{\leavevmode
  \let\@citea\@empty
  \@cite{\@for\@citeb:=#2\do
    {\@citea\def\@citea{,\penalty\@m\ }%
     \edef\@citeb{\expandafter\@firstofone\@citeb\@empty}%
     \if@filesw\immediate\write\@auxout{\string\citation{\@citeb}}\fi
     \@ifundefined{b@\@citeb}{\hbox{\reset@font\bfseries 
     \expandafter\strip@prefix\meaning\@citeb}%
       \G@refundefinedtrue
       \@latex@warning
         {Citation `\@citeb' on page \thepage \space undefined}}%
       {\@cite@ofmt{\csname b@\@citeb\endcsname}}}}{#1}}
\makeatother
\begin{document}
\null\vfill\thispagestyle{empty}

This: \cite{undef} should look like: [{\bfseries ?? undef}]

This one \cite{article-minimal} shouldn't.

Ugly citation keys with special chars like \cite{_$^/:&!@*(-=+)} 
also should be printed.

\bibliography{xampl.bib}
\bibliographystyle{plain}
\end{document}

enter image description here

This answer is sponsored by texdef and (very!) special comments by tohecz and Gonzalo Medina :)

2
  • Works great, unless you have _ in citation keys. Putting \expandafter\strip@prefix\meaning\@citeb instead of #2 quite works :)
    – yo'
    Aug 24, 2013 at 15:44
  • @tohecz I felt something was wrong, but couldn't tell what... fixed it!
    – henrique
    Aug 26, 2013 at 0:25
3

biblatex provides this functionality by default:

enter image description here

\documentclass{article}
\usepackage{biblatex}% http://ctan.org/pkg/biblatex
\begin{document}
See~\cite{author}.
\end{document}

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.