titlesec 干扰 addcontentsline/addtocontents (将部分和章节添加到 lot 和 lof)

titlesec 干扰 addcontentsline/addtocontents (将部分和章节添加到 lot 和 lof)

对于我来说lotlof我正在使用我发现的东西这里,这样parts 和chapters 就会显示在其中(lotlof)。而对于我的标题(Part,,Chapter... Section),我使用的是titlesec,所以它们看起来(在我看来)比我在书籍类中获得的默认效果更好。

现在我的问题是,如果我单独使用这两个,它们工作正常,但同时使用时则不行。titlesec使用时,它会以某种方式干扰addcontentsline/ addtocontents,因此parts 和chapters 不再显示在lot或中lof

我的 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}

lot应该lof看起来像这样: 在此处输入图片描述

但它看起来像这样(注释掉\iffalse\fi): 在此处输入图片描述

我对 LaTeX 还比较陌生,如果我的英语不好,请见谅。

如果这个问题能解决,那将对我有很大帮助,谢谢。

答案1

好吧,正如 @cfr 在评论中指出的那样,不可能同时用两种不同的方式修补命令。这意味着为了拥有漂亮的标题和将parts/ chapters放入lot/中lof,我不得不以某种方式摆脱 或titlesecxpatch因为我实际上喜欢使用 ,所以titlesec我决定放弃xpatch。因此,我将 的修补xpatch放入<after code>的部分titleformat。正如我在评论中指出的那样,这出人意料地有效!

所以我的解决方案是:

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

所以再次感谢@cfr 对我的帮助!

相关内容