The problem is the header in uppercase. Even without hyperref
you get warnings like
LATEX WARNING: REFERENCE `SEC:THIRDSECTION' ON PAGE 13 UNDEFINED on input line 44.
If hyperref
is loaded, then \ref
is not expandable and its argument is converted
to uppercase before it is used for getting the reference data.
There are several ways to deal with this. One way is to use a robust macro that includes \ref
and its argument. Then this command does not get expanded if used in \MakeUppercase
and the argument is not converted to uppercase:
% before \tableofcontents
\DeclareRobustCommand*{\RefSecThirdSection}{\ref{sec:thirdSection}}
\begin{document}
...
\tableofcontents
...
\chapter{Some thoughts about Section~\RefSecThirdSection}
Using e-TeX's \protected\def
instead of LaTeX's \DeclareRobustCommand
will not help,
because of the bookmarks. There \MakeUppercase
is disabled and the command is made
expandable again. Alternative is \pdfstringdefDisableCommands
:
\protected\def\RefSecThirdSection{\ref{sec:thirdSection}}
\pdfstringdefDisableCommands{%
\def\RefSecThirdSection{\ref{sec:thirdSection}}%
}
For convenience, this can be put in a macro, e.g.:
\newcommand*{\headref}[1]{%
\csname headref@#1\endcsname
}
\newcommand*{\declareheadref}[1]{%
\protected\expandafter\def\csname headref@#1\endcsname{%
\ref{#1}%
}%
\expandafter\pdfstringdefDisableCommands\expandafter{%
\expandafter\def\csname headref@#1\endcsname{%
\ref{#1}%
}%
}%
}
\declareheadref{sec:thirdSection}
\begin{document}
...
\tableofcontents
...
\chapter{Some thoughts about Section~\headref{sec:thirdSection}}
Because of the expandibility of \headref
, babel shorthands cannot be supported
in the label name.
If you do not want a link, then \getrefnumber
of package refcount
can be used.
\getrefnumber
is expandable, thus the contents of the reference is converted to
uppercase, not the label name.
\usepackage{refcount}
...
\refused{sec:thirdSection}
\chapter{Some thoughts about Section~\getrefnumber{sec:thirdSection}}
A third way is using uppercase label names in the first place:
\label{SEC:THIRDSECTION}
...
\chapter{Some thoughts about Section~\ref{SEC:THIRDSECTION}}
The general mechanism is to set an anchor some where in your document with \label{<name>}
, where <name>
can be a combination of numbers, letters, :
and -
(to be safe). These anchors must be set explicitly by the author, thats why \ref{sec:47}
doesn’t work unless it is not defined by you. After setting up an anchor you can refer to it with \ref{<name>}
(giving the corresponding counter, like chapter or section or equation or … number) or \pageref{<name>}
(giving the corresponding page number).
Some author use identifiers like sec:
, eq:
, thm:
etc. to make anchor names more human readable but this is not necessary for the mechanism to work.
packages
LaTeX’s mechanism can be extended by some packages:
hyperref
makes references click-able hyper links (and has some more features).
cleveref
finds the right names for labels and prints \ref{anchor5}
as section~5
instead of 5
, for instance.
varioref
compares the page numbers of anchor and reference and add text like on previous page
, on page 6
etc. automatically.
nameref
can print the actual title of an anchor.
MWE
\documentclass[english]{article}
% langage
\usepackage{babel}
% only for \lipsum blind text
\usepackage{lipsum}
% ref packages
\usepackage{nameref}
% folowing must be in this order
\usepackage{varioref}
\usepackage{hyperref}
\usepackage{cleveref}
\begin{document}
\section{First section}
\lipsum[1-10]
\section{My section}\label{sec:mysection}
\lipsum[1-22]
\section{References}
\begin{itemize}
\item \verb+\ref{sec:mysection}+: \ref{sec:mysection}
\item \verb+\pageref{sec:mysection}+: \pageref{sec:mysection}
\item \verb+\cref{sec:mysection}+: \cref{sec:mysection}
\item \verb+\cpageref{sec:mysection}+: \cpageref{sec:mysection}
\item \verb+\vref{sec:mysection}+: \vref{sec:mysection}% already combined with \cref
\item \verb+\vpageref{sec:mysection}+: \vpageref{sec:mysection}
\item \verb+\nameref{sec:mysection}+: \nameref{sec:mysection}
\end{itemize}
\end{document}
Best Answer
Have a look at https://www.ctan.org/pkg/xr , quote
This actually does work (i use it for supplementary material), please read the package's documentation for further guidance