3

I have a .tex document that contains some references to non-existing labels. For example, it may have \ref*{some:string}, but no \label{some:string}. This results in printing double question-marks ?? when generating the PDF. These references used to point to a super-large appendix which I'm not including in this short version of the document. Still, I would like to be able to print the correct section numbering instead of ??.

So, I made a command that creates an empty equation that contains only the missing label and gives it the desired tag, so that I can link label strings to the correct section numbers.

\newcommand{\dummylabel}[2]{\begin{equation*}\tag{#2}\label{#1}\end{equation*}}

and at some point of my document I have a list of such \dummylabels, like so:

\dummylabel{some:string1}{A.1}
\dummylabel{some:string2}{C}
...

However, this has the side-effect of printing empty equations that are numbered (although I use the starred equations environment), which is something I don't want.

So, is there a way to hide these empty equations? Or, is there another way to create labels for those kinds of references and manually give them a tag/numbering, so that when the PDF is generated it will print that tag instead of ???

Note that these references are starred (\ref*{...}), so no hyperlink is actually created, which is good.

1
  • The packages xr and xr-hyper provide means for importing cross-referencing-data from other documents as long as these documents' .aux-files are available. – Ulrich Diez Feb 14 at 23:57
4

You need to manually define the reference, of course.

\documentclass{article}

\makeatletter
\newcommand{\dummylabel}[2]{\def\@currentlabel{#2}\label{#1}}
\makeatother

\begin{document}

\dummylabel{some:string1}{A.1}
\dummylabel{some:string2}{C}

Here are references to the dummy labels: \ref{some:string1} and \ref{some:string2}

\end{document}

enter image description here

If you load hyperref, use \ref* in order not to make links.

Explanation: \label{foo} makes foo refer to the current value of \@currentlabel; usually it is \refstepcounter that sets \@currentlabel, but one can set it to whatever one desires.

Caveat. The \dummylabel commands should go after \begin{document}, because in the preamble the .aux file is not available for writing.

You can as well place \dummylabel at the bottom, just before \end{document}.

2

I solve the same kind of problem with a hack by @Ulrike Fischer at comp.text.tex. It's the commented part on the code below. I define another command to keep the original \ref, and to have the possibility to add extra content if the label doesn't exist

\documentclass{report}

\usepackage[T1]{fontenc}
\usepackage{hyperref}
\usepackage{xparse}
\makeatletter
\NewDocumentCommand{\checkref}{mmO{\ref{#1}}}{\@ifundefined{r@#1}{#2}{#3}}
%\AtBeginDocument{\let\Oriref\ref
%   \renewcommand\ref[1]{%
%       \@ifundefined{r@#1}
%       {no label #1}
%       {ref of #1: \Oriref{#1}}}}
\makeatother

\begin{document}
    \section{one}\label{testyes}
    \ref{testyes}
    
    \ref{testno}
    
    
    \checkref{testyes}{NO}
    \checkref{testyes}{NO}[YES]
    
    \checkref{testno}{NO}[YES]
\end{document}
3
  • Thanks for the answer. I tried it, but my document wouldn't compile. It was giving an error for references that target labels with underscores, like \ref*{some_string}. – frabala Feb 5 at 14:12
  • No error with the \checkref macro. If you tried the commented part, the error comes from the two calls to #1 outside any macro. – NBur Feb 5 at 14:22
  • You can code {no label \texttt{\detokenize{#1}}}{ref of \texttt{\detokenize{#1}}: \Oriref{#1}} to avoid the error. – NBur Feb 5 at 14:37

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.