删除或更改目录前的零

删除或更改目录前的零

我想添加图表列表(Lof)在我的目录中,标题内有此代码:

\usepackage{tocbibind}
\renewcommand*{\tableofcontents}{%
  \begingroup
  \tocchapter
  \tocfile{\contentsname}{toc}
  \endgroup
}
\renewcommand*{\listoffigures}{%
  \begingroup
  \tocchapter
  \tocfile{\listfigurename}{lof}
  \endgroup
}

我的主文件如下所示:

\input{header}

\begin{document}

\makeatletter
\def\l@lstlisting#1#2{\@dottedtocline{1}{1.5em}{4em}{#1}{#2}}
\makeatother



\let\oldsection\section
\let\oldsubsection\subsection
\let\oldchapter\chapter

\renewcommand{\section}{\counterwithin{lstlisting}{section}\oldsection}
\renewcommand{\subsection}{\counterwithin{lstlisting}{subsection}\oldsubsection}
\renewcommand{\chapter}{\counterwithin{lstlisting}{chapter}\oldchapter}
\frontmatter


\include{kapitel/titelseite_Standard}

\include{kapitel/erklaerung}


\tableofcontents


\mainmatter
\input{kapitel/Einleitung}


\input{kapitel/Grundlagen}
\input{kapitel/Programmierumgebungen}
\input{kapitel/Analyse}
\input{kapitel/Implementierung}
\input{kapitel/Test}
\input{kapitel/Fazit}
\input{kapitel/anhang}

\listoffigures
\lstlistoflistings
\bibliography{biblio/biblio}

在此处输入图片描述

现在我的目录被视为带有标题的章节,并且标题前面是零。我怎样才能将零改为一。我的documentclassscrbook

感谢您的帮助。

答案1

该命令\tocchapter允许\tableofcontents使用\chapter而不是\chapter*标题,因此这将导致0 Contents

然而,这确实是一个坏的想法\tableofcontents自从listings劫持以来就发生改变\tableofcontents并且将使用基本相同的代码\listoflistings

在我看来,根本\tableofcontents不应该重新定义,因为tocbibind已经将 LoF、ToC 等添加到 ToC(如果没有用等阻止的notoc话)。

\counterwithin之前的语句没有\section必要,因为\counterwithin{lstlisting}{subsection}是足够的,因为在和subsection的计数器重置列表中,所以重置任何一个计数器也会重置。sectionchapterlstlisting

listingslstlisting仅在处定义计数器,\AtBeginDocument因此在此计数器\counterwithin之前不能使用。\begin{document}

\documentclass{book}

\usepackage{listings}
\usepackage{chngcntr}
\usepackage{tocbibind}


% Actually not really needed!
% \AtBeginDocument{
%   \renewcommand*{\tableofcontents}{%
%    \begingroup
%    \tocfile{\contentsname}{toc}
%    \endgroup
%  }
%}



%\input{header}

\makeatletter
\AtBeginDocument{%
  \counterwithin{lstlisting}{subsection}
  \def\l@lstlisting#1#2{\@dottedtocline{1}{1.5em}{4em}{#1}{#2}}
}
\makeatother

\begin{document}


\frontmatter


%\include{kapitel/titelseite_Standard}

%\include{kapitel/erklaerung}


\tableofcontents


\mainmatter

\chapter[foo]{stuff}
\chapter{Foo stuff}

\section{Foo section}

\subsection{Foo subsection}

\begin{lstlisting}[caption=Foo]
Boo
\end{lstlisting}

\subsection{Other foo subsection}

\begin{lstlisting}[caption=Foo again]
Boo
\end{lstlisting}

\section{Other foo section}

\begin{lstlisting}[caption=Foo again]
Boo
\end{lstlisting}

\begin{lstlisting}[caption=Foo again \thesection]
Boo
\end{lstlisting}


\chapter{Other foo chapter}


\begin{lstlisting}[caption=Foo again]
Boo
\end{lstlisting}



%\input{kapitel/Einleitung}


%\input{kapitel/Grundlagen}
%\input{kapitel/Programmierumgebungen}
%\input{kapitel/Analyse}
%\input{kapitel/Implementierung}
%\input{kapitel/Test}
%\input{kapitel/Fazit}
%\input{kapitel/anhang}

\listoffigures
\lstlistoflistings
\bibliography{biblio/biblio}

\end{document}

在此处输入图片描述

答案2

使用 KOMA-Script 类时scrbook无需加载包tocbibind

列表的目录条目可以通过选项完成listof=totoc。如果您确实想要目录中的目录条目,您可以使用\setuptoc{toc}{totoc}

确保\lstlistoflistings尊重KOMA-Script选项listof加载包的设置scrhack

\documentclass[
  listof=totoc
]{scrbook}
\setuptoc{toc}{totoc}
\usepackage{scrhack}
\usepackage{listings}
\usepackage{chngcntr}

\DeclareTOCStyleEntry[
  level=1,
  indent=1.5em,
  numwidth=4em
]{tocline}{lstlisting}

\usepackage{xpatch}
\makeatletter
\AtBeginDocument{%
  \counterwithin{lstlisting}{subsection}%
  \xpatchcmd{\fnum@lstlisting}{\thelstlisting}{\thelstlisting\autodot}{}{\PatchFailed}%
}
\makeatother


\begin{document}
\frontmatter
\tableofcontents
\mainmatter
\chapter[foo]{stuff}
\chapter{Foo stuff}
\section{Foo section}
\subsection{Foo subsection}
\begin{lstlisting}[caption={A Listing}]
Boo
\end{lstlisting}
\listoffigures
\lstlistoflistings
\end{document}

在此处输入图片描述


如果你真的想要列表的特殊编号,你可以使用

\documentclass[
  listof=totoc
]{scrbook}
\setuptoc{toc}{totoc}
\usepackage{scrhack}
\usepackage{listings}
\usepackage{chngcntr}

\DeclareTOCStyleEntry[
  level=1,
  indent=1.5em,
  numwidth=4em
]{tocline}{lstlisting}

\usepackage{xpatch}
\makeatletter
\AtBeginDocument{%
  \counterwithin*{lstlisting}{subsection}%
  \renewcommand\thelstlisting{%
    \ifnum\value{section}=0
      \thechapter
    \else
      \ifnum\value{subsection}=0
        \thesection
      \else
        \thesubsection
      \fi
    \fi
    .\arabic{lstlisting}%
  }%
  \xpatchcmd{\fnum@lstlisting}{\thelstlisting}{\thelstlisting\autodot}{}{\PatchFailed}%
}
\makeatother

\begin{document}
\frontmatter
\tableofcontents
\mainmatter
\chapter[foo]{stuff}
\begin{lstlisting}[caption={A Listing in Chapter 1}]
  Boo
\end{lstlisting}
\chapter{Foo stuff}
\begin{lstlisting}[caption={A Listing in Chapter 2}]
  Boo
\end{lstlisting}
\section{Foo section}
\begin{lstlisting}[caption={A Listing in Section 2.1}]
  Boo
\end{lstlisting}
\subsection{Foo subsection}
\begin{lstlisting}[caption={A Listing in Subsection 2.1.1}]
  Boo
\end{lstlisting}
\listoffigures
\lstlistoflistings
\end{document}

在此处输入图片描述

相关内容