7

On request, I have two different layouts for chapters for a book. The first one is exclusive to the Chapter 1 and the other one is for the others. The layout affect how the sections, definitions and paragraphs are displayed. I have the code to handle this in a little class file. This is a MWE:

\documentclass[12pt,letterpaper]{book}

\usepackage{amsmath,amsfonts,amssymb,amsthm}
\usepackage{thmtools}
\usepackage{ifthen,calc}
\usepackage{blindtext}

\parindent=0mm

\newtheorem{teo}{Theorem}[section]
\newtheorem{axi}{Axiom}
\newtheorem{lem}{Lemma}

\newcounter{definition}
\declaretheoremstyle[%
  spaceabove=6pt,%
  spacebelow=6pt,%
  headfont=\normalfont\bf,%
  notefont=\normalfont\bf,
  notebraces={{}{}},
  bodyfont=\itshape,
  headpunct={.}]{defistyle} 
\declaretheorem[
    name={Definition},
    numberlike=definition,
    style=defistyle]{defi}

\renewcommand\thesection
    {\arabic{chapter}.\arabic{section}}
\makeatletter
\renewcommand\@seccntformat[1]{
    {\csname the#1\endcsname}.\hspace{0.5em}}
\renewcommand\section{\@startsection
    {section}{1}{0mm}                
    {6pt}                            
    {1pt}                            
    {\bfseries}}                     

\renewcommand\paragraph{\@startsection
    {paragraph}{3}{0cm}             
    {6pt}                           
    {1pt}                           
    {\bfseries}}                    
\makeatother

\makeatletter
\newcommand{\capi}[1]{
    \chapter{#1}
    \ifthenelse{\value{chapter}=2}{
        \renewcommand\section{\@startsection
        {section}{1}{0mm}               
        {0pt}                           
        {1pt}                           
        {\Large}}                       
        \@addtoreset{definition}{section}
        \renewcommand{\thedefi}{\thesection.\arabic{definition}}        
        }{}}
\makeatother

\begin{document}
\capi{Some preliminaries}
\blindtext

\section{the first thing}
\blindtext[1]

\begin{defi}
The thing $1$ is real number. If $x$ is real then $x+1$ is real.
\end{defi}

\blindtext

\capi{Cool stuff}
\blindtext

\section{Complex things}
bla bla bla

\begin{defi}
We say that a number $x$ is complex if it is not really a number.
\end{defi}

\blindtext
\end{document}

As you can see, my actual way to handle the different style for each chapter is not so elegant. I have the book splitted in several files, one per chapter. When I'm reviewing the book I'm select the chapters I want to see via the \includeonly command. The problem is that in order to get the proper look of the sections, paragraphs and definitions I always have to include the chapter 2. Also, I'll would like to avoid to define a new command to the chapters. My question is: is there a way to improve this?

leo
  • 1,337

2 Answers2

6

Just define \capi to redefine itself when it's not the first chapter, so that the test and the redefinition will be performed only once.

\makeatletter
\newcommand{\capi}{%
  \ifnum\value{chapter}>0
    \changesectionanddefi
    \let\capi\chapter
  \fi
    \chapter
}
\def\changesectionanddefi{%
   \renewcommand\section{\@startsection{section}{1}{0mm}{6pt}{1pt}{\Large}}%
   \@addtoreset{definition}{section}%
   \renewcommand{\thedefi}{\thesection.\arabic{definition}}%
}
\makeatother

You can even do better: redefine \chapter so as to get rid of the non standard \capi command:

\makeatletter
\let\leo@chapter\chapter
\renewcommand{\chapter}{
  \ifnum\value{chapter}>0 
    \changesectionanddefi 
    \let\chapter\leo@chapter
  \fi
    \leo@chapter
}
\def\changesectionanddefi{%
   \renewcommand\section{\@startsection{section}{1}{0mm}{6pt}{1pt}{\Large}}%
   \@addtoreset{definition}{section}%
   \renewcommand{\thedefi}{\thesection.\arabic{definition}}%
}
\makeatother

So, no \capi with this, but the usual \chapter.


Be careful with end-of-lines in definitions. Also check the parameters to \@startsection: your code was not working correctly.

egreg
  • 1,121,712
  • Now, it works, at least the MWE works. I have edited my question. – leo May 09 '12 at 07:40
  • Why the condition is \value{chapter}>0 instead of \value{chapter}>1? – leo May 09 '12 at 07:54
  • @leo I've tested with \includeonly and it seems to do what's requested. The condition on \value{chapter} is because I put the test before the counter is incremented. So for the first \capi the counter value is 0. – egreg May 09 '12 at 07:57
3

Your problem is that you use a suboptimal condition. The condition is not that the chapter counter is equal to 2 but that the counter is greater than 1. Then the condition holds for all sections in all chapters. Thus you can then define \section to change its layout depending on the condition instead of placing the redefinition of \section inside the \chapter command.

Downside: the test will be executed whenever a section is processed (rather than only during chapter 2 once) but that is will not make a noticable difference in processing time.

  • I did it in that way precisely to avoid do the test whenever a section is processed. I mean, in an attempt to do the test only once when the chapter 2 is processed just for change things beginning with the chapter 2. – leo May 09 '12 at 07:07
  • But then you create exactly the situation you want to avoid: you have something that is modified only in case chapter 2 is being processed. If you want to be able to process all chapters individually you either have to do the test in \section or do the redefinition whenever a \chapter is being processed. You can't have both. – Frank Mittelbach May 09 '12 at 07:27
  • Yes that was my problem. At some point I eventually resigned and then I opted for the way I did. – leo May 09 '12 at 07:37