3

I recently found this thread on how to make section titles clickable/return to their entry in the Table of Contents -- only problem is, the title text of the section headers are clickable, but the numbers are not. I simply want to make the Table of Contents mirror this style.

Here is a template document:

\documentclass[11pt]{article}
\usepackage[margin=0.5in,footskip=0.4in]{geometry}
\usepackage[T1]{fontenc}
\usepackage{hyperref}
\hypersetup{
    colorlinks=true,
    linkcolor=blue,
    linktoc=all,
}

% For clickable section headers
% See https://tex.stackexchange.com/a/251964/73149
\makeatletter
\let\hypersection\section
\def\section{\@ifstar\starsection\mysection}
\def\starsection{\hypersection*}
\newcommand{\mysection}[2][\@empty]% #1=optional (toc), #2=title
{\ifx#1\@empty \hypersection[#2]{\hyperlink{toc.section.\thesection}{#2}}
 \else \hypersection[#1]{\hyperlink{toc.section.\thesection}{#2}}
 \fi}
\let\hypersubsection\subsection
\def\subsection{\@ifstar\starsubsection\mysubsection}
\def\starsubsection{\hypersubsection*}
\newcommand{\mysubsection}[2][\@empty]% #1=optional (toc), #2=title
{\ifx#1\@empty \hypersubsection[#2]{\hyperlink{toc.subsection.\thesubsection}{#2}}
 \else \hypersubsection[#1]{\hyperlink{toc.subsection.\thesubsection}{#2}}
 \fi}
\makeatother
\let\hypercontentsline=\contentsline
\renewcommand{\contentsline}[4]{\hypertarget{toc.#4}{}\hypercontentsline{#1}{#2}{#3}{#4}}

% Body
\begin{document}
  \title{This is a Title}
  \date{}
  \maketitle
  \tableofcontents
  \section{A Section}
  Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
  \section{Another One}
  \subsection{Hello}
  Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
  \subsection{Goodbye}
  Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
  \section{Final Section}
  Peace out.
\end{document}
3
  • Why not mirror the other way?
    – cfr
    Jan 30, 2018 at 0:05
  • Got the impression that would be more difficult. Check out the comment thread in the answer from the thread I linked.
    – Luke Davis
    Jan 30, 2018 at 3:02
  • One involves trying to separate \numberline from the title, the other involves replacing \@sect. Jan 30, 2018 at 4:45

1 Answer 1

3

This both adds the section number to the back link, and removes the section number from the toc line. The latter was definitely harder.

\documentclass[11pt]{article}
\usepackage[margin=0.5in,footskip=0.4in]{geometry}
\usepackage[T1]{fontenc}
\usepackage{etoolbox}
\usepackage{hyperref}
\hypersetup{
    colorlinks=true,
    linkcolor=blue,
    linktoc=all,
}

% For clickable section headers
% See https://tex.stackexchange.com/a/251964/73149
\makeatletter
\def\@sect#1#2#3#4#5#6[#7]#8{%
  \ifnum #2>\c@secnumdepth
    \let\@svsec\@empty
  \else
    \refstepcounter{#1}%
    \protected@edef\@svsec{\@seccntformat{#1}\relax}%
  \fi
  \@tempskipa #5\relax
  \ifdim \@tempskipa>\z@
    \begingroup
      #6{\sbox0{\@svsec}% measure width
      \interlinepenalty \@M
      \hangindent=\dimexpr \wd0+#3\relax
      \noindent\hskip #3\relax
      \hyperlink{toc.#1.\csname the#1\endcsname}%
        {\@svsec #8}%
      \@@par}%
    \endgroup
    \csname #1mark\endcsname{#7}%
    \addcontentsline{toc}{#1}{%
    \ifnum #2>\c@secnumdepth \else
      \protect\numberline{\csname the#1\endcsname}%
    \fi
    #7}%
  \else
    \def\@svsechd{%
      #6{\hskip #3\relax
      \@svsec #8}%
    \csname #1mark\endcsname{#7}%
    \addcontentsline{toc}{#1}{%
    \ifnum #2>\c@secnumdepth \else
      \protect\numberline{\csname the#1\endcsname}%
    \fi
    #7}}%
  \fi
  \@xsect{#5}}
\makeatother

%\let\hypercontentsline=\contentsline
%\renewcommand{\contentsline}[4]{\hypertarget{toc.#4}{}\hypercontentsline{#1}{#2}{#3}{#4}}

% begin code to remove section numbers from the TOC
\makeatletter
\newcommand{\@savenumber}{}% reserve global names
\newcommand{\@gettitle}[1]% remove \numberline from title
  {\bgroup\let\numberline=\@gobble#1\egroup}

\def\contentsline#1#2#3#4{% #1=type, #2=entry, #3=page, #4=anchor
  \hypertarget{toc.#4}{}% set up backlink
  \bgroup% separate \numberline from title
    \renewcommand{\numberline}[1]{\gdef\@savenumber{##1}}%
    \sbox0{#2}% throw away title, keep number
  \egroup
  \begingroup
    \Hy@safe@activestrue
  \edef\x{\endgroup
    \def\noexpand\Hy@tocdestname{#4}%
  }\x
  \ifx\Hy@tocdestname\ltx@empty
    \csname l@#1\endcsname{#2}{#3}%
  \else
    \ifcase\Hy@linktoc % none
      \csname l@#1\endcsname{#2}{#3}%
    \or % section
      \csname l@#1\endcsname{\numberline{\@savenumber}%
        {\hyper@linkstart{link}{\Hy@tocdestname}\@gettitle{#2}\hyper@linkend}}{#3}%
    \or % page
      \def\Hy@temp{#3}%
      \ifx\Hy@temp\ltx@empty
        \csname l@#1\endcsname{#2}{#3}%
      \else
        \csname l@#1\endcsname{{#2}}{%
          \hyper@linkstart{link}{\Hy@tocdestname}{#3}\hyper@linkend
        }%
      \fi
    \else % all
      \def\Hy@temp{#3}%
      \ifx\Hy@temp\ltx@empty
        \csname l@#1\endcsname{\numberline{\@savenumber}%
          {\hyper@linkstart{link}{\Hy@tocdestname}\@gettitle{#2}\hyper@linkend}
        \egroup}{}%
      \else
        \csname l@#1\endcsname{\numberline{\@savenumber}%
          {\hyper@linkstart{link}{\Hy@tocdestname}\@gettitle{#2}\hyper@linkend}
        }{%
          \hyper@linkstart{link}{\Hy@tocdestname}{#3}\hyper@linkend
        }%
      \fi
    \fi
  \fi
}
\makeatother

% Body
\begin{document}
  \title{This is a Title}
  \date{}
  \maketitle
  \tableofcontents
  \section{A Section}
  Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
  \section{Another One}
  \subsection{Hello}
  Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
  \subsection{Goodbye}
  Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
  \section{Final Section}
  Peace out.
\end{document}
4
  • Thanks a lot! This is utterly black magic to me. Might want to mention also that the \let\hypercontentsline seems to make the backlinks point to specific items in the Table of Contents; they point to the head of the document without those lines. At least this is the case when I tried it.
    – Luke Davis
    Jan 31, 2018 at 3:49
  • It worked for me. Both redefinitions of \contentsline add \hypertarget{toc.#4}{} at the start. The source for \@sect comes from source2e.pdf (CTAN) and the source for \contentsline comes from hyperref.sty. Jan 31, 2018 at 4:05
  • This doesn't seem to work for \chapters :(
    – Svalorzen
    Sep 13, 2022 at 13:23
  • @Svalorzen - Macros like \@sect are specific to sections. Sep 13, 2022 at 15:00

You must log in to answer this question.

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