使用report
文档类,我如何创建如下所示的目录:
第一章:标题
1.1 Title
1.2 Title
Appendices
1.A
1.B
第 2 章:标题
2.1 Title
2.2 Title
Appendices
2.A
2.B
答案1
下面有一个可能的解决方案;代码包括 1)重新定义\@chapter
(如中所定义report.cls
),以在目录的章节条目中包含单词“章节”。2)两个新定义的命令:\bappendix
和\eappendix
;前者启动一个组,将“附录”添加到目录并重新定义\section
(如中所定义report.cls
)以根据要求修改章节计数器;后者只是结束该组:
\documentclass{report}
\newcounter{appendix}[chapter]
\makeatletter
\def\@chapter[#1]#2{\ifnum \c@secnumdepth >\m@ne
\refstepcounter{chapter}%
\typeout{\@chapapp\space\thechapter.}%
\addcontentsline{toc}{chapter}%
{\@chapapp~\thechapter: #1}%
\else
\addcontentsline{toc}{chapter}{#1}%
\fi
\chaptermark{#1}%
\addtocontents{lof}{\protect\addvspace{10\p@}}%
\addtocontents{lot}{\protect\addvspace{10\p@}}%
\if@twocolumn
\@topnewpage[\@makechapterhead{#2}]%
\else
\@makechapterhead{#2}%
\@afterheading
\fi}
\newcommand\bappendix{%
\addtocontents{toc}{\protect\addvspace{10pt}Appendices}
\begingroup
\renewcommand\section{\stepcounter{appendix}%
\renewcommand\thesection{\thechapter.\Alph{appendix}}
\@startsection {section}{1}{\z@}%
{-3.5ex \@plus -1ex \@minus -.2ex}%
{2.3ex \@plus.2ex}%
{\normalfont\Large\bfseries}}}
\makeatother
\newcommand\eappendix{\endgroup}
\begin{document}
\tableofcontents
\chapter{First Chapter}
\section{Section One One}
\section{Section One Two}
\bappendix
\section{Appendix One A}
\section{Appendix One B}
\eappendix
\chapter{Second Chapter}
\section{Section Two One}
\section{Section Two Two}
\bappendix
\section{Appendix Two A}
\section{Appendix Two B}
\eappendix
\end{document}
以下是相同的方法,但使用etoolbox
包来简化代码:
\documentclass{report}
\usepackage{etoolbox}
\newcounter{appendix}[chapter]
\makeatletter
\patchcmd{\@chapter}{\protect\numberline{\thechapter}#1}
{\@chapapp~\thechapter: #1}{}{}
\newcommand\bappendix{%
\addtocontents{toc}{\protect\addvspace{10pt}Appendices}
\begingroup
\pretocmd{\section}{\stepcounter{appendix}%
\renewcommand\thesection{\thechapter.\Alph{appendix}}}{}{}}
\makeatother
\newcommand\eappendix{\endgroup}
\begin{document}
\tableofcontents
\chapter{First Chapter}
\section{Section One One}
\section{Section One Two}
\bappendix
\section{Appendix One A}
\section{Appendix One B}
\eappendix
\chapter{Second Chapter}
\section{Section Two One}
\section{Section Two Two}
\bappendix
\section{Appendix Two A}
\section{Appendix Two B}
\eappendix
\end{document}
subappendices
现在,这是一个使用包中的环境的更简单的解决方案appendix
;subappendices
使用该包对环境进行了轻微修改(在目录中的“附录”之前添加一些垂直空间)etoolbox
;该包还用于在目录中的章节条目之前添加“章节”:
\documentclass{report}
\usepackage{appendix}
\usepackage{etoolbox}
\AtBeginEnvironment{subappendices}{%
\addtocontents{toc}{\protect\addvspace{10pt}Appendices}
}
\makeatletter
\patchcmd{\@chapter}{\protect\numberline{\thechapter}#1}
{\@chapapp~\thechapter: #1}{}{}
\makeatother
\begin{document}
\tableofcontents
\chapter{First Chapter}
\section{Section One One}
\section{Section One Two}
\begin{subappendices}
\section{Appendix One A}
\section{Appendix One B}
\end{subappendices}
\chapter{Second Chapter}
\section{Section Two One}
\section{Section Two Two}
\begin{subappendices}
\section{Appendix Two A}
\section{Appendix Two B}
\end{subappendices}
\end{document}
答案2
以下 MWE 提供了您的问题的解决方案。MWE 还允许您指定章节中何时只有一个附录(当只有一个附录时,副标题显示为“附录”可能看起来很奇怪)。
\documentclass{report}
\usepackage{tocloft,ifthen}
\renewcommand\cftchappresnum{Chapter }
\cftsetindents{chapter}{0em}{6em}
\cftsetindents{section}{0em}{2.25em}
\cftsetindents{subsection}{2.25em}{3em}
\makeatletter
%% For source of "@seccntformat" command, see book "The LaTeX Companion"
\def\@seccntformat#1{\@ifundefined{#1@cntformat}%
{\csname the#1\endcsname\ \ }% default
{\csname #1@cntformat\endcsname}}% enable individual control
%% Individual control: '\section@cntformat'
\newcommand{\section@cntformat}{\thechapter.\thesection\ \ }
% Macros to redefine numbering of appendix sections
\newcommand{\appname}{} % dummy definition
\newcommand{\appsecnumbering}[1][0]{%
\ifthenelse{#1=1}{\renewcommand\appname{Appendix}}
{\renewcommand\appname{Appendices}}
\setcounter{section}{0}
\renewcommand\thesection{\thechapter.\Alph{section}}
\renewcommand{\section@cntformat}{\appendixname~\thesection\ \ }
\addtocontents{toc}{\medskip\protect{\mdseries\appname\par}}}
\newcommand\normalsecnumbering{%
\renewcommand{\thesection}{\thechapter.\arabic{section}}
\renewcommand{\section@cntformat}{\thesection\ \ }}
\let\origchapter\chapter
\renewcommand\chapter{\normalsecnumbering % first, reset numbering style
\origchapter} % second, execute the original \chapter command
\makeatother
\begin{document}
\tableofcontents
\chapter{First Chapter}
\section{First Section Title} \label{sec:11}
\section{Second Section Title} \label{sec:12}
\appsecnumbering
\section{Some additional stuff} \label{sec:1a}
\section{Still more additional stuff} \label{sec:1b}
A couple of cross-references:
Section~\ref{sec:11} and Appendix~\ref{sec:1b}.
\chapter{Another Chapter}
\section{First Section Title}
\section{Second Section Title}
\appsecnumbering[1] % specify "1" to request singular form of noun "Appendix"
\section{Some singular additional stuff}
\chapter{Yet Another Chapter}
\section{First Section Title}
\section{Second Section Title}
\appsecnumbering[7] % any number other than "1" generates plural form of noun
\section{Some additional stuff}
\section{Still more additional stuff}
\end{document}
请注意第一整页的外观,特别是对常规部分和附录区域部分的交叉引用。
最后,如果您希望节标题的开头与章节标题垂直对齐,则应将行
\cftsetindents{section}{0em}{2.25em}
到:
\cftsetindents{section}{0em}{6em}
答案3
memoir
类似这样的事情使用类或者通过手动调用附录包(usepackage{appendix}
)来为其他文档类完成并不太难。
请注意,虽然\addcontentsline{toc}
可能适用于任何 LaTeX 文档,
环境subappendices
(\begin{subappendices} ... \end{subappendices}
)来自memoir
类或appendix
包。
\newenvironment{coolsubappendices}{
\addcontentsline{toc}{section}{Appendices}
\begin{subappendices}
}{
\end{subappendices}
}
这是我实际做的具体事情,其中一些额外的功能与回答您的问题没有直接关系。
\newenvironment{coolsubappendices}{
\clearpage
\chapter*{Chapter \thechapter. ~ Appendices}
\addcontentsline{toc}{section}{Appendices}
\namedsubappendices % from memoir class I think
\begin{subappendices}
}{
\end{subappendices}
}
例如,在章节附录之前进行分页,为附录提供由章节标记的章节内标题,用于namedsubappendices
自动使文内章节名称为“附录 1.A”而不是仅仅“1.A”(尽管默认情况下目录中仍然只显示“1.A”)。
事后看来,这实际上只是已接受答案建议之一的简化版本,所以也许我不应该发布这个。无论如何,这是一个使用类的最小示例(从该答案复制和修改report
)
\documentclass{report}
\usepackage{appendix}
\newenvironment{coolsubappendices}{
\addcontentsline{toc}{section}{Appendices}
\begin{subappendices}
}{
\end{subappendices}
}
\begin{document}
\tableofcontents
\chapter{First Chapter}
\section{One One}
\section{One Two}
\begin{coolsubappendices}
\section{One A}
\section{One B}
\end{coolsubappendices}
\chapter{Second Chapter}
\section{Two One}
\section{Two Two}
\begin{coolsubappendices}
\section{Two A}
\section{Two B}
\end{coolsubappendices}
\end{document}