按章节名称对列表进行分组

按章节名称对列表进行分组

我想将章​​节名称写入我的 LOF、LOT 和 LOL,以使其更易于阅读,并发现以下代码这里

%%%%% from http://www.komascript.de/comment/5070#comment-5070 (Markus Kohm)
\makeatletter
\let\chapterhas@original@addcontentsline\addcontentsline
\renewcommand*{\addcontentsline}[1]{%
  \immediate\write\@auxout{\string\chapterhas{\thechapter}{#1}}%
  \chapterhas@original@addcontentsline{#1}%
}
\newcommand*{\chapterhas}[2]{%
  \global\@namedef{chapterhas@#1@#2}{true}%
}
\renewcommand*{\addchaptertocentry}[2]{%
  \addtocentrydefault{chapter}{#1}{#2}%
  \if@chaptertolists
    \doforeachtocfile{%
      \iftocfeature{\@currext}{chapteratlist}{%
        \ifundefinedorrelax{chapterhas@\thechapter @\@currext}{%
        }{%
          \addxcontentsline{\@currext}{chapteratlist}[{#1}]{#2}%
        }%
      }{}%
    }%
    \@ifundefined{float@addtolists}{}{\scr@float@addtolists@warning}%
  \fi
}
\makeatother
%%%%

它对 LOF 和 LOT 运行良好,但对 LOL 不起作用。我使用该包listings来生成我的列表,这会是个问题吗?这是一个完整的示例,显示了问题:

\documentclass[12pt,headsepline,toc=bibliography,toc=listof,chapteratlists=entry]{scrbook}

\AfterTOCHead[lof]{\addtokomafont{chapterentrypagenumber}{\nullfont}}
\AfterTOCHead[lot]{\addtokomafont{chapterentrypagenumber}{\nullfont}}

%%%%% from http://www.komascript.de/comment/5070#comment-5070 (Markus Kohm)
\makeatletter
\let\chapterhas@original@addcontentsline\addcontentsline
\renewcommand*{\addcontentsline}[1]{%
  \immediate\write\@auxout{\string\chapterhas{\thechapter}{#1}}%
  \chapterhas@original@addcontentsline{#1}%
}
\newcommand*{\chapterhas}[2]{%
  \global\@namedef{chapterhas@#1@#2}{true}%
}
\renewcommand*{\addchaptertocentry}[2]{%
  \addtocentrydefault{chapter}{#1}{#2}%
  \if@chaptertolists
    \doforeachtocfile{%
      \iftocfeature{\@currext}{chapteratlist}{%
        \ifundefinedorrelax{chapterhas@\thechapter @\@currext}{%
        }{%
          \addxcontentsline{\@currext}{chapteratlist}[{#1}]{#2}%
        }%
      }{}%
    }%
    \@ifundefined{float@addtolists}{}{\scr@float@addtolists@warning}%
  \fi
}
\makeatother
%%%%

\usepackage{listings}

\begin{document}

\tableofcontents
\listoffigures
\listoftables
\lstlistoflistings

\chapter{Chapter 1}
\begin{figure}
\caption{Figure 1}
\end{figure}
\begin{figure}
\caption{Figure 2}
\end{figure}

\chapter{Chapter 2}

\chapter{Chapter 3}
\begin{figure}
\caption{Figure 3}
\end{figure}
\begin{table}
\caption{Table 1}
\end{table}

\begin{lstlisting}[caption={Listing 1}]
\end{lstlisting}
\begin{lstlisting}[caption={Listing 2}]
\end{lstlisting}
\begin{lstlisting}[caption={Listing 3}]
\end{lstlisting}

\end{document}

工作表 非功能性清单

简而言之:如何让章节名称出现在列表列表中,就像在表格列表中一样?

答案1

在您的代码中,会发生以下情况:KOMA 类宏\doforeachtocfile调用所有已注册的列表文件,检查它们是否具有属性chapteratlist,然后将部分条目写入相应的列表文件。listings但是,该包不会使用 KOMA 类注册列表列表。但您可以轻松提供这一点。只需添加:

  • \addtotoclist{lol}将清单列表添加到清单列表中,
  • \setuptoc{lol}{chapteratlist}将该房产添加chapteratlist到列表。
\documentclass[12pt,headsepline,toc=bibliography,toc=listof,chapteratlists=entry]{scrbook}

\AfterTOCHead[lof]{\addtokomafont{chapterentrypagenumber}{\nullfont}}
\AfterTOCHead[lot]{\addtokomafont{chapterentrypagenumber}{\nullfont}}

\addtotoclist{lol}
\setuptoc{lol}{chapteratlist}

%%%%% from http://www.komascript.de/comment/5070#comment-5070 (Markus Kohm)
\makeatletter
\let\chapterhas@original@addcontentsline\addcontentsline
\renewcommand*{\addcontentsline}[1]{%
  \immediate\write\@auxout{\string\chapterhas{\thechapter}{#1}}%
  \chapterhas@original@addcontentsline{#1}%
}
\newcommand*{\chapterhas}[2]{%
  \global\@namedef{chapterhas@#1@#2}{true}%
}
\renewcommand*{\addchaptertocentry}[2]{%
  \addtocentrydefault{chapter}{#1}{#2}%
  \if@chaptertolists
    \doforeachtocfile{%
      \iftocfeature{\@currext}{chapteratlist}{%
        \ifundefinedorrelax{chapterhas@\thechapter @\@currext}{%
        }{%
          \addxcontentsline{\@currext}{chapteratlist}[{#1}]{#2}%
        }%
      }{}%
    }%
    \@ifundefined{float@addtolists}{}{\scr@float@addtolists@warning}%
  \fi
}
\makeatother
%%%%

\usepackage{listings}

\begin{document}

\tableofcontents
\listoffigures
\listoftables
\lstlistoflistings

\chapter{Chapter 1}
\begin{figure}
\caption{Figure 1}
\end{figure}
\begin{figure}
\caption{Figure 2}
\end{figure}

\chapter{Chapter 2}

\chapter{Chapter 3}
\begin{figure}
\caption{Figure 3}
\end{figure}
\begin{table}
\caption{Table 1}
\end{table}

\begin{lstlisting}[caption={Listing 1}]
\end{lstlisting}
\begin{lstlisting}[caption={Listing 2}]
\end{lstlisting}
\begin{lstlisting}[caption={Listing 3}]
\end{lstlisting}

\end{document}

在此处输入图片描述

答案2

KOMA-Script 还提供了scrhack改进其他几个软件包的软件包,例如,listings要使用的补丁tocbasic。如果您使用此软件包,chapteratlists=entry则自动适用于\listoflistings

\documentclass[12pt,headsepline,toc=bibliography,toc=listof,chapteratlists=entry]{scrbook}

\AfterTOCHead[lof]{\addtokomafont{chapterentrypagenumber}{\nullfont}}
\AfterTOCHead[lot]{\addtokomafont{chapterentrypagenumber}{\nullfont}}

%%%%% from http://www.komascript.de/comment/5070#comment-5070 (Markus Kohm)
\makeatletter
\let\chapterhas@original@addcontentsline\addcontentsline
\renewcommand*{\addcontentsline}[1]{%
  \immediate\write\@auxout{\string\chapterhas{\thechapter}{#1}}%
  \chapterhas@original@addcontentsline{#1}%
}
\newcommand*{\chapterhas}[2]{%
  \global\@namedef{chapterhas@#1@#2}{true}%
}
\renewcommand*{\addchaptertocentry}[2]{%
  \addtocentrydefault{chapter}{#1}{#2}%
  \if@chaptertolists
    \doforeachtocfile{%
      \iftocfeature{\@currext}{chapteratlist}{%
        \ifundefinedorrelax{chapterhas@\thechapter @\@currext}{%
        }{%
          \addxcontentsline{\@currext}{chapteratlist}[{#1}]{#2}%
        }%
      }{}%
    }%
    \@ifundefined{float@addtolists}{}{\scr@float@addtolists@warning}%
  \fi
}
\makeatother
%%%%

\usepackage{listings}
\usepackage{scrhack}% use tocbasic for \listoflistings (and more)
\AfterTOCHead[lol]{\addtokomafont{chapterentrypagenumber}{\nullfont}}

\begin{document}

\tableofcontents
\listoffigures
\listoftables
\lstlistoflistings

\chapter{Chapter 1}
\begin{figure}
\caption{Figure 1}
\end{figure}
\begin{figure}
\caption{Figure 2}
\end{figure}

\chapter{Chapter 2}

\chapter{Chapter 3}
\begin{figure}
\caption{Figure 3}
\end{figure}
\begin{table}
\caption{Table 1}
\end{table}

\begin{lstlisting}[caption={Listing 1}]
\end{lstlisting}
\begin{lstlisting}[caption={Listing 2}]
\end{lstlisting}
\begin{lstlisting}[caption={Listing 3}]
\end{lstlisting}

\end{document}

在此处输入图片描述

相关内容