2

I want to use a conditional expression that modifies the chapter title depending if the chapter has some sections or not.

Example:

\ifnum \value{chaptersections}>0
      \setchaptername1
\else
      \setchaptername2
\fi

I've tried to find some counter that gives me information (chaptersections) but without success.

Any suggestion?

jub0bs
  • 58,916
Jay-Si
  • 21

2 Answers2

2

This is a quick solution with the cntperchap package (I am quite familar with its author ;-))

The key is to register counters which should be tracked on a per chapter base, here it is the section counter.

Then use the \GetStoredCounterValue[chapter number]{section} macro to get the number of sections in the relevant chapter. This does not print the number itself, it is stored in the cps@@tempstoragecounter counter macro...

I should change/document this in the next release of cntperchap package, probably during the next days.

Some notes

  • The cntperchap package needs two (pdf)LaTeX runs to store the correct values
  • Basically any LaTeX counter be tracked this way, however, the page counter is a little bit tricky due to the shipout mechanism.

\documentclass{book}

\usepackage{cntperchap}

\RegisterCounters{section} % Register the counters which should be watched

\makeatletter
\newcommand{\currentchaptertitle}[1]{%
  \GetStoredCounterValue[#1]{section}
  \ifnumgreater{\value{cps@@tempcounterstorage}}{0}{%
    A chapter with \number\value{cps@@tempcounterstorage} sections
  }{%
    A chapter without any sections
  } 
}
\makeatother

\begin{document}

\chapter{\currentchaptertitle{1}}

\section{First}
\section{Second}


\chapter{\currentchaptertitle{2}}


\end{document}

enter image description here

1

The following example remembers the section count in the .aux file.

  • A counter abs@chapter is used to identify the chapter. Thus also starred or unnumbered chapters are supported.
  • \section is redefined to write a line in the .aux file, which increases the number of sections for the current chapter. Also starred \section commands are supported.
  • In the next LaTeX run, \GetChapterSections returns the number of sections for the current chapter. \GetNextChapterSections expands to the number of the sections in the next chapter.

Full example file:

\documentclass{report}

\usepackage{auxhook}
\AddLineBeginAux{%
  \string\providecommand\string\AuxChapterSection[1]{}%
}

\makeatletter
\newcommand*{\AuxChapterSection}[1]{%
  \@ifundefined{AuxChapterSection@#1}{%
    \expandafter\gdef\csname AuxChapterSection@#1\endcsname{1}%
  }{%
    \expandafter\xdef\csname AuxChapterSection@#1\endcsname{%
      \the\numexpr\csname AuxChapterSection@#1\endcsname + 1\relax
    }%
  }%
}
\newcommand*{\WriteAuxChapterSection}{%
  \if@filesw
    \immediate\write\@auxout{%
      \string\AuxChapterSection{\the\value{abs@chapter}}%
    }%
  \fi
}

\newcounter{abs@chapter}
\newcommand{\Original}{}
\let\OriginalChapter\chapter
\renewcommand*{\chapter}{%
  \stepcounter{abs@chapter}%
  \OriginalChapter
}
\let\OriginalSection\section
\renewcommand*{\section}{%
  \WriteAuxChapterSection
  \OriginalSection
}

\newcommand*{\@GetChapterSections}[1]{%
  \@ifundefined{AuxChapterSection@#1}{%
    0%
  }{%
    \csname AuxChapterSection@#1\endcsname
  }%
}
\newcommand*{\GetChapterSections}{%
  \expandafter\@GetChapterSections\expandafter{\the\value{abs@chapter}}%
}
\newcommand*{\GetNextChapterSections}{%
  \expandafter\@GetChapterSections\expandafter
    {\the\numexpr\value{abs@chapter}+1\relax}%
}
\makeatother

\begin{document}
\chapter*{Preface}
Sections: \GetChapterSections\\
Sections in next chapter: \GetNextChapterSections
\chapter{Introduction}
Sections: \GetChapterSections\\
Sections in next chapter: \GetNextChapterSections
\section{First section}
\section{Second section}
\chapter{Summary}
Sections: \GetChapterSections
\section*{Starred section}   
\end{document}

\end{document}

Page 1
Page 2
Page 3

Heiko Oberdiek
  • 271,626