0

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

enter image description here

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

enter image description here

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}

enter image description here

Using Tl 2021

17
  • If you have a second section in this document, there will be a "problem 1.2.1", same for chapter. If you refer to it by just "1", how will you know the difference between "1.1.1" and "1.2.1"?
    – gusbrs
    Commented Mar 13, 2022 at 2:00
  • problem number in \label{n} is unique for the whole document. it is a counter. These are 1,2,3,4,... and I want to do \ref{1} and \ref{2} etc... and have it display 1 and 2. I do not care for the name of the section or subsection the problem is located in. Similar to how \href{url}{display text} works. But \ref does not seem to take a display text.
    – Nasser
    Commented Mar 13, 2022 at 2:03
  • Your MWE uses a subsection for "problem 1", and that is not an unique counter, since it will be reset every section and chapter. If you have indeed an unique counter, it is easy to define the printed representation of that counter to be by itself. But please provide a MWE which corresponds to that which you are describing then.
    – gusbrs
    Commented Mar 13, 2022 at 2:12
  • Btw, if you really have a counter which is not reset on sectioning levels, it probably does what you want by default.
    – gusbrs
    Commented Mar 13, 2022 at 2:13
  • 1
    If it is an external "counter", from LaTeX's perspective, the "reference" you want is "arbitrary text", and indeed, as far as I can tell, \ref is not the best tool. I'd personally use \label and \hyperref[label]{text}.
    – gusbrs
    Commented Mar 13, 2022 at 2:46

1 Answer 1

5

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 of hyperref 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.

\documentclass[12pt]{book}
\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}

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. ;-)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .