我想获取章节第一节和最后一节的名称,并将其分别打印到每个章节的目录中。因此,我想使用 -package etoc
。在etoc
32.4 节的 -documentation 中描述了一个宏,他们使用它来打印章节的统计信息。我想推导出这一点,因此目录打印如下:
Table of Contents
Chapter 1 Chaptername
<Name of first Section> to <Name of last Section>
Chapter 2 Chaptername
etc
我的知识还不够,我不懂宏。有没有人能帮我,或者我应该放弃这个想法?
\documentclass[
twoside,
12pt,
]{book}
\usepackage{luaotfload}
\usepackage{fontspec}
\usepackage{etoc}
\begin{document}
\tableofcontents
\chapter{Chapter 1}
\section{Section 1}
\section{Section 2}
\section{Section 3}
\chapter{Chapter 1}
\section{Section 4}
\section{Section 5}
\end{document}
答案1
答案已编辑,以处理空章节的情况,并使用\etociffirst
条件etoc 1.08
或更高版本。
\documentclass[
twoside,
12pt,
]{book}
% This is commented out to compile with pdflatex:
% \usepackage{luaotfload}
% \usepackage{fontspec}
\usepackage{etoc}
% here some empty definitions to get a warning in case we
% overwrite already existing macros from class or packages
\newcommand*\firstsectionname {}
\newcommand*\firstsectionnumber {}
\newcommand*\firstsectionpage {}
% auxiliary command, to be executed from within the etoc user-defined
% section style
\newcommand*\storefirstsectiondata {%
\let\firstsectionname \etocthelinkedname
\let\firstsectionnumber\etocthelinkednumber
\let\firstsectionpage \etocthelinkedpage
}%
% A boolean to handle the case of chapters with no sections.
% To handle booleans in a more LaTeX way, use rather etoolbox package.
\newif\ifchapterhadnosections
% The definition of special Table of contents as per Question's request
% standard \tableofcontents not modified and can also be used
% independently
\newcommand*{\mytableofcontents}{%
% our definitions are made inside a temporary group
\begingroup
% chapter style
% \etociffirst conditional requires etoc version 1.08 or later
\etocsetstyle{chapter}
{}
{\etociffirst
{% first chapter, no extra vertical spacing should be added yet.
}
{% not the first chapter, check if previous one had no sections
\ifchapterhadnosections
This chapter contains no sections.\par
\fi
% let's add always some vertical spacing
\addvspace{1em}}%
}
{\noindent\makebox[6em][l]{Chapter~\etocnumber}%
\textbf{\etocname}\par
% boolean which will be set to false by first encountered section
\chapterhadnosectionstrue
}
{\ifchapterhadnosections % this code is executed after the last chapter
This chapter contains no sections.\par
\fi
}
% section style
\etocsetstyle{section}
{\chapterhadnosectionsfalse }
{\etociffirst{\storefirstsectiondata}{}}
{}
{% this piece of the section style is the one executed
% after the last section of the current chapter has been seen
\firstsectionname{} (on page \firstsectionpage)
to
% \etocname, \etocnumber, \etocpage currently contain
% the name, number, page number, of the last section
% of the enveloping chapter
\etocname{} (on page \etocpage)%
\par
}%
%
\etocsetnexttocdepth {section}%
\tableofcontents
\endgroup
}
\usepackage{hyperref}% to test the links
\begin{document}
\mytableofcontents
\chapter{ALPHA}
\section{Section 1}
\clearpage
\section{Section 2}
\clearpage
\section{Section 3}
\chapter{BETA}
\section{Section 4}
\clearpage
\section{Section 5}
\chapter{GAMMA}
\chapter{DELTA}
\section{Section 6}
\clearpage
\section{Section 7}
\chapter{Final chapter (with no sections)}
\end{document}
输出:
先前的答案:
\documentclass[
twoside,
12pt,
]{book}
% commented out to compile with pdflatex
%\usepackage{luaotfload}
%\usepackage{fontspec}
\usepackage{etoc}
\newcommand*\firstsectionname {}
\newcommand*\firstsectionnumber {}
\newcommand*\firstsectionpage {}
\newcommand*\lastsectionname {}
\newcommand*\lastsectionnumber {}
\newcommand*\lastsectionpage {}
\newcommand*\storefirstsectiondata {}
\newcommand*{\mytableofcontents}{%
\begingroup
\etocsetstyle{chapter}
{\etocskipfirstprefix}
{\addvspace{1em}}
{\noindent\makebox[6em][l]{Chapter~\etocnumber}%
\textbf{\etocname}\par}
{}
%
\etocsetstyle{section}
{\def\storefirstsectiondata{%
\let\firstsectionname \etocthelinkedname
\let\firstsectionnumber\etocthelinkednumber
\let\firstsectionpage \etocthelinkedpage
\let\storefirstsectiondata\empty
}%
}
{}
{\storefirstsectiondata
\let\lastsectionname \etocthelinkedname
\let\lastsectionnumber\etocthelinkednumber
\let\lastsectionpage \etocthelinkedpage}
{\firstsectionname{} (on page \firstsectionpage)
to
\lastsectionname{} (on page \lastsectionpage)%
\par
}%
%
\etocsetnexttocdepth {1}%
\tableofcontents
\endgroup
}
\usepackage{hyperref}% to test the links
\begin{document}
\mytableofcontents
\chapter{ALPHA}
\section{Section 1}
\clearpage
\section{Section 2}
\clearpage
\section{Section 3}
\chapter{BETA}
\section{Section 4}
\clearpage
\section{Section 5}
\chapter{GAMMA}
\chapter{DELTA}
\section{Section 6}
\clearpage
\section{Section 7}
\end{document}
输出:
初始答案的不必要的复杂代码(给出与上面相同的输出)。
\documentclass[
twoside,
12pt,
]{book}
% commented out to compile with pdflatex
%\usepackage{luaotfload}
%\usepackage{fontspec}
\usepackage{etoc}
\newcommand*\firstsectionname {}
\newcommand*\firstsectionnumber {}
\newcommand*\firstsectionpage {}
\newcommand*\lastsectionname {}
\newcommand*\lastsectionnumber {}
\newcommand*\lastsectionpage {}
\newcommand*\storefirstsectiondata {}
\newcommand*\previouschaptername {}
\newcommand*\previouschapternumber {}
\newcommand*\previouschapterpage {}
\newif\ifpreviouschapterhassections
\newcommand*{\mytableofcontents}{%
\begingroup
\etocsetstyle{chapter}
{\etocskipfirstprefix}
{\ifpreviouschapterhassections
\else
\noindent\makebox[6em][l]{Chapter~\previouschapternumber}%
\textbf{\previouschaptername}\par
\fi
\addvspace{1em}
}
{\let\previouschaptername \etocthelinkedname
\let\previouschapternumber\etocthelinkednumber
\let\previouschapterpage \etocthelinkedpage
\previouschapterhassectionsfalse
}
{\ifpreviouschapterhassections % only if last chapter of document has no sections
\else
\noindent\makebox[6em][l]{Chapter~\previouschapternumber}%
\textbf{\previouschaptername}\par
\fi
}
%
\etocsetstyle{section}
{\def\storefirstsectiondata{%
\let\firstsectionname \etocthelinkedname
\let\firstsectionnumber\etocthelinkednumber
\let\firstsectionpage \etocthelinkedpage
\let\storefirstsectiondata\empty
}%
}
{}
{\storefirstsectiondata
\let\lastsectionname \etocthelinkedname
\let\lastsectionnumber\etocthelinkednumber
\let\lastsectionpage \etocthelinkedpage}
{\previouschapterhassectionstrue
\noindent\makebox[6em][l]{Chapter~\previouschapternumber}%
\textbf{\previouschaptername}\par
\firstsectionname{} (on page \firstsectionpage)
to
\lastsectionname{} (on page \lastsectionpage)%
\par
}%
%
\etocsetnexttocdepth {1}%
\previouschapterhassectionsfalse
\tableofcontents
\endgroup
}
\usepackage{hyperref}% to test the links
\begin{document}
\mytableofcontents
\chapter{ALPHA}
\section{Section 1}
\clearpage
\section{Section 2}
\clearpage
\section{Section 3}
\chapter{BETA}
\section{Section 4}
\clearpage
\section{Section 5}
\chapter{GAMMA}
\chapter{DELTA}
\section{Section 6}
\clearpage
\section{Section 7}
\end{document}
答案2
\documentclass{book}
\usepackage[explicit]{titlesec}
\usepackage{etoolbox}
\usepackage{tocloft}
\gdef\firstsection{}
\gdef\lastsection{}
\setcounter{tocdepth}{0}
\makeatletter
\newlength\mylen
\renewcommand\cftchappresnum{\@chapapp~}
\settowidth\mylen{\bfseries\cftchappresnum\cftchapaftersnum}
\addtolength\cftchapnumwidth{\mylen}
\makeatother
\titleformat{\section}
{\normalfont\Large\bfseries}
{\thesection}
{1em}
{%
\ifnum\value{section}=1\relax\gdef\firstsection{#1}\fi%
\gdef\lastsection{#1}#1%
}
\makeatletter
\pretocmd{\chapter}{%
\ifnum\value{chapter}=0\relax\else
\ifnum\value{section}>1\relax
\protect\addtocontents{toc}{\firstsection~to~\lastsection\par}
\fi
\fi}{}{}
\AtEndDocument{%
\ifnum\value{section}>1\relax
\protect\addtocontents{toc}{\firstsection~to~\lastsection\par}
\fi
}
\makeatother
\begin{document}
\tableofcontents
\chapter{A test chapter}
\section{Test section one one}
\section{Test section one two}
\section{Test section one three}
\chapter{Another test chapter}
\section{Test section two one}
\section{Test section two two}
\section{Test section two three}
\section{Test section two four}
\chapter{A test chapter without sections}
\chapter{A last test chapter}
\section{Test section three one}
\section{Test section three two}
\section{Test section three three}
\section{Test section three four}
\section{Test section three five}
\section{Test section three six}
\chapter{Another test chapter without sections}
\end{document}