我正在尝试为每个章节创建一个目录(请参阅下面的 MNWE)。当表格长度超过页面长度并应该中断时,就会出现问题。对于设置,我使用 tcolorbox 库,它具有大量自定义选项。我使用了一个应该breakable
可以确保这一点的选项,但不幸的是它没有。见下图。
使用 可将内容插入章节序言中\setchapterpreamble
。这非常有用 - 我使用的是双列格式,类选项为 twocolumn,因为标题和序言始终设置为单列布局。序言可能不能超过两页。
如果您对如何使用不同的解决方案实现相同的效果有任何其他想法,我会很高兴听取您的建议。
\documentclass[chapterprefix=on, twocolumn, a5paper]{scrbook}
\usepackage{xpatch}
\usepackage{blindtext}
\usepackage{ifthen} % The package's basic command is \ifthenelse
% https://tex.stackexchange.com/questions/669008/
\usepackage[most]{tcolorbox}
\makeatletter
\newif\ifusechaptertoc % Switch to tell \addtocentrydefault to not only make entries
% to the toc-file but also to the current section-toc-file
\newcommand*{\chaptertoc}[2][\thechapter]{ % new command to generate and show a chapter toc
\usechaptertoctrue % switch on chapter-toc-entries
\edef\ext@chaptoc{tcc#1} % extension of the chapter-toc-file, e.g., tcc1
\DeclareNewTOC{\ext@chaptoc} % declare a new toc file
%\addsec*{Contents} % header of the chaptertoc
\setchapterpreamble{%
\begin{tcolorbox}[
breakable
]
\value{tocdepth}=\subsubsectiontocdepth % we want entries down to subsection
\listoftoc*{\ext@chaptoc} % show the toc without header
\end{tcolorbox} %
\par\nobreak\noindent\hrulefill\par % \par\bigskip\nobreak\noindent\hrulefill\par
\bigskip\noindent\ignorespaces % add some vertical space after the toc and do not indent
} % the following text
}
\xapptocmd\addtocentrydefault{ % patch the KOMA-Script's generic toc entry generator
\ifusechaptertoc % if chapter toc entries should be generated
\Ifstr{#1}{chapter}{}
{\expandafter\tocbasic@addxcontentsline
\expandafter{\ext@chaptoc}{#1}{#2}{#3}
}% do it
\fi
}{}{}
\xpretocmd\chapter{\usechaptertocfalse}{}{} % automatically switch of chapter toc
% entries at start of every \chapter
\xpretocmd\part{\usechaptertocfalse}{}{} % entries in chapter toc are automatically
% switched off at start of \part
\makeatother
\newcommand\setchaptertoc[1][]{%
\Ifstr{#1}{}
{\AddtoOneTimeDoHook{heading/preinit/chapter}{\chaptertoc}}
{\AddtoOneTimeDoHook{heading/preinit/chapter}{\chaptertoc[#1]}}%
}
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
\setcounter{tocdepth}{\partnumdepth}
\begin{document}
\tableofcontents
\setchaptertoc
\chapter{My chapter}
\blindtext
\section{My section I}
\subsection{My subsection I}
\blindtext
\section{My section II}
\subsection{My subsection I}
\subsubsection{My subsubsection II}
\subsection{My subsection II}
\subsubsection{My subsubsection II}
\subsubsection{My subsubsection III}
\subsection{My subsection III}
\section{My section III}
\section{My section IV}
\subsection{My subsection I}
\subsection{My subsection I}
\subsection{My subsection II}
\section{My section V}
\subsection{My subsection I}
\subsection{My subsection I}
\subsection{My subsection III}
\section{My section V}
\subsection{My subsection I}
\subsection{My subsection I}
\subsection{My subsection III}
\section{My section V}
\section{My section V}
\subsection{My subsection I}
\subsection{My subsection II}
\subsection{My subsection III}
\section{My section V}
\section{My section VI}
\section{My section VII}
\section{My section VIII}
\section{My section IV}
\section{My section V}
\section{My section VI}
\end{document}
答案1
KOMA-Script 使用可选参数\@topnewpage
(内部命令也使用\twocolumn
)在双列文档中打印跨章标题和序言的列。LaTeX 内核为此参数使用一个框,因此不支持在此参数内分页。您需要对每一章的内容使用onecolumn
模式和包。但据我所知仍然不起作用,因为它还为序言使用了一个显式框。multicol
\setchapterpreamble
下面是一个不自动打印章节目录并切换到两栏的示例:
\documentclass[chapterprefix=on,twocolumn,a5paper]{scrbook}
\usepackage{multicol}
\usepackage{xpatch}
\usepackage{blindtext}
\usepackage{ifthen} % The package's basic command is \ifthenelse
% https://tex.stackexchange.com/questions/669008/
\usepackage[most]{tcolorbox}
\makeatletter
\newif\ifusechaptertoc % Switch to tell \addtocentrydefault to not only make entries
% to the toc-file but also to the current section-toc-file
\newcommand*{\chaptertoc}[2][\thechapter]{ % new command to generate and show a chapter toc
\usechaptertoctrue % switch on chapter-toc-entries
\edef\ext@chaptoc{tcc#1} % extension of the chapter-toc-file, e.g., tcc1
\DeclareNewTOC{\ext@chaptoc} % declare a new toc file
%\addsec*{Contents} % header of the chaptertoc
}
\newcommand\printchaptertoc{%
\ifusechaptertoc
\begin{tcolorbox}[
breakable
]
\value{tocdepth}=\subsubsectiontocdepth % we want entries down to subsection
\listoftoc*{\ext@chaptoc} % show the toc without header
\end{tcolorbox} %
\par\nobreak\noindent\hrulefill\par % \par\bigskip\nobreak\noindent\hrulefill\par
\bigskip\noindent\ignorespaces % add some vertical space after the toc and do not indent
\fi
} % the following text
\xapptocmd\addtocentrydefault{ % patch the KOMA-Script's generic toc entry generator
\ifusechaptertoc % if chapter toc entries should be generated
\Ifstr{#1}{chapter}{}
{\expandafter\tocbasic@addxcontentsline
\expandafter{\ext@chaptoc}{#1}{#2}{#3}
}% do it
\fi
}{}{}
\xpretocmd\chapter{\usechaptertocfalse}{}{} % automatically switch of chapter toc
% entries at start of every \chapter
\xpretocmd\part{\usechaptertocfalse}{}{} % entries in chapter toc are automatically
% switched off at start of \part
\makeatother
\newcommand\setchaptertoc[1][]{%
\Ifstr{#1}{}
{\AddtoOneTimeDoHook{heading/preinit/chapter}{\chaptertoc}}
{\AddtoOneTimeDoHook{heading/preinit/chapter}{\chaptertoc[#1]}}%
}
\setcounter{tocdepth}{\partnumdepth}
\begin{document}
\tableofcontents
\onecolumn
\setchaptertoc
\chapter{My chapter}
\printchaptertoc
\begin{multicols}{2}
\blindtext
\section{My section I}
\subsection{My subsection I}
\blindtext
\section{My section II}
\subsection{My subsection I}
\subsubsection{My subsubsection II}
\subsection{My subsection II}
\subsubsection{My subsubsection II}
\subsubsection{My subsubsection III}
\subsection{My subsection III}
\section{My section III}
\section{My section IV}
\subsection{My subsection I}
\subsection{My subsection I}
\subsection{My subsection II}
\section{My section V}
\subsection{My subsection I}
\subsection{My subsection I}
\subsection{My subsection III}
\section{My section V}
\subsection{My subsection I}
\subsection{My subsection I}
\subsection{My subsection III}
\section{My section V}
\section{My section V}
\subsection{My subsection I}
\subsection{My subsection II}
\subsection{My subsection III}
\section{My section V}
\section{My section VI}
\section{My section VII}
\section{My section VIII}
\section{My section IV}
\section{My section V}
\section{My section VI}
\end{multicols}
\end{document}
也许您还可以找到钩子来自动添加\printchaptertoc
,\begin{multicols}{2}
和\end{multicols}
。
注意:multicols
处理类似figure
和table
不同的浮点数。有关更多信息,请参阅手册。