定义自定义章节

定义自定义章节

我在定义自定义章节“mybox”时遇到了麻烦,我希望它基于\chapter名为 Box 的属性并按字母顺序编号。

下面是我的代码,命令\mybox已创建但与相同\chapter。输出和期望结果如下所示。

\documentclass[a4paper, 12pt]{report}

\usepackage[titles]{tocloft}

%---- Define Box ----%        
\newcommand{\mybox}{\chapter}
%\renewcommand{\myboxname}{Box}    - Causes error
\newcounter{mybox}
\renewcommand\themybox{\Alph{mybox}}

\begin{document}

\chapter{Chapter 1}

\mybox{Box A}

\end{document}

在此处输入图片描述

答案1

为了完整性,提供一个更短的解决方案:

\documentclass{report}
\usepackage{xcolor}

\makeatletter
\let\chaptercopy\chapter
\newcommand*\userbox[2][]{%
  \begingroup
    \def\@chapapp{Box}
    \def\thechapter{\@Alph\c@chapter}
    \color{green}
    \if\relax\detokenize{#1}\relax
      \chaptercopy{#2}
    \else
      \chaptercopy[#1]{#2}
    \fi
  \endgroup
}
\makeatother

\begin{document}
\userbox{My fancy box}
\end{document}

输出

展示目录

答案2

这将复制定义report.cls并添加更改(其中有很多:计数器、章节标题命令、章节单元名称(即“Box”))

为了使其hyperref与部分命令一起工作\theHsection,也\thesection必须更改。

ToC目前它的用途与常规命令相同chapter

\documentclass[a4paper, 12pt]{report}

\usepackage{xcolor}
\usepackage{xpatch}
\usepackage[titles]{tocloft}

\newcounter{boxchapter}
\renewcommand\theboxchapter{\Alph{boxchapter}}


\makeatletter
\@addtoreset{section}{boxchapter}% Reset the section within BoxChapter 

\newif\if@boxchapter
\renewcommand{\thesection}{\if@boxchapter\theboxchapter.\arabic{section}\else\thechapter.\arabic{section}\fi}
\AtBeginDocument{%
  \newcommand{\theHboxchapter}{boxchapter.\arabic{boxchapter}}% 
  \@ifpackageloaded{hyperref}{
    \renewcommand{\theHsection}{\if@boxchapter\theHboxchapter.\arabic{section}\else\theHchapter.\arabic{section}\fi}
  }{
    \providecommand{\phantomsection}{}
  }
}
\newcommand{\@boxchapapp}{Box}

\xpretocmd{\chapter}{\@boxchapterfalse}{}{}

\newcommand\BoxChapter{%
  \@boxchaptertrue\relax
  \if@openright\cleardoublepage\else\clearpage\fi
  \thispagestyle{plain}%
  \global\@topnum\z@%
  \@afterindentfalse%
  \secdef\@boxchapter\@sboxchapter%
  }
  \def\@boxchapter[#1]#2{\ifnum \c@secnumdepth >\m@ne
    \refstepcounter{boxchapter}%
    \typeout{\@boxchapapp\space\theboxchapter.}%
    \addcontentsline{toc}{chapter}%
    {\protect\numberline{\theboxchapter}#1}%
    \else
    \addcontentsline{toc}{chapter}{#1}%
    \fi
    \chaptermark{#1}%
  % \addtocontents{lof}{\protect\addvspace{10\p@}}%
  % \addtocontents{lot}{\protect\addvspace{10\p@}}%
  \if@twocolumn
  \@topnewpage[\@makeboxchapterhead{#2}]%
  \else
  \@makeboxchapterhead{#2}%
  \@afterheading
  \fi}

\def\@makeboxchapterhead#1{%
  \vspace*{50\p@}%
  {\parindent \z@ \raggedright \normalfont
    \ifnum \c@secnumdepth >\m@ne
    \color{green!60!black}%
        \huge\bfseries \@boxchapapp\space \theboxchapter
        \par\nobreak
        \vskip 20\p@
    \fi
    \interlinepenalty\@M
    \Huge \bfseries #1\par\nobreak
    \vskip 40\p@
  }}
\def\@boxschapter#1{\if@twocolumn
                   \@topnewpage[\@makesboxchapterhead{#1}]%
                 \else
                   \@makesboxchapterhead{#1}%
                   \@afterheading
                 \fi}
  \def\@makesboxchapterhead#1{%
    \vspace*{50\p@}%
    {\parindent \z@ \raggedright
      \normalfont
      \interlinepenalty\@M
      \color{green!60!black}%
      \Huge \bfseries  #1\par\nobreak
      \vskip 40\p@
    }}

\makeatother

%\usepackage{hyperref}



\begin{document}
\tableofcontents

\clearpage

Compare \ref{box::nicebox} and \ref{chapter::foo} and \ref{box::nicetoo}

\BoxChapter{My nice Box} \label{box::nicebox}

\section{Foo}

\chapter{Foo} \label{chapter::foo}

\section{Foostuff}

\BoxChapter{My other nice Box} \label{box::nicetoo}

\section{Foobarstuff}
\end{document}

在此处输入图片描述

相关内容