我怎样才能使目录中的部分文本可点击,但部分编号保持不可点击?

我怎样才能使目录中的部分文本可点击,但部分编号保持不可点击?

我最近发现此主题关于如何使章节标题可点击/返回到目录中的条目——唯一的问题是,标题文字部分标题是可点击的,但数字不是。我只是想让目录反映这种风格。

这是一个模板文档:

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

答案1

这既会将章节编号添加到反向链接,也会从目录中删除章节编号。后者肯定更难。

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

相关内容