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:

But it looks like this (comment out \iffalse and \fi):

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.
titletocroute and keeptitlesecor dumptitlesecand keep your own customisation of the commandstitlesecchanges. You can't take a non-titlesecgroup of patches and expect them to work on commandstitlesecis already patching. Personally, I systematically avoidtitlesecand 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.titlesecdoes not play well with others! – cfr Feb 18 '24 at 07:24xpatchthing and include the patching intotitlesec. I actually got it to work, by putting the patching in the<after code>part fromtitleformat. 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