# Replace undefined reference ?? with custom text?

I'm using luatex and the cleverref package while writing a large document. I often only want to compile only parts of it, but I'd like my output to be a little nicer when I only compile a single chapter.

Instead of an undefined reference being replaced with ??, I would like to insert my own custom text.

Currently my solution is to define new commands \ucref and \ucite and use them instead of \cite and \cref for any cross-chapter reference. When I compile only a specific chapter I define these commands to be:

\newif\ifpublishversion{}
\publishversionfalse{}
%\publishversiontrue{}

\ifpublishversion{}
\newcommand{\ucref}[1]{\color{red} ref{(#1)} \color{black}}
\newcommand{\UCref}[1]{\color{red} ref{(#1)} \color{black}}
\newcommand{\ucite}[1]{[\color{green}#1\color{black}]}
\else
\newcommand{\ucref}[1]{\cref{#1}}
\newcommand{\UCref}[1]{\Cref{#1}}
\newcommand{\ucite}[1]{\cite{#1}}
\fi


And then when I compile the whole document I change them to point to \cref and \cite to get default behavior.

It would be nice if I could just use \cref and \cite everywhere, but when a reference is undefined have latex spit out a warning and then insert this custom "nice" text instead of a ?? double question mark.

Is there a way to write something like ...

% PSEUDOCODE
% Do ways to do something like this exist?
\setundefinedreftext[1]{\ucref{#1}}
\setundefinedcitetext[1]{\ucite{#1}}

• You could use \include to input your chapters. allowing you to typeset just one chapter while making \ref etc still make the correct output is the whole point of the include mechanism. – David Carlisle Mar 20 '16 at 17:40
• I use \input. Is that so much different than include in this circumstance? Also I don't see how include really helps solve my problem. I only have a medium level understanding of the latex system, so maybe I'm not seeing a subtlety. – Erotemic Mar 20 '16 at 17:48
• Yes \include is completely different and all the differences are specifically to address this issue! If you use \include then after you have done a full run to resolve all references you can use \includeonly[chapter3} and just typeset chapter3 but all numbering and cross references will work to anywhere in the full document – David Carlisle Mar 20 '16 at 18:18
• That makes sense. What if I also want to include only certain sections within a chapter? A few of my chapters are large, so they are broken down into more than one file. I guess I'm still interested in a solution to the original problem. – Erotemic Mar 20 '16 at 18:53
• you can \include sections so long as they start on a new page – David Carlisle Mar 20 '16 at 20:28

As I mentioned in the comments, it is impossible to suggest how to patch your \cite command because we don't know how you are 'doing' your bibliography stuff. But the cref stuff can be fairly easy (depending on what parts of the package you are using). For example:

\documentclass{article}
\parindent 0pt
\usepackage[T1]{fontenc}
\usepackage{etoolbox, xcolor, cleveref}

\newcommand*{\undeffmt}[1]{\textcolor{red}{ref (#1)}}%
\newcommand*{\patchsuccess}[1]{\typeout{Etoolbox patch: Success for \string#1}}
\newcommand*{\patchfailure}[1]{\typeout{Etoolbox patch: Failure for \string#1}}

\makeatletter

\patchcmd{\@setcref}%
{\nfss@text{\reset@font\bfseries ??}}
{\nfss@text{\reset@font\undeffmt{#1}}}
{\patchsuccess{\@setcref}}
{\patchfailure{\@setcref}}

\patchcmd{\@setcrefrange}%
{\nfss@text{\reset@font\bfseries ??}}%
{\nfss@text{\reset@font\undeffmt{#1}}}
{\patchsuccess{\@setcrefrange}}
{\patchfailure{\@setcrefrange}}
\patchcmd{\@setcrefrange}%
{\nfss@text{\reset@font\bfseries ??}}%
{\nfss@text{\reset@font\undeffmt{#2}}}
{\patchsuccess{\@setcrefrange}}
{\patchfailure{\@setcrefrange}}
\patchcmd{\@setcrefrange}%
{\nfss@text{\reset@font\bfseries ??}}%
{\nfss@text{\reset@font\undeffmt{#1}}}
{\patchsuccess{\@setcrefrange}}
{\patchfailure{\@setcrefrange}}
\patchcmd{\@setcrefrange}%
{\nfss@text{\reset@font\bfseries ??}}%
{\nfss@text{\reset@font\undeffmt{#2}}}
{\patchsuccess{\@setcrefrange}}
{\patchfailure{\@setcrefrange}}

\patchcmd{\@setnamecref}%
{\nfss@text{\reset@font\bfseries ??}}%
{\nfss@text{\reset@font\undeffmt{#1}}}
{\patchsuccess{\@setnamecref}}
{\patchfailure{\@setnamecref}}

\patchcmd{\@setcpageref}%
{\nfss@text{\reset@font\bfseries ??}}%
{\nfss@text{\reset@font\undeffmt{#1}}}
{\patchsuccess{\@setcpageref}}
{\patchfailure{\@setcpageref}}

\patchcmd{\@setcpagerefrange}%
{\nfss@text{\reset@font\bfseries ??}}%
{\nfss@text{\reset@font\undeffmt{#1}}}
{\patchsuccess{\@setcpagerefrange}}
{\patchfailure{\@setcpagerefrange}}
\patchcmd{\@setcpagerefrange}%
{\nfss@text{\reset@font\bfseries ??}}%
{\nfss@text{\reset@font\undeffmt{#2}}}
{\patchsuccess{\@setcpagerefrange}}
{\patchfailure{\@setcpagerefrange}}
\patchcmd{\@setcpagerefrange}%
{\nfss@text{\reset@font\bfseries ??}}%
{\nfss@text{\reset@font\undeffmt{#1}}}
{\patchsuccess{\@setcpagerefrange}}
{\patchfailure{\@setcpagerefrange}}
\patchcmd{\@setcpagerefrange}%
{\nfss@text{\reset@font\bfseries ??}}%
{\nfss@text{\reset@font\undeffmt{#2}}}
{\patchsuccess{\@setcpagerefrange}}
{\patchfailure{\@setcpagerefrange}}

\makeatother

\begin{document}

\section{One}

defined cref:   \cref{aaa}   \par
undefined cref: \cref{undef} \par
\bigskip

defined crefrange:        \crefrange{aaa}{ccc} \par
1st undefined crefrange:  \crefrange{und}{ccc} \par
2nd undefined crefrange:  \crefrange{aaa}{und} \par
both undefined crefrange: \crefrange{un1}{un2} \par
\bigskip

defined cpageref:   \cpageref{aaa} \par
undefined cpageref: \cpageref{bbb} \par
\bigskip

defined cpagerefrange:        \cpagerefrange{aaa}{ccc} \par
1st undefined cpagerefrange:  \cpagerefrange{und}{ccc} \par
2nd undefined cpagerefrange:  \cpagerefrange{aaa}{und} \par
both undefined cpagerefrange: \cpagerefrange{unf1}{un2} \par
\bigskip

defined nameref:   \namecref{aaa} \par
undefined nameref: \namecref{und} \par

\newpage
\section{Two}

\label{aaa}%
% \label{bbb}%
It would be nice if I could just use \Cref{ccc} and cite everywhere,
but when a reference is undefined have latex spit out a warning and
then insert this custom nice'' (e.g., \Cref{bbb}) text instead of a
??  double question mark.

\newpage
\section{Three}
\label{ccc}

\end{document}


As you can see, it is set up so that you define your desired string in the definition for \undeffmt. (I tried to keep it similar to your original example, but I'd probably choose something different if it were up to me.)

Finally, if this does not suit your needs, this is most likely because a question of this sort really needs a 'minimal working example' so others can properly see what your implicit choices and assumptions are.

• Thank you. This works beautifully. As for \cite, I didn't know there were different versions. I'm using hyperref and ieee as my style. I'm not sure if that is how I'm 'doing' my bib stuff, but those are the things I know that definitely pertain to it. However, bib stuff isn't nearly as important as patching the cref command. My citations are all in Zotero, so it is rare that I don't have citation defined. – Erotemic Mar 21 '16 at 14:34
• Glad the cleveref part works. As for the \cite stuff, I'd really need to see a minimal example so I have somethingn to work off of. I never use cleveref or IEEE stuff, so I don't have an intuitive sense of what your use case looks like. (Though I agree that unused \cite refs are less likely than \refs to an unspecified \label.) – jon Mar 21 '16 at 14:58