3

I would like a way to make an hyperref link to the beginning of the current section or chapter, that has the same effect of using \label{section} and \ref{section} with hyperref on, but without having to use \label.

Here is an MWE:

\documentclass[10pt,a5paper]{report}
\usepackage{fontspec}
\usepackage{lipsum}
\usepackage[linktoc=all,hidelinks,bookmarksnumbered,pagebackref,xetex]{hyperref}


\begin{document}
\chapter{Chapter 1}
\lipsum[1-2]

\section{Section 1}
\lipsum[3-5]

\section{Section 2}
\lipsum[6]
\textbf{Link to current section.}\\
\textbf{Link to current chapter.}\\
\lipsum[6]
\end{document}
HcN
  • 1,274
  • 9
  • 25
  • What is the current section and what is wrong with \label? –  Sep 29 '15 at 12:47
  • The "current" section is the section where the link is. Since I would use this function quite a lot, it would be nicer not to have to label them each time. – HcN Sep 29 '15 at 12:51
  • Anyway, this means auto-labelling somewhat. How do you want to refer to the current section. The current section 'moves' through the document when being viewed. –  Sep 29 '15 at 12:53
  • See the edited question if it is clearer. – HcN Sep 29 '15 at 12:58
  • The table of contents already has links to all the chapters etc. The names can be found in the aux file. – John Kormylo Sep 29 '15 at 13:46

1 Answers1

4

Something like this?

\documentclass[10pt,a5paper]{report}
\usepackage{xcolor}
\usepackage{fontspec}
\usepackage{lipsum}
\usepackage[linktoc=all,hidelinks,bookmarksnumbered,pagebackref,xetex]{hyperref}


\begin{document}
\chapter{Chapter 1}\hypertarget{chapter::\theHchapter}{}%
\lipsum[1-2]

\section{Section 1}
\lipsum[3-5]

\section{Section 2}\hypertarget{section::\theHsection}{}%
\lipsum[6]
\hyperlink{section::\theHsection}{\textcolor{blue}{Link to current section.}}
\hyperlink{chapter::\theHchapter}{\textcolor{blue}{Link to current chapter.}}
\lipsum[6]



\chapter{Chapter 1}\hypertarget{chapter::\theHchapter}{}%
\lipsum[1-2]

\section{Section 1}
\lipsum[3-5]

\section{Section 2}\hypertarget{section::\theHsection}{}%
\lipsum[6]
\hyperlink{section::\theHsection}{\textcolor{blue}{Link to current section.}}
\hyperlink{chapter::\theHchapter}{\textcolor{blue}{Link to current chapter.}}
\lipsum[6]

\end{document}
  • 4
    You don't need to add hypertargets. They are already there. The format is "chapter.\arabic{chapter}" and "section.\arabic{chapter}.\arabic{section}" not "chapter.\thechapter". – John Kormylo Sep 29 '15 at 13:54
  • Perfect. \hyperlink{section.\arabic{chapter}.\arabic{section}}{Link to current section.} does exactly what I want. Many thanks to you two. – HcN Sep 29 '15 at 14:09
  • Actually, \theHsection and \theHchapter are formatted that way too. – John Kormylo Sep 29 '15 at 14:11
  • Is it possible to refer to \labels I placed? I've tried to \hyperlink{\ref{chap:last}}{Last}, but it fails: Use of \hyper@link@ doesn't match its definition. – Frederick Nord Oct 01 '21 at 11:06