0

I redefined subsection titles, so the table of contents looks like this:

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

Table of contents

However, such redefinition breaks hyperlinks pointing subsections. Besides I want parentheses around pages where subsections are also be hyperlinked. After many hours of searching I have been founded the best way to fix it is to renew \section command directly (as it is implemented in book.cls).

I figured out (see here) that \@startsection command (it is implemented in latex.ltx) has six required arguments. Moreover, it has two extra arguments defining toctitle and title but the are not used. So I tried to use them and define them manually. But there is a problem --- I want to use subsection title but I don't know how to access it. Answers founded here don't work (I think because of titlesec).

So, how should I redefine this command? Or are there any other ideas how to do what I want?

EDIT 1: toc and aux files:

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
    The first step is too look at the aux and/or toc files and see what hyperref is actually doing. The formatting is done by \l@subsection using the arguments passed. – John Kormylo Jul 23 '23 at 14:30
  • @John Kormylo, I looked through toc and aux files but I didn't find any unusual there. I can attach these files to the post. – Anton Dmitrievich Jul 23 '23 at 15:35

1 Answers1

0

There is a solution (it was found here, thank Gonzalo Medina very much):

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