0

basically I would like to ask how to make the hyperlinks in toc look like the picture bellow. Thanks in advance! enter image description here

In this thread the exact opposite was asked and solved. Maybe it helps with the solution I am asking. I just don't know what variables to change and how. How can I make the section-text in a Table of Contents clickable, but keep the section-number *unclickable*?

1 Answers1

0

I took my linked solution and placed the hyperlinks around \@savenumber instead of \@savetitle.

Since there was no mention of cross-linking sections and TOC entries, I removed that part of the solution.

\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,
}

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

% begin code to remove section titles 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{\hyper@linkstart{link}{\Hy@tocdestname}\@savenumber\hyper@linkend}%
        {\@gettitle{#2}}}{#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{\hyper@linkstart{link}{\Hy@tocdestname}\@savenumber\hyper@linkend}%
          {\@gettitle{#2}}
        \egroup}{}%
      \else
        \csname l@#1\endcsname{\numberline{\hyper@linkstart{link}{\Hy@tocdestname}\@savenumber\hyper@linkend}%
          {\@gettitle{#2}}
        }{%
          \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}
John Kormylo
  • 79,712
  • 3
  • 50
  • 120
  • Thank you so much! –  Apr 08 '19 at 05:41
  • There seems to be a problem when you try to right in math mode in the section title. For example when I write this \subsection{for (r>r_{0})} I get an error. When I remove it it works fine. Is there any fix for that? –  Apr 08 '19 at 06:06
  • There is also an error when I use \renewcommand\thesubsection{(\emph{\roman{subsection}})}. –  Apr 08 '19 at 06:23
  • I used \xdef to separate the title from the number, and there are certain things (like \emph) that \xdef cannot handle. OTOH, using math in titles will generate missing $ messages regardless. – John Kormylo Apr 08 '19 at 14:28
  • I revised the solution to stop using \xdef. – John Kormylo Apr 08 '19 at 15:14
  • Thank you thank you thank you! You are awesome sir. My respects to you! –  Apr 08 '19 at 16:07
  • There seems to be a problem when I try to use \addcontentsline{toc}{section}{Appendix}. For some reason the numbering continues. –  Apr 16 '19 at 18:06
  • Fixed it. you just add \addcontentsline{toc}{section}{\protect\numberline{}Appendix} –  Apr 16 '19 at 18:18