Label, Ref and Hyperref in LaTeX
preface
We will introduce \label-\ref commands first, Then move into Hyperref.
\p@COUNTER and \refstepcounter
What is these commands used for ? see : Use of \p@COUNTER. For a mwe, see below:
1 | |
Lets see what happens:
- Command
\refstepcounter {<counter>}in\@sectcommands which construct all\section-like command, which will update a macro named\@currentlabelinto something in form of :\p@COUNTER\theCOUNTER. - Then the
\labelcommand will write\@currentlabelthe the.auxfile - Finally, command
\refretrives contents from.auxfile. See label-ref system in LaTeX for moore info.
Remark:
- command
\refstepcountercall the\stepcountercommand to step the COUNTER, see definition inltcounts.dtx:\def\refstepcounter#1{\stepcounter{<counter>}% ... } - thus, the
\p@in command\p@COUNTERjust menas prefix.
Then we can custom the enumerate number format by re-define counter: \p@enumii, see the original definition in LaTeX2e:
1 | |
Thus you will get 3(b)iv for item in level 3/2/4. Therefore, simple number format does NOT need additional packages like enumerate, etc. Just re-define them yourself.
An simple example about enumerate, mwe:
1 | |
The output:

hyperref insight
Background
There are 2 basic conceptions in hyperref for hyperlinking:
-
Link: Internal links and bookmarks can jump to “destination”
-
destination:
- “destination” in PDF, \pdfdest
- “anchor” in HTML, \hyper@anchor
- “target” under normal case in future, see doc
hyperref-linktarget.pdf
Links commands
The following commands are provided by all drivers to create links:
1 | |
The argument meaning are:
context: normally consists oflink,url,cite; Refer tolinkcolorandcitecoloroptions for hyperref package; If this context not defined, nothing will happens.destination name: This is self-explainary, such as the followingztargetB,ztargetBC.link text: the clickable text shown in pdf
mwe preamble
Then we make a simple explanation or mwe to each command, we assume all the preable of these mwe is:
1 | |
Example 1
Let see the first example:
1 | |
Example 2
After click the link text, you will jump to the start place of the target, in this case, it is the first \dotfill, that will be shown on the top of your screen.
1 | |
Example 3
The destinations that aready exsits:
1 | |
How to set the target ?
Targets are created automatically when \refstepcounter is used and it is well known that \@sect command will call this command. Thus the star-version section commands need some more attentions.
For package authors, there are some commands to define these Targets. You can see document hyperref-linktarget for detailed information about how to use these commands.
- \MakeLinkTarget
- \LinkTargetOff / \LinkTargetOn
- \NextLinkTarget
- \SetLinkTargetFilter
The first four commands can be used even if hyperref is not loaded. And package authors can also provide these definitions directly:
1 | |
So, what is \MakeLinkTarget and others doing ? see the Examples below. Is there any relation to code like:
1 | |
Or \theHchapter ? Why hyperref has introduced \theH<counter> ?
For that
\the<counter>is not always unique. For example, 2 different figures in different section may have the same value if\thefigurehas no section counter prefix.
When should we re-def \theH<counter> ?
Usually problems with duplicate destination names can be solved by an appropriate definition of
\theH<counter>.
Example 1: basic usage
Firstly, we see how to use command \MakeLinkTarget, the argument spec is:
1 | |
Numbers can be occur in the name of the target. The name is then stored globally1 in \@currentHref and used for example in the next \label.
After creating this target by \MakeLinkTarget, you can use command \hyper@link to link to this target, see below:
1 | |
it is recommended to stick to ASCII and to avoid spaces in target. The default target format is:
1 | |
Sometimes, users will use \phantomsection for an empty section anchor, this command will create a dummy counter named section*. Then this target generated will be:
1 | |
Example 2: \MakeLinkTarget*
Though you need to obey the basic rules about creating a target, you can change current target(stores in macro \@currentHref ) by using command \MakeLinkTarget*{<target>} sometimes. See below:
1 | |
In this case: the target name is stored by the next \label ? How to check that ? see below, when you typeset these commands:
1 | |
Then you will see a line
1 | |
in your current .aux file. That is how it stores the target !
Example 3: \NextLinkTarget
Then let see how to use \NextLinkTarget command to change the next target name, see the mwe below:
1 | |
Then the output look like:

Remark:
- This command also affects targets created by
\refstepcounter. - It does the same as the \hypersetup key next-anchor.
Example 4: \LinkTargetOn/Off
Suppresing the target locally:
- target created with
\MakeLinkTarget - target from an internal
\refstepcounter
After suppresing these targets, you can still declare a new target in this local group for future referencing. See below:
1 | |
Example 5: \SetLinkTargetFilter
When merge multi pdf into a single, destinations may come into conflict, eg. all document has section.1 destination. How to solve this ? A very simple way you may think about is that you can add a different prefixs for all destinations for different pdf file.
See below to see how to do it. If you have a pdf file named docA, hyperref sets anchor with name Doc-Start at the begin of the document. Re-def it by adding this line to your preamble:
1 | |
Then source of PDF will look like:
1 | |
For that \MakeLinkTarget uses this filter too: it would break internal link commands if it would ignore it. To stay compatible with future development the filter should not be redefined directly but be set with \SetLinkTargetFilter. The command can only be used in the preamble:
1 | |
Example 6: \HyperDestLabelReplace - todo
This command may be related to:
\labelcommand.auxfile wrirting- command
\HyperDestRename{⟨destination⟩}{⟨newname⟩}
Patches
To make hyper link work properly, a lot of commands have been re-defined. For Example hyperref patched:
-
sectioning commands: @sect, @ssect, @chapter, @schapter, @part, @spart. Targets are needed for bookmarks and the table of contents, so
\@currentHrefshould get the correct meaning before\addcontentslineis used. You can suppress these patches by define a command\hyper@nopatch@sectioningbefore loading hyperref, see below:1
2
3
4\makeatletter
\def\hyper@nopatch@sectioning{}
\makeatother
\usepackage{hyperref} -
footnotes: …
-
amsmath tags: hyperref redefines two internal commands of amsmath related the
\tagcommand
to add an anchor. -
theorems: hyperref patches command
\@thm, that why your own thm environment created with\newenvironmentor\NewDocumentEnvironmentwith no\refstepcountercan NOT been jumped to after click the\reftext. This code can be suppressed by defining\hyper@nopatch@thm. Here is the detailed patches:1
2
3
4
5
6
7
8
9
10
11\@ifundefined{hyper@nopatch@thm}{%
\AtBeginDocument{%
\@ifpackageloaded{cleveref}
{\AddToHook{cmd/@thm/before}{\ifhmode\unskip\fi}}
{%
\ifpatchable\@thm{\refstepcounter}
{\patchcmd\@thm{\refstepcounter}{\Hy@theorem@refstepcounter}{}{}}
{}%
}
}
}{} -
table of contents: hyperref redefines
\contentslineto be able to add links to toc entries. For example, you can see the destination names in.tocfile like:\contentsline {section}{\numberline {1}Test 1}{2}{section.1}%, wheresection.1is the destination name. -
math environments: hyperref patches
\equation/\endequation,\eqnarray,\endeqnarray -
counters: hyperref patches the kernel command
\@definecounter,\@newctr,\@addtoresetand the amsmath command\numberwithinto ensure that for every counter the correct\theHcounterrepresentation is created or reset -
…
Bookmark
How does the bookmark generated ? And when ? See 5.7 PDF-specific display options . Hyperref will automatically add bookmarks for \section and similar macros. But you can also set them manually by command:
1 | |
There are some other commands:
1 | |
These commands are defined in file hpdftex.def, see:
1 | |
A simple use example:
1 | |
The output bookmark looks like as below:

source
The source for reference:
1 | |