自定义编辑目录中带有超链接的子章节标题

自定义编辑目录中带有超链接的子章节标题

我重新定义了小节标题,因此目录如下所示:

\documentclass[twoside, a4paper, 12pt, showtrims]{book}
%\usepackage{showframe}
\usepackage[X2,T1,T2A]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}

\usepackage{xcolor}
\usepackage[shortlabels]{enumitem}

\usepackage{etoolbox}

\usepackage[pagestyles, newparttoc, explicit]{titlesec}
\usepackage{titletoc}
\usepackage{caption}

\usepackage[linktoc = all]{hyperref}
\usepackage{cleveref}
\usepackage{hypcap}
\usepackage{lipsum}

\renewcommand*\thepart{\Roman{part}}
\renewcommand*\thesection{\S\arabic{section}.}
\renewcommand*\thesubsection{\Roman{subsection}.}
\renewcommand*\thesubsubsection{\arabic{subsubsection}.}

\makeatletter
\renewcommand\subsection{
    \@startsection{subsection}{2}{\z@}%
    {-3.25ex\@plus -1ex \@minus -.2ex}%
    {1.5ex \@plus .2ex}%
    {\normalfont\large\bfseries}
    %{style}*[toctitle]{title}
    %What should be here?
}
\makeatother

\titlecontents*{subsection}
[32 pt]
{\small}
{\thecontentslabel\ }
{}
{\ (\thecontentspage)}
[.\ ]
[]

\begin{document}
\part{The Part}
\chapter{Caption}
    \section{Com Primo}
    \lipsum[1]
    \section{Secondo}
        \subsection{Erste}
            \subsubsection{SubSub}
            This is a test.
        \subsection{Subsection 2}
    \section{Zweite}
        \subsection{Sub 1}
            \subsubsection{SubSub 2}
        \subsection{Sub Secondo}
    \part{The second Part}
    \setcounter{tocdepth}{2}
    \tableofcontents{}
\end{document}

目录

但是,这种重新定义会破坏指向子节的超链接。此外,我希望在子节也超链接的页面周围使用括号。经过几个小时的搜索,我发现修复它的最佳方法是\section直接更新命令(因为它在中实现book.cls)。

我发现(见这里) 该\@startsection命令(它在 中实现latex.ltx)有六个必需参数。此外,它还有两个额外的参数定义toctitletitle的额外参数,但没有使用。所以我尝试使用它们并手动定义它们。但有一个问题 --- 我想使用小节标题,但我不知道如何访问它。答案成立这里不起作用(我认为是因为titlesec)。

那么,我应该如何重新定义此命令?或者还有其他想法可以实现我想要的功能吗?

编辑1tocaux文件:

toc

\babel@toc {english}{}\relax 
\contentsline {part}{I\hspace {1em}The Part}{1}{part.1}%
\contentsline {chapter}{\numberline {1}Caption}{3}{chapter.1}%
\contentsline {section}{\numberline {\S 1.}Com Primo}{3}{section.1.1}%
\contentsline {section}{\numberline {\S 2.}Secondo}{3}{section.1.2}%
\contentsline {subsection}{\numberline {I.}Erste}{3}{subsection.1.2.1}%
\contentsline {subsubsection}{SubSub}{3}{subsubsection*.1}%
\contentsline {subsection}{\numberline {II.}Subsection 2}{4}{subsection.1.2.2}%
\contentsline {section}{\numberline {\S 3.}Zweite}{4}{section.1.3}%
\contentsline {subsection}{\numberline {I.}Sub 1}{4}{subsection.1.3.1}%
\contentsline {subsubsection}{SubSub 2}{4}{subsubsection*.2}%
\contentsline {subsection}{\numberline {II.}Sub Secondo}{4}{subsection.1.3.2}%
\contentsline {part}{II\hspace {1em}The second Part}{5}{part.2}%
\contentsfinish

aux

\relax 
\providecommand\babel@aux[2]{}
\@nameuse{bbl@beforestart}
\providecommand\hyper@newdestlabel[2]{}
\providecommand\HyperFirstAtBeginDocument{\AtBeginDocument}
\HyperFirstAtBeginDocument{\ifx\hyper@anchor\@undefined
\global\let\oldnewlabel\newlabel
\gdef\newlabel#1#2{\newlabelxx{#1}#2}
\gdef\newlabelxx#1#2#3#4#5#6{\oldnewlabel{#1}{{#2}{#3}}}
\AtEndDocument{\ifx\hyper@anchor\@undefined
\let\newlabel\oldnewlabel
\fi}
\fi}
\global\let\hyper@last\relax 
\gdef\HyperFirstAtBeginDocument#1{#1}
\providecommand\HyField@AuxAddToFields[1]{}
\providecommand\HyField@AuxAddToCoFields[2]{}
\babel@aux{english}{}
\@writefile{toc}{\contentsline {part}{I\hspace  {1em}The Part}{1}{part.1}\protected@file@percent }
\@writefile{toc}{\contentsline {chapter}{\numberline {1}Caption}{3}{chapter.1}\protected@file@percent }
\@writefile{lof}{\addvspace {10\p@ }}
\@writefile{lot}{\addvspace {10\p@ }}
\@writefile{toc}{\contentsline {section}{\numberline {\S  1.}Com Primo}{3}{section.1.1}\protected@file@percent }
\@writefile{toc}{\contentsline {section}{\numberline {\S  2.}Secondo}{3}{section.1.2}\protected@file@percent }
\@writefile{toc}{\contentsline {subsection}{\numberline {I.}Erste}{3}{subsection.1.2.1}\protected@file@percent }
\@writefile{toc}{\contentsline {subsubsection}{SubSub}{3}{subsubsection*.1}\protected@file@percent }
\@writefile{toc}{\contentsline {subsection}{\numberline {II.}Subsection 2}{4}{subsection.1.2.2}\protected@file@percent }
\@writefile{toc}{\contentsline {section}{\numberline {\S  3.}Zweite}{4}{section.1.3}\protected@file@percent }
\@writefile{toc}{\contentsline {subsection}{\numberline {I.}Sub 1}{4}{subsection.1.3.1}\protected@file@percent }
\@writefile{toc}{\contentsline {subsubsection}{SubSub 2}{4}{subsubsection*.2}\protected@file@percent }
\@writefile{toc}{\contentsline {subsection}{\numberline {II.}Sub Secondo}{4}{subsection.1.3.2}\protected@file@percent }
\@writefile{toc}{\contentsline {part}{II\hspace  {1em}The second Part}{5}{part.2}\protected@file@percent }
\ttl@finishall
\gdef \@abspage@last{7}

答案1

有一个解决方案(发现这里, 感谢贡萨洛·梅迪纳非常):

\documentclass[twoside, a4paper, 12pt, showtrims]{book}
%\usepackage{showframe}
\usepackage[X2,T1,T2A]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}

\usepackage{xcolor}
\usepackage[shortlabels]{enumitem}

\usepackage{etoolbox}

\usepackage[pagestyles, newparttoc, explicit]{titlesec}
\usepackage{titletoc}
\usepackage{caption}

\usepackage[linktoc = all]{hyperref}
\usepackage{cleveref}
\usepackage{hypcap}
\usepackage{lipsum}

\renewcommand*\thepart{\Roman{part}}
\renewcommand*\thesection{\S\arabic{section}.}
\renewcommand*\thesubsection{\Roman{subsection}.}
\renewcommand*\thesubsubsection{\arabic{subsubsection}.}

\makeatletter
\newlength\rightmargintoc
\setlength\rightmargintoc{\linewidth}
\addtolength\rightmargintoc{0em}

\makeatletter
\def\subsectocline#1#2#3#4#5{%
    \parshape 2 #2 \rightmargintoc \dimexpr\parindent+3em\relax \rightmargintoc
    \@tempdima #3
    \ifdim\lastskip=#2\relax\else\fi{{#4}~\hyperlink{\Hy@tocdestname}{(#5)}.}\hskip1em%
}
\renewcommand*\l@subsection{\subsectocline{2}{3em}{3em}}
\makeatother

\pretocmd{\part}{\addtocontents{toc}{\par}}{}{}
\pretocmd{\chapter}{\addtocontents{toc}{\par}}{}{}
\pretocmd{\section}{\addtocontents{toc}{\par}}{}{}

\AtEndDocument{%
    \ifnum\value{subsection}>0\relax
    \addtocontents{toc}{\par}
    \fi}

\begin{document}
    \part{The Part}
    \chapter{Caption}
    \section{Com Primo}
    \lipsum[1]
    \section{Secondo}
    \subsection{Erste}
    \subsubsection{SubSub}
    This is a test.
    \subsection{Subsection 2}
    \section{Zweite}
    \subsection{Sub 1}
    \subsubsection{SubSub 2}
    \subsection{Sub Secondo}
    \part{The second Part}
    \setcounter{tocdepth}{2}
    \tableofcontents{}
\end{document}

相关内容