3

I have having trouble defining a custom chapter "mybox" which I would like to be based upon the attributes of \chapter but called Box and numbered alphabetically.

Below is my code, the command \mybox is created but is identical to \chapter. The output and desired results are shown below.

\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}

enter image description here

2
  • Shall this Box appear in the table of contents as well? Does it have subboxes (like \section?)Giving it a different name will not make it use the other counter then, by the way
    – user31729
    Jan 3, 2017 at 11:33
  • It should do everything as a normal chapter, have sections (i.e. A.1,...) and appear in the TOC. Document structure would be e.g. Chapter 1, Chapter 2, Box A, Chapter 3, Box B, Chapter 4, ... Jan 3, 2017 at 11:49

2 Answers 2

3

For the sake completeness a shorter solution:

\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}

output

showcase_toc

7
  • In the TOC, this appears as Chapter A. How can I have the entry appear as Box A followed by Chapter 1, etc.? Jan 3, 2017 at 13:33
  • @user2568648 -- In my example the TOC shows simply "A ..." which, I guess, is acceptable. You must have activated some kind of tweak to the TOC in your version. Can you tell me what exactly that is?
    – Ruben
    Jan 3, 2017 at 13:39
  • I added the following above the definition \newlength\mylength \renewcommand\cftchappresnum{Chapter~} \settowidth\mylength{\cftchappresnum\cftchapaftersnum{5em}} \addtolength\cftchapnumwidth{\mylength} to add the text "Chapter" before the numbers. Obviously this also adds before the letters corresponding to box chapters. I was wondering if I could distinguish between the two. Jan 3, 2017 at 13:43
  • @user2568648 -- all those commands are new, thus can't be redefined. I guess you are doing lots of patching that is obscure to me and everybody watching this question as it is not in your MWE above. Anyways, your last mentioned issue is definitively indipendent from the question asked here. That means: you can either ask a new question regarding your new issue or (better) first search TeX.SX as I suspect that this exact question alredy got solved here.
    – Ruben
    Jan 3, 2017 at 13:50
  • This is intriguengly short -- too short -- try this with \label{foo} for the userbox and the \ref will be empty (and hyperref will complain as well!) I had something pretty similar in my first version and realized that there was some errors.
    – user31729
    Jan 3, 2017 at 18:05
1

This copies the definitions from report.cls and adds the changes (there is bunch of them: counter,chapter head commands, chapter unit name (i.e. 'Box')

In order to make it work with hyperref and the section command the \theHsection and \thesection has to be changed as well.

Currently it uses the same ToC as the regular chapter command.

\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}

enter image description here

5
  • Thanks. Out of interest why does the following not function as it would with a 'traditional' chapter: \boxchapterfont{\fontsize{24}{27}\selectfont}? Jan 3, 2017 at 12:32
  • @user2568648: There is no command \chapterfont or \boxchapterfont (yet!)
    – user31729
    Jan 3, 2017 at 12:35
  • A general side note: A bunch of patching is not easier, in my point of view
    – user31729
    Jan 3, 2017 at 12:45
  • I agree. \chapterfont exists in the sectsty package. Jan 3, 2017 at 12:51
  • @user2568648 -- the sectsty package defines those macros as hooks to restyle the standard sectioning commands. That does not mean that they exist as a standard core feature. (In fact: if a package defines a command you know that the opposite is the case.) If you want to have a \chapterfont or in your case \boxchapterfont macro you have to code it into your custom definition.
    – Ruben
    Jan 3, 2017 at 13:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .