禁用章节标题中的超链接,但保留章节编号和页面中的超链接

禁用章节标题中的超链接,但保留章节编号和页面中的超链接

基本上我想问如何让目录中的超链接看起来像下面的图片。提前谢谢!在此处输入图片描述

在这个帖子中,有人问了完全相反的问题,并解决了这个问题。也许这对我所要求的解决方案有帮助。我只是不知道要改变哪些变量以及如何改变。我怎样才能使目录中的部分文本可点击,但部分编号保持不可点击?

答案1

我采用了链接解决方案,并将超链接放在了周围\@savenumber而不是\@savetitle

由于没有提到交叉链接部分和目录条目,我删除了该部分解决方案。

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

相关内容