两栏书目录:被误解的行为

两栏书目录:被误解的行为

目标是为书籍的(子)部分制作一个包含两栏的主目录。这可以按照帖子的答案实现:修补 \chapter 命令,使所有下部章节在 ToC 中设置为两列

为了能够在章节内添加附录,我定义了一个subappendices环境,借鉴了包中的想法appendix

在这个阶段,这很好。为了保持 LaTeX 文档的传统结构(目录、主要内容、附录……)并考虑到一些可能的附录作为章节,我简单地定义了一个新的辅助计数器(参见 MWE 下方)。

此外,必须修改子附录的页面布局。使用geometry和包,我用和etoolbox包装了subappendices环境 。\BeforeBeginEnvironment{subappendices}{\clearpage\newpagelayout}\AfterEndEnvironment{subappendices}{\restoregeometry}

当文档以环境结尾时,就会出现问题subappendices\AtEndDocument宏不再muticols在目录中结束环境。

经过反复试验后,我发现在文档末尾添加一些文本可以解决问题,但显然也会添加一个新的无用页面(尽管使用 \phantom 宏可以使该页面为空)。

我对任何建议和解释都很感兴趣。我错过了什么?

梅威瑟:

\documentclass{book}
\usepackage{multicol,etoolbox}
\usepackage{titletoc,titlesec}
\usepackage{geometry}
%\usepackage{hyperref}

\geometry{margin=4cm,a4paper,showframe}

%-- Command for changing the page layout mid-document
\newcommand{\newpagelayout}{\newgeometry{margin=2cm}}

% With \appendix macro \value{chapter} is resetted to zero
\newcounter{countchapters}

% From https://tex.stackexchange.com/questions/199139
\titleformat{\chapter}[display]
  {\Huge\bfseries}
  {\chaptertitlename~\thechapter}
  {1em}{}
  [\ifnum\value{chapter}>0 
    \addtocontents{toc}{\protect\begin{multicols}{2}}
   \fi
   \stepcounter{countchapters}
  ]% After-Code

\titleformat{name=\chapter,numberless}[display]
  {\Huge\bfseries}
  {\chaptertitlename~\thechapter}
  {1em}{}[]

\pretocmd{\chapter}{%
  \ifnum\value{countchapters}>0 
    \addtocontents{toc}{\protect\end{multicols}}%
  \fi}{}{}

\AtEndDocument{%
  \ifnum\value{countchapters}>0
    %\phantom{}% Works but adds a useless page
  \addtocontents{toc}{\protect\end{multicols}}%
  \fi}

% Idea borrowed to the `appendix' package
\newcounter{subappends}
\newenvironment{subappendices}{\par
  \stepcounter{subappends}
  \setcounter{section}{0}
  \renewcommand{\thesection}{\Alph{section}}
}{}

\BeforeBeginEnvironment{subappendices}{\clearpage\newpagelayout}
\AfterEndEnvironment{subappendices}{\restoregeometry}

\begin{document}

\tableofcontents
\mainmatter

\chapter{FOO}
\section{bar1}
\section{bar2}
\section{bar3}
\section{bar4}
\begin{subappendices}
\section{Appendix A}
\section{Appendix B}
\end{subappendices}

\chapter{BAR}
\section{foo1}
\section{foo2}
\section{foo3}
\section{foo4}
\begin{subappendices}% It fails for the last subappendix
\section{Appendix A}
\section{Appendix B}
\end{subappendices}

\appendix

% Test: OK whithout and with `subappendices'
%\chapter{FOO appendix}
%\section{bar1}
%\section{bar2}
%\section{bar3}
%\section{bar4}

\end{document}

答案1

我添加了一个\newif\ifmulticolsused并将一个\ifmulticolsused-测试写入其中ToC,使其ToC本身负责multicols环境的关闭。

\documentclass{book}
\usepackage{multicol,etoolbox}
\usepackage{titletoc,titlesec}
\usepackage{geometry}
%\usepackage{hyperref}

\geometry{margin=4cm,a4paper,showframe}

%-- Command for changing the page layout mid-document
\newcommand{\newpagelayout}{\newgeometry{margin=2cm}}

% With \appendix macro \value{chapter} is resetted to zero
\newcounter{countchapters}


\newif\ifmulticolsused

\pretocmd{\multicols}{\global\multicolsusedtrue}{}{}
\apptocmd{\endmulticols}{\global\multicolsusedfalse}{}{}

% From http://tex.stackexchange.com/questions/199139
\titleformat{\chapter}[display]
  {\Huge\bfseries}
  {\chaptertitlename~\thechapter}
  {1em}{}
  [\ifnum\value{chapter}>0 
    \addtocontents{toc}{\protect\begin{multicols}{2}\protect\multicolsusedtrue}
   \fi
   \stepcounter{countchapters}
  ]% After-Code

\titleformat{name=\chapter,numberless}[display]
  {\Huge\bfseries}
  {\chaptertitlename~\thechapter}
  {1em}{}[]

\pretocmd{\chapter}{%
  % Close the multicols if needed
  \addtocontents{toc}{\protect\ifmulticolsused\protect\end{multicols}\protect\fi}
}{}{}

\AtEndDocument{%
  % Close the multicols if needed
  \addtocontents{toc}{\protect\ifmulticolsused\protect\end{multicols}\protect\fi}
}

% Idea borrowed to the `appendix' package
\newcounter{subappends}
\newenvironment{subappendices}{\par
  \stepcounter{subappends}
  \setcounter{section}{0}
  \renewcommand{\thesection}{\Alph{section}}
}{%
  % Close the multicols if needed
  \addtocontents{toc}{\protect\ifmulticolsused\protect\end{multicols}\protect\fi}%
}

\BeforeBeginEnvironment{subappendices}{\clearpage\newpagelayout}
\AfterEndEnvironment{subappendices}{\restoregeometry}

\begin{document}

\tableofcontents
\mainmatter

\chapter{FOO}
\section{bar1}
\section{bar2}
\section{bar3}
\section{bar4}
\begin{subappendices}
\section{Appendix A}
\section{Appendix B}
\end{subappendices}

\chapter{BAR}
\section{foo1}
\section{foo2}
\section{foo3}
\section{foo4}
\begin{subappendices}% It fails for the last subappendix
\section{Appendix A}
\section{Appendix B}
\end{subappendices}

\appendix

% Test: OK whithout and with `subappendices'
\chapter{FOO appendix}
\section{bar1}
\section{bar2}
\section{bar3}
\section{bar4}

\end{document}

相关内容