0

For my lot and lof i am using something i found here, so that the parts and chapters show in them (lot and lof). And for my Titles (Part, Chapter, Section ...) i am using titlesec, so they look (imo) better than the default i get with the book class.

Now my Problem is, that if i use those two on their own, they work just fine, but not when i use those simultaneously. When titlesec is used, it somehow interferes with addcontentsline/addtocontents, so the parts and chapters do not show up in lot or lof anymore.

My MWE:

\documentclass[a4paper, 11pt]{book}

%Change Font to sf \renewcommand{\familydefault}{\sfdefault}

\iffalse %Make Titles pretty \usepackage[explicit]{titlesec} \usepackage{xcolor} \def\leer{32pt} \def\ler{4pt} \fboxsep0pt \titleclass{\part}{top} \titleformat{\part}[display]{\normalfont\LARGE\bfseries}{}{0em}{\colorbox{gray!100}{\fbox{\parbox{\textwidth}{\parbox[t]{\leer}{\hspace{\ler}\thepart}\parbox[t]{\dimexpr\textwidth-\leer\relax}{\strut#1\strut}}}}}{} \titleformat{name=\part, numberless}[display]{\normalfont\LARGE\bfseries}{}{0em}{\colorbox{gray!100}{\fbox{\parbox{\textwidth}{\hspace{\ler}\strut#1\strut}}}}{} \titlespacing{\part}{0pt}{0pt}{1em} \titleclass{\chapter}{straight} \titleformat{\chapter}[display]{\normalfont\Large\bfseries}{}{0em}{\colorbox{gray!75}{\fbox{\parbox{\textwidth}{\parbox[t]{\leer}{\hspace{\ler}\thechapter}\parbox[t]{\dimexpr\textwidth-\leer\relax}{\strut#1\strut}}}}}{} \titleformat{name=\chapter, numberless}[display]{\normalfont\Large\bfseries}{}{0em}{\colorbox{gray!75}{\fbox{\parbox{\textwidth}{\hspace{\ler}\strut#1\strut}}}}{} \titlespacing{\chapter}{0pt}{0pt}{1em} \titleformat{\section}[display]{\normalfont\large\bfseries}{}{0em}{\colorbox{gray!50}{\fbox{\parbox{\textwidth}{\parbox[t]{\leer}{\hspace{\ler}\thesection}\parbox[t]{\dimexpr\textwidth-\leer\relax}{\strut#1\strut}}}}}{} \titleformat{name=\section, numberless}[display]{\normalfont\large\bfseries}{}{0em}{\colorbox{gray!100}{\fbox{\parbox{\textwidth}{\hspace{\ler}\strut#1\strut}}}}{} \fi

%Change Language \usepackage[ngerman]{babel}

%Absolutly no clue \usepackage[utf8]{inputenc} \usepackage[T1]{fontenc}

%For Pictures and Graphics \usepackage{graphicx} \usepackage{float} \usepackage{wrapfig}

%For Tables \usepackage{tabularx}

%Add Parts and Chapters in lof, lot and lol \usepackage{xpatch}

\makeatletter

\def\thisparttitle{}\def\thispartnumber{} \def\thischaptertitle{}\def\thischapternumber{} \let\Chaptermark\chaptermark \def\chaptermark#1{\def\Chaptername{#1}\Chaptermark{#1}}

\newtoggle{noFigs} \newtoggle{noTabs} \newtoggle{noLists} \newtoggle{Fpart} \newtoggle{Tpart} \newtoggle{Lpart}

\xpatchcmd{@part}{% \addcontentsline{toc}{part}{\thepart\hspace{1em}#1}% }{% \addcontentsline{toc}{part}{\thepart\hspace{1em}#1}% \xdef\thisparttitle{#1}% \global\toggletrue{Fpart}% \global\toggletrue{Tpart}% \global\toggletrue{Lpart}% }{\typeout{Patch success}}{\typeout{Patch failure}}

\xpatchcmd{@chapter}{% \addcontentsline{toc}{chapter}{\protect\numberline{\thechapter}#1}% }{% \addcontentsline{toc}{chapter}{\protect\numberline{\thechapter}#1}% \xdef\thischaptertitle{#1}% \global\togglefalse{noFigs}% \global\togglefalse{noTabs}% \global\togglefalse{noLists}% }{\typeout{Patch success}}{\typeout{Patch failure}} \makeatother

\usepackage{hyperref}

\AtBeginDocument{ \AtBeginEnvironment{table}{ \nottoggle{noTabs}{ \iftoggle{Tpart}{ \addtocontents{lot}{\protect\contentsline{part}{\thepart\hspace{1em}\thisparttitle}{}{part.\theHpart}} \global\togglefalse{Tpart} }{} \addcontentsline{lot}{chapter}{\protect\numberline{\thechapter}\Chaptername} \global\toggletrue{noTabs} }{} } \AtBeginEnvironment{figure}{ \nottoggle{noFigs}{ \iftoggle{Fpart}{ \addtocontents{lof}{\protect\contentsline{part}{\thepart\hspace{1em}\thisparttitle}{}{part.\theHpart}} \global\togglefalse{Fpart} }{} \addcontentsline{lof}{chapter}{\protect\numberline{\thechapter}\Chaptername} \global\toggletrue{noFigs} }{} } \AtBeginEnvironment{lists}{ \nottoggle{noLists}{ \iftoggle{Lpart}{ \addtocontents{lol}{\protect\contentsline{part}{\thepart\hspace{1em}\thisparttitle}{}{part.\theHpart}} \global\togglefalse{Lpart} }{} \addcontentsline{lol}{chapter}{\protect\numberline{\thechapter}\Chaptername} \global\toggletrue{noLists} }{} } } \usepackage{xurl}

\begin{document} \makeatletter @ifclassloaded{book}{\frontmatter}{} \makeatother \chapter{Vorwort} Some Formula and Text. \makeatletter @ifclassloaded{book}{\mainmatter}{} \makeatother \part{Gleichstromtechnik} \chapter{Ladung} Some Formula and Text. \part{Wechselstromtechnik} \chapter{Stationäres magnetisches Feld} \section{Magnetische Spannung} Some Formula and Text. \section{Magnetische Feldstärke} Some Formula and Text. \section{Magnetische Felddichte} Some Formula and Text. \subsection{Permeabilität} Some Formula and Text. \section{Magnetisches Feld} Some Formula and Text. \subsection{Magnetischer Widerstand und Leitfähigkeit} Some Formula and Text. \subsection{Analogie zu elektrischen Kreisen} Some Formula and Text. \chapter{Induktion} \chapter{Magnetische Energie} \section{Verketteter Fluss} Some Formula and Text. \section{Energie und Energiedichte} Some Formula and Text. \part{Mathematische Spielereien} \chapter{Herleitungen verschiedener Formeln} Some Formula and Text. \section{Herleitung von $\frac{i_2}{i_1}=\frac{w_1}{w_2}$} Some Formula and Text. \chapter{Der Sinus} \section{test} \begin{figure}[H] \caption{Mein erster Plot mit pgfplots/tikz} \end{figure} \chapter{Zusammenhänge der Felder} \section{test2} Some Formula and Text. \begin{table}[H] \caption{Felder} \end{table} \chapter{Schaltkreise mit TikZ} \section{test3} \begin{figure}[H] \caption{My first Circuit with Circuitikz} \end{figure} \begin{figure}[H] \caption{Transformator mit TikZ} \end{figure} \newpage \makeatletter @ifclassloaded{book}{\backmatter}{} \makeatother \appendix %\renewcommand{\lstlistlistingname}{Programmverzeichnis} %\renewcommand{\lstlistingname}{Programmverzeichnis} %\lstlistoflistings %\addcontentsline{toc}{chapter}{\lstlistingname} %\newpage \listoftables \addcontentsline{toc}{chapter}{\listtablename} %\addtocontents{toc}{\listtablename} \newpage \listoffigures \addcontentsline{toc}{chapter}{\listfigurename} \end{document}

The lot or lof should look like this: enter image description here

But it looks like this (comment out \iffalse and \fi): enter image description here

I am relativly new to LaTeX and sry if my english is bad.

It would help me A LOT when this problem could be solved, thank you.

  • Welcome! Yes, these will conflict. Either go the titletoc route and keep titlesec or dump titlesec and keep your own customisation of the commands titlesec changes. You can't take a non-titlesec group of patches and expect them to work on commands titlesec is already patching. Personally, I systematically avoid titlesec and have eradicated it from my own code. In every case, I found it more trouble than it was worth. If you just want to use what it offers, it's probably fine - other people love it. Just you have to go all-titlesec. titlesec does not play well with others! – cfr Feb 18 '24 at 07:24
  • 1
    Okey, so after i read your comment, i tried to get rid of the hole xpatch thing and include the patching into titlesec. I actually got it to work, by putting the patching in the <after code> part from titleformat. Thank you so much for hinting me to this path! I will post the solution/code later and close this threat. – Gabriele Di Toma Feb 19 '24 at 14:51
  • Good for you GDT! – cfr Feb 19 '24 at 17:24

1 Answers1

0

Alright, so as @cfr pointed out in the comments; it is not possible to patch commands in two different ways at the same time. That means to have the nice headings/titles AND to put parts/chapters in lot/lof, i somehow had to get rid of either titlesec or xpatch. Because i actually like working with titlesec i decided to ditch xpatch. Therefore i put the patching i had with xpatch into the <after code> part of titleformat. And as i pointed out in the comments, this worked surpisingly well!

So here is my solution:

\documentclass[a4paper, 11pt]{book}

%Change Font to sf \renewcommand{\familydefault}{\sfdefault}

%Make Titles pretty and add them to lot, lof and lol, plus hyperref \usepackage[explicit]{titlesec} \usepackage{etoolbox} \def\thischaptertitle{}\def\thischapternumber{} \newtoggle{noFigs}\newtoggle{noTabs}\newtoggle{noList} \def\thisparttitle{}\def\thispartnumber{} \newtoggle{Fpart}\newtoggle{Tpart}\newtoggle{Lpart} \usepackage{xcolor} \def\leer{32pt} \def\ler{4pt} \fboxsep0pt

\titleclass{\part}{top} \titleformat{\part}[display]{\normalfont\LARGE\bfseries}{}{0em}{\colorbox{gray!100}{\fbox{\parbox{\textwidth}{\parbox[t]{\leer}{\hspace{\ler}\thepart}\parbox[t]{\dimexpr\textwidth-\leer\relax}{\strut#1\strut}}}}}[ \gdef\thisparttitle{#1}\gdef\thispartnumber{\thepart}\global\toggletrue{Fpart}\global\toggletrue{Tpart}\global\toggletrue{Lpart} ] \titleformat{name=\part, numberless}[display]{\normalfont\LARGE\bfseries}{}{0em}{\colorbox{gray!100}{\fbox{\parbox{\textwidth}{\hspace{\ler}\strut#1\strut}}}}[ \gdef\thisparttitle{#1}\gdef\thispartnumber{\thepart}\global\toggletrue{Fpart}\global\toggletrue{Tpart}\global\toggletrue{Lpart} ] \titlespacing{\part}{0pt}{0pt}{1em}

\titleclass{\chapter}{straight} \titleformat{\chapter}[display]{\normalfont\Large\bfseries}{}{0em}{\colorbox{gray!75}{\fbox{\parbox{\textwidth}{\parbox[t]{\leer}{\hspace{\ler}\thechapter}\parbox[t]{\dimexpr\textwidth-\leer\relax}{\strut#1\strut}}}}}[ \gdef\thischaptertitle{#1}\gdef\thischapternumber{\thechapter}\global\toggletrue{noFigs}\global\toggletrue{noTabs}\global\toggletrue{noList} ] \titleformat{name=\chapter, numberless}[display]{\normalfont\Large\bfseries}{}{0em}{\colorbox{gray!75}{\fbox{\parbox{\textwidth}{\hspace{\ler}\strut#1\strut}}}}[ \gdef\thischaptertitle{#1}\gdef\thischapternumber{\thechapter}\global\toggletrue{noFigs}\global\toggletrue{noTabs}\global\toggletrue{noList} ] \titlespacing{\chapter}{0pt}{0pt}{1em}

\titleformat{\section}[display]{\normalfont\large\bfseries}{}{0em}{\colorbox{gray!50}{\fbox{\parbox{\textwidth}{\parbox[t]{\leer}{\hspace{\ler}\thesection}\parbox[t]{\dimexpr\textwidth-\leer\relax}{\strut#1\strut}}}}}[] \titleformat{name=\section, numberless}[display]{\normalfont\large\bfseries}{}{0em}{\colorbox{gray!100}{\fbox{\parbox{\textwidth}{\hspace{\ler}\strut#1\strut}}}}[] \titleformat{\subsection}[display]{\normalfont\normalsize\bfseries}{}{0em}{\colorbox{gray!25}{\fbox{\parbox{\textwidth}{\parbox[t]{\leer}{\hspace{\ler}\thesubsection}\parbox[t]{\dimexpr\textwidth-\leer\relax}{\strut#1\strut}}}}}[] \titleformat{name=\subsection, numberless}[display]{\normalfont\LARGE\bfseries}{}{0em}{\colorbox{gray!25}{\fbox{\parbox{\textwidth}{\hspace{\ler}\strut#1\strut}}}}[] \titleformat{\subsubsection}[display]{\normalfont\normalsize\bfseries}{}{0em}{\colorbox{gray!25}{\fbox{\parbox{\textwidth}{\parbox[t]{\leer}{\hspace{\ler}\thesubsubsection}\parbox[t]{\dimexpr\textwidth-\leer\relax}{\strut#1\strut}}}}}[] \titleformat{name=\subsubsection, numberless}[display]{\normalfont\normalsize\bfseries}{}{0em}{\colorbox{gray!25}{\fbox{\parbox{\textwidth}{\hspace{\ler}\strut#1\strut}}}}[]

\usepackage{hyperref}

\AtBeginDocument{ \AtBeginEnvironment{figure}{ \iftoggle{noFigs}{ \iftoggle{Fpart}{ \addtocontents{lof}{\protect\contentsline{part}{\thepart\hspace{1em}\thisparttitle}{}{part.\theHpart}} \global\togglefalse{Fpart} }{} \addtocontents{lof}{\protect\contentsline{chapter}{\thechapter\hspace{1em}\thischaptertitle}{}{chapter\thechapter}} \global\togglefalse{noFigs} }{} } \AtBeginEnvironment{table}{ \iftoggle{noTabs}{ \iftoggle{Tpart}{ \addtocontents{lot}{\protect\contentsline{part}{\thepart\hspace{1em}\thisparttitle}{}{part.\theHpart}} \global\togglefalse{Tpart} }{} \addtocontents{lot}{\protect\contentsline{chapter}{\thechapter\hspace{1em}\thischaptertitle}{}{chapter\thechapter}} \global\togglefalse{noTabs} }{} } \AtBeginEnvironment{lists}{ \iftoggle{noList}{ \iftoggle{Lpart}{ \addtocontents{lol}{\protect\contentsline{part}{\thepart\hspace{1em}\thisparttitle}{}{part.\theHpart}} \global\togglefalse{Tpart} }{} \addtocontents{lol}{\protect\contentsline{chapter}{\thechapter\hspace{1em}\thischaptertitle}{}{chapter.\theHchapter}} \global\togglefalse{noList} }{} } } \usepackage{xurl}

%Change Language \usepackage[ngerman]{babel}

%Absolutly no clue \usepackage[utf8]{inputenc} \usepackage[T1]{fontenc}

%For Pictures and Graphics \usepackage{graphicx} \usepackage{float} \usepackage{wrapfig}

%For Tables \usepackage{tabularx}

\begin{document} \frontmatter \chapter{Testchapter} Some Formula and Text. \mainmatter \tableofcontents \part{Testpart} \chapter{Testchapter} Some Formula and Text. \part{Testpart} \chapter{Testchapter} \section{Testsection} Some Formula and Text. \section{Testsection} Some Formula and Text. \section{Testsection} Some Formula and Text. \subsection{Testsubsection} Some Formula and Text. \section{Testsection} Some Formula and Text. \subsection{Testsubsection} Some Formula and Text. \subsection{Testsubsection} Some Formula and Text. \chapter{Testchapter} \chapter{Testchapter} \section{Testsection} Some Formula and Text. \section{Testsection} Some Formula and Text. \part{Testpart} \chapter{Testchapter} Some Formula and Text. \section{Testsection} Some Formula and Text. \chapter{Testchapter} \section{Testsection} \begin{figure}[H] \caption{Testfigure} \end{figure} \chapter{Testchapter} \section{Testsection} Some Formula and Text. \begin{table}[H] \caption{Testtable} \end{table} \chapter{Testchapter} \section{Testsection} \begin{figure}[H] \caption{Testfigure} \end{figure} \begin{figure}[H] \caption{Testfigure} \end{figure} \newpage \backmatter \listoftables \addcontentsline{toc}{chapter}{\listtablename} \newpage \listoffigures \addcontentsline{toc}{chapter}{\listfigurename} \end{document}

So thanks again to @cfr for helping me!