Example using \label and \ref
The following MWE
\documentclass[12pt]{book}%ext
%\usepackage{hyperref}
\begin{document}
see problem \ref{1} below and problem \ref{5} below.
\chapter{Differential equations}
\section{some section name}
\subsection{problem 1 from some book}
\label{1}
\subsection{problem 2 from some book}
\label{2}
OK.
\chapter{Differential equations second book}
\section{some section name}
\subsection{problem 1 from some second book}
\label{3}
OK.
\subsection{problem 2 from some second book}
\label{5}
OK.
\end{document}
Compiled using lualatex foo.tex
gives
What needs to change to make \ref{1}
prints the exact label name 1
in this case? So that the output reads
see problem 1 below and problem 5 below
I do not care for the subsection number when reading the PDF. I need to know the actual problem number itself, which is what is in what the argument of \ref{1}
is meant to be. Ofcourse clicking on the ref
should still send me to the actual subsection in the document where the same label
is located. I just want the display to be 1
exactly as it is.
The label I use is global counter which is increased by one for each problem.
I looked at number of documentation and so far not able to figure how. Do I need to use different package for this? Or do I need to use phantomsection? I need to emulate the output using \hyperlink and \hypertarget as shown below. Is this possible? if not, I will continue to use \hyperlink
and \hypertarget
in this case
Example using \hyperlink
and \hypertarget
\documentclass[12pt]{book}
\usepackage{hyperref}
\begin{document}
see problem \hyperlink{1}{1} below and problem \hyperlink{4}{4} below.
\chapter{Differential equations}
\section{some section name}
\subsection{problem 1 from some book}
\hypertarget{1}{}
\subsection{problem 2 from some book}
\hypertarget{2}{}
OK.
\chapter{Differential equations second book}
\section{some section name}
\subsection{problem 1 from some second book}
\hypertarget{3}{}
OK.
\subsection{problem 2 from some second book}
\hypertarget{4}{}
OK.
\end{document}
When the above is compiled, it gives the output I want
Or
Example using \label and \hyperref
This was suggested thanks to gusbrs in comment
\documentclass[12pt]{book}%ext
\usepackage{hyperref}
\begin{document}
see problem \hyperref[1]{1} below and problem \hyperref[4]{4} below.
\chapter{Differential equations}
\section{some section name}
\subsection{problem 1 from some book}
\label{1}
\subsection{problem 2 from some book}
\label{2}
OK.
\chapter{Differential equations second book}
\section{some section name}
\subsection{problem 1 from some second book}
\label{3}
OK.
\subsection{problem 2 from some second book}
\label{4}
OK.
\end{document}
Using Tl 2021
Best Answer
The advice you received to use
\label
and\ref
is usually a sound one. But, as you explained in the comments, in your case, the "global counter" you'd like to refer to is not a LaTeX counter, but a counter generated elsewhere by an external program which generates your LaTeX document. That given, this "counter", from the perspective of the LaTeX referencing system, is "arbitrary text", in which case, the tools ofhyperref
seem the most appropriate ones.Using
\hypertarget
and\hyperlink
as you've been doing should work fine, but you may get some simplification by using\label
and\hyperref
, as in the example below. There is also a technical difference between using\hypertarget
/\hyperlink
vs.\label
/\hyperref
: anchor placement. With the latter, the hyperlink anchor will be placed with the sectioning command, while with the former the anchor will be just bellow it. In practice, this means that with\label
/\hyperref
you are sure to see the heading you'd expect when you follow the hyperlink.That doesn't mean you could not use
\label
and\ref
in interesting ways, but for that, I'd say the counter should be handled on the LaTeX side, for example if you created a environment for your problems with a dedicated counter. But this would probably have some cost in adjusting the program that generates your document, whether it'd be worth it, I cannot tell, but you certainly can. ;-)