0

I would like to make a section title (say 'Land') in the document a clickable hyperlink, so that when I click on it, I jump to some other section (say 'Beach'). So, I'd need some hyperref command as argument of \section{Land}. I've tried \section[Land]{\hyperlink{hyp:sec:beach}{Land}{}} which produces a clickable link for the section title 'Land', but when one clicks on it, it jumps to the top of the document, not to 'Beach'. Here is a MWE (including some formatting commands for the section title, toc, and headers, because the sought-after solution needs to be compatible with these):

\documentclass[12pt,a4paper,openany]{book} 
\renewcommand{\familydefault}{cmss} 
\usepackage[newparttoc]{titlesec} 
\usepackage{titletoc} 
\usepackage{color}
\definecolor{darkred}{rgb}{0.7,0,0}
\usepackage[linktocpage]{hyperref}
\hypersetup{colorlinks,linkcolor=darkred}
\titlecontents{chapter}[0mm]   
   {\vspace{0pt}\normalsize\normalfont} 
   {\contentslabel[\thecontentslabel]{10mm}}{}
   {\titlerule*[.75em]{.}\thecontentspage}   
\titlecontents{section}[10mm]    
   {\vspace{0pt}\normalsize\normalfont} 
   {\contentslabel[\thecontentslabel]{10mm}}{}
   {\titlerule*[.75em]{.}\thecontentspage}   
\newpagestyle{mystylechapter}{
\sethead
   [\thepage][\thechapter\hspace{1em}\chaptertitle][]
   {}{\thesection\hspace{1em}\sectiontitle}{\thepage}
} 

\titleformat{\section}{\color{darkred}\normalfont\Large}
  {\color{darkred}\thesection}{1em}{}
\titleformat{\chapter}{\color{darkred}\normalfont\LARGE}
  {\color{darkred}\thechapter}{1em}{}

\begin{document}
\tableofcontents 
\pagestyle{mystylechapter}
\chapter{Continent} 
\section[Land]{\hyperlink{hyp:sec:beach}{Land}{}} 
\label{sec:land} 
\newpage
\section{Beach}
\label{sec:beach}
\end{document}  

1 Answer 1

1

If you check your log file, you will find a message like this:

name{hyp:sec:beach} has been referenced but does not exist, replaced by a fixed one

There are two mistakes in your code: You have labeled the section "Beach" as \label{sec:beach} while trying to link to hyp:sec:beach, so either the label or the linktarget name have to be changed. Moreover, you are using the wrong command. According to the hyperref documentation, the command \hyperlink links to a target defined with \hypertarget{name}{text} -- not normal labels! For linking to normal LaTeX labels, you have to use \hyperref[label]{text}.

Here is your code with corrections:

\documentclass[12pt,a4paper,openany]{book}
\renewcommand{\familydefault}{cmss}
\usepackage[newparttoc,pagestyles]{titlesec}
\usepackage{titletoc}
\usepackage{color}
\definecolor{darkred}{rgb}{0.7,0,0}
\usepackage[linktocpage]{hyperref}
\hypersetup{colorlinks,linkcolor=darkred}
\titlecontents{chapter}[0mm]
   {\vspace{0pt}\normalsize\normalfont}
   {\contentslabel[\thecontentslabel]{10mm}}{}
   {\titlerule*[.75em]{.}\thecontentspage}
\titlecontents{section}[10mm]
   {\vspace{0pt}\normalsize\normalfont}
   {\contentslabel[\thecontentslabel]{10mm}}{}
   {\titlerule*[.75em]{.}\thecontentspage}
\newpagestyle{mystylechapter}{
\sethead
   [\thepage][\thechapter\hspace{1em}\chaptertitle][]
   {}{\thesection\hspace{1em}\sectiontitle}{\thepage}
}

\titleformat{\section}{\color{darkred}\normalfont\Large}
  {\color{darkred}\thesection}{1em}{}
\titleformat{\chapter}{\color{darkred}\normalfont\LARGE}
  {\color{darkred}\thechapter}{1em}{}

\begin{document}
\tableofcontents
\pagestyle{mystylechapter}
\chapter{Continent}
\section[Land]{\hyperref[sec:beach]{Land}}
\label{sec:land}
\newpage
\section{Beach}
\label{sec:beach}
\end{document}
1
  • Thanks, works perfectly. Follow-up question: if I want the section label to be clickable instead of the section title, what should I change?
    – jvd
    Commented Oct 6, 2017 at 19:56

You must log in to answer this question.

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