将部分和章节标题添加到带有格式和换行功能的图表列表中

将部分和章节标题添加到带有格式和换行功能的图表列表中

在目录样式的图表列表中,我想将这些问题的出现结合起来。

图表列表中的部分标题?

是否在带有 titletoc 的图表列表中包含章节?

部分标题

章节标题

本章图片列表

章节标题

本章图片列表

部分标题

章节标题

本章图片列表

章节标题

本章图片列表

我尝试过合并各部分,但在下面的 MWE 中,我似乎没有包含各部分标题,而且我似乎无法更改这些新添加标题的格式。我需要在它们之间添加一些空间,使它们与我引用的图表的标题有更明显的不同吗?

\documentclass{report}
    \usepackage{etoolbox}  % or xpatch
    \usepackage{bookmark}
    \usepackage{hyperref}


\makeatletter

    % initial definitions of the part info (name and number)
    \def\thisparttitle{}\def\thispartnumber{}
    \def\thischaptertitle{}\def\thischapternumber{}

    \newtoggle{noFigs}
    \newtoggle{noTabs}

    \apptocmd{\@chapter}{\gdef\thischaptertitle{#1}\gdef\thischapternumber{\thechapter}\global\toggletrue{noFigs}}{}{}
    \apptocmd{\@part}{\gdef\thisparttitle{#1}\gdef\thispartnumber{\thepart}\global\toggletrue{noFigs}\global\toggletrue{noTabs}}{}{}

    % the figure environment does the job: the first time it is used after a \chapter command, it writes the information of the chapter to the LoF
    \AtBeginDocument{%
        \AtBeginEnvironment{figure}{%
            \iftoggle{noFigs}{
                \addtocontents{lof}{\protect\contentsline{part}%
                    {\protect\numberline{\thisparttitle}}{}{3.2em} }
                \addtocontents{lof}{\protect\contentsline {chapter}%
                    {\protect\numberline {\thischapternumber} {\thischaptertitle}}{}{} }
                \global\togglefalse{noFigs}
            }{}
        }%
        \AtBeginEnvironment{table}{%
            \iftoggle{noTabs}{
                \addtocontents{lot}{\protect\contentsline{part}%
                    {\protect\numberline{\thisparttitle}}{}{} }
                \addtocontents{lot}{\protect\contentsline {chapter}%
                    {\protect\numberline {\thischapternumber} {\thischaptertitle}}{}{} }
                \global\togglefalse{noTabs}
            }{}
        }%
    }


\makeatother

\begin{document}

    \tableofcontents
    \listoffigures


    \part{The first}

    \chapter{Introduction with no Figures}

    \chapter{Test Chapter with Figures}
    \begin{figure}
        \caption{caption text}
    \end{figure}
    \begin{figure}
        \caption{caption text}
    \end{figure}

    \chapter{Test Chapter with no Figures}

    \part{The second}

    \chapter{Another Test Chapter with Figures}
    \begin{figure}
        \caption{caption text}
    \end{figure}
    \begin{figure}
        \caption{caption text}
    \end{figure}
    \begin{figure}
        \caption{caption text}
    \end{figure}
    \begin{figure}
        \caption{caption text}
    \end{figure}
    \begin{figure}
        \caption{caption text}
    \end{figure}

\end{document}

更新

如果我用这个替换下面解决方案的文档块:

\begin{document}

\tableofcontents
\listoffigures


\part{The first}

\chapter{Introduction with no Figures}

\chapter{Test Chapter with Figures}
    \begin{figure}
        \caption{caption text}
    \end{figure}
    \begin{figure}
        \caption{caption text}
    \end{figure}

\chapter{Test Chapter with no Figures}

\part{The second}

\chapter{Look no figures here either}

\chapter{Another Test Chapter with Figures}
    \begin{figure}
        \caption{caption text}
    \end{figure}

\chapter{Another in the same part}
    \begin{figure}
        \caption{caption text}
    \end{figure}
    \begin{figure}
        \caption{caption text}
    \end{figure}
    \begin{figure}
        \caption{caption text}
    \end{figure}
    \begin{figure}
        \caption{caption text}
    \end{figure}

\end{document}

然后我得到:

LOF 快照

即使章节是连续的(第 4 和第 5 章以蓝色圈出以突出显示),并且没有穿插缺少图表的章节,也会重复部分标题。

答案1

\contentsline内容在 OP 的文档中使用错误,即错误放置了{}分隔符。

例如

 \addtocontents{lof}{\protect\contentsline{part}%
                    {\protect\numberline{\thisparttitle}}{}{3.2em} }

尝试以部分标题作为数字来写入\numberline,省略标题并将页码设置为,3.2em实际上是长度。

一个正确的方法是

\addtocontents{lof}{%
\protect\contentsline{part}{
  \protect\numberline{\thispartnumber}\thisparttitle}{% page number group -- empty here 
}% end of \contentsline
}% end of \addtocontents

章节标题和也是如此LoT

\documentclass{report}
\usepackage{etoolbox}  % or xpatch

\makeatletter

    % initial definitions of the part info (name and number)
\def\thisparttitle{}\def\thispartnumber{}
\def\thischaptertitle{}\def\thischapternumber{}

\newtoggle{noFigs}
\newtoggle{noTabs}

\apptocmd{\@chapter}{\gdef\thischaptertitle{#1}\gdef\thischapternumber{\thechapter}\global\toggletrue{noFigs}}{}{}
\apptocmd{\@part}{\gdef\thisparttitle{#1}\gdef\thispartnumber{\thepart}\global\toggletrue{noFigs}\global\toggletrue{noTabs}}{}{}

% the figure environment does the job: the first time it is used after a \chapter command, it writes the information of the chapter to the LoF
\AtBeginDocument{%
    \AtBeginEnvironment{figure}{%
        \iftoggle{noFigs}{%
          \addtocontents{lof}{\protect\contentsline{part}{\protect\numberline{\thispartnumber}\thisparttitle}{}}%
          \addtocontents{lof}{\protect\contentsline{chapter}{\protect\numberline{\thischapternumber}\thischaptertitle}{}}%
          \global\togglefalse{noFigs}%
        }{}
    }%
    \AtBeginEnvironment{table}{%
        \iftoggle{noTabs}{%
          \addtocontents{lot}{\protect\contentsline{part}{\protect\numberline{\thispartnumber}\thisparttitle}{}}%
          \addtocontents{lot}{\protect\contentsline{chapter}{\protect\numberline{\thischapternumber}\thischaptertitle}{}}%
          \global\togglefalse{noTabs}%
        }{}
      }%
}


\makeatother

\begin{document}

\tableofcontents
\listoffigures


\part{The first}

\chapter{Introduction with no Figures}

\chapter{Test Chapter with Figures}
\begin{figure}
    \caption{caption text}
\end{figure}
\begin{figure}
    \caption{caption text}
\end{figure}

\chapter{Test Chapter with no Figures}

\part{The second}

\chapter{Another Test Chapter with Figures}
\begin{figure}
    \caption{caption text}
\end{figure}
\begin{figure}
    \caption{caption text}
\end{figure}
\begin{figure}
    \caption{caption text}
\end{figure}
\begin{figure}
    \caption{caption text}
\end{figure}
\begin{figure}
    \caption{caption text}
\end{figure}

\end{document}

在此处输入图片描述

编辑

现在hyperref支持,但不显示章节和部分的页码。(请注意:由于切换样式,如果图形不在章节中,则此功能不起作用!)更好的方法是询问图形编号。

\documentclass{report}

\usepackage{xpatch}


\makeatletter

% initial definitions of the part info (name and number)
\def\thisparttitle{}\def\thispartnumber{}
\def\thischaptertitle{}\def\thischapternumber{}

\newtoggle{noFigs}
\newtoggle{noTabs}
\newtoggle{newpart}


\xpatchcmd{\@part}{%
  \addcontentsline{toc}{part}{\thepart\hspace{1em}#1}%
}{%
  \addcontentsline{toc}{part}{\thepart\hspace{1em}#1}%
  \xdef\thisparttitle{#1}%    Evaluate the parameter #1
  \global\togglefalse{noFigs}%
  \global\togglefalse{noTabs}%
  \global\toggletrue{newpart}%
}{\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}%  Evaluate the parameter #1
  \global\togglefalse{noFigs}%
  \global\togglefalse{noTabs}%
}{\typeout{Patch success}}{\typeout{Patch failure}}
\makeatother



\usepackage{hyperref}

\AtBeginDocument{%
  \AtBeginEnvironment{figure}{%
    \nottoggle{noFigs}{%
      \iftoggle{newpart}{%
      \addtocontents{lof}{\protect\contentsline{part}{\thepart\hspace{1em}\thisparttitle}{}{part.\theHpart}}%
      \global\togglefalse{newpart}%
      }{}%
      \addtocontents{lof}{\protect\contentsline{chapter}{\protect\numberline{\thechapter}\thischaptertitle}{}{chapter.\theHchapter}}%
      \global\toggletrue{noFigs}
    }{}
  }

  \AtBeginEnvironment{table}{%
    \nottoggle{noTabs}{%
      \iftoggle{newpart}{%
      \addtocontents{lot}{\protect\contentsline{part}{\thepart\hspace{1em}\thisparttitle}{}{part.\theHpart}}%
      \global\togglefalse{newpart}%
      }{}%
      \addtocontents{lot}{\protect\contentsline{chapter}{\protect\numberline{\thechapter}\thischaptertitle}{}{chapter.\theHchapter}}%
      \global\toggletrue{noTabs}%
    }{}
  }
}


\begin{document}

\tableofcontents
\listoffigures
\listoftables

\part{The first}

\chapter{Introduction with no Figures}

\chapter{Test Chapter with Figures}
\begin{figure}
    \caption{caption text}
\end{figure}
\begin{figure}
    \caption{caption text}
\end{figure}

\chapter{Test Chapter with no Figures}

\part{The second}

\chapter{Look no figures here either}


\chapter{Another Test Chapter with Figures}
\begin{figure}
    \caption{caption text from chapter \thechapter}
\end{figure}

\begin{figure}
    \caption{caption text from chapter \thechapter}
\end{figure}
\begin{figure}
    \caption{caption text from chapter \thechapter}
\end{figure}
\begin{figure}
    \caption{caption text from chapter \thechapter}
\end{figure}
\begin{figure}
    \caption{caption text from chapter \thechapter}
\end{figure}
\begin{figure}
    \caption{caption text from chapter \thechapter}
\end{figure}
\begin{figure}
    \caption{caption text from chapter \thechapter}
\end{figure}

%\chapter{Another chapter, without figures}


\chapter{Another in the same part}
    \begin{figure}
        \caption{caption text}
    \end{figure}
    \begin{figure}
        \caption{caption text}
    \end{figure}
    \begin{figure}
        \caption{caption text}
    \end{figure}
    \begin{figure}
        \caption{caption text}
    \end{figure}




\part{Without figs}

\chapter{A chapter with a table}

\begin{table}
\caption{Some table}
\end{table}

\end{document}

在此处输入图片描述

相关内容