4

I have a few parts in a document of class book. I want to print the chapters range for the current part in its title page.

Any idea on how can I do that?

I know that the \tableofcontents command creates a toc file, but I have no idea at all how I can extract its chpaters lines to print the chapters range for a specific \part.

\documentclass{book}

\usepackage{titlesec}
  \titleformat{\part}[display]
  {\centering\huge}
  {\partname~\thepart}
  {0pt}
  {}
  [\Large Chapters range goes here]

\begin{document}
\part{Introduction}
\chapter{Algebra}
\section{sec one} \subsection{subsec one} \subsection{subsec two} \section{sec two}
\chapter{Geometry}
\section{sec one} \section{sec two} \subsection{subsec one} \subsection{subsec two}
\end{document}

enter image description here

3
  • If you happen to use an old latex version, you could use the xcntperchap package to count the number of chapters per part (I don't think it works with current texlive23). Commented May 15, 2023 at 15:48
  • Nice, but I am using texlive '22 and newer
    – tush
    Commented May 15, 2023 at 15:50
  • With texlive22 you might still be able to use it with this fix topanswers.xyz/tex?q=1987#a2230 Commented May 15, 2023 at 15:53

2 Answers 2

4

The following code inserts appropriate \labels with every \part and at the end of the document, then retrieves them to build the chapter range. It assumes numberic (\arabic) chapter numbering, and doesn't consider single-chapter parts.

enter image description here

\documentclass{book}

\usepackage{refcount}

\usepackage{titlesec}
  \titleformat{\part}[display]
  {\centering\huge}
  {\partname~\thepart}
  {0pt}
  {}
  [\Large Chapters
    \inteval{\getrefnumber{part-\arabic{part}}+1}% FROM chapter pulled from current \part + 1
    --% Range delimiter
    \ifcsname r@part-\number\numexpr\arabic{part}+1\endcsname
      \getrefnumber{part-\inteval{\arabic{part}+1}}% TO chapter pulled from next \part
    \else
      \getrefnumber{part-end}% At end-of-document chapter number
    \fi]

\makeatletter
\NewCommandCopy{\oldpart}{\part}
\RenewDocumentCommand{\part}{s o m}{%
  \IfBooleanTF{#1}
    {\oldpart*{#3}}% \part*[.]{..}
    {\IfValueTF{#2}
      {\oldpart[#2]{#3}}% \part[.]{..}
      {\oldpart{#3}}% \part{..}
      {\renewcommand{\@currentlabel}{\arabic{chapter}}\label{part-\arabic{part}}}% Store chapter number after issuing \part
    }
}
\AtEndDocument{%
  \renewcommand{\@currentlabel}{\thechapter}%
  \label{part-end}% Insert end-of-document chapter number capture
}
\makeatother

\begin{document}

\part{Introduction 1}

\chapter{Algebra 1}

\section{sec one} \subsection{subsec one} \subsection{subsec two} \section{sec two}

\chapter{Geometry 1}

\section{sec one} \section{sec two} \subsection{subsec one} \subsection{subsec two}

\chapter{Trigonometry 1}

\section{sec one} \section{sec two} \subsection{subsec one} \subsection{subsec two}

\part{Introduction 2}

\chapter{Algebra 2}

\section{sec one} \subsection{subsec one} \subsection{subsec two} \section{sec two}

\chapter{Geometry 2}

\section{sec one} \section{sec two} \subsection{subsec one} \subsection{subsec two}

\end{document}
5

Here is a possible approach, you will need two compilations to get the correct results.

\documentclass{book}

\usepackage{titlesec}
\titleformat{\part}[display]
{\centering\huge}
{\partname~\thepart}
{0pt}
{}
[\Large \chpterrange]

\makeatletter

\newcommand*\savechapnum[2]{
    \@newl@bel{chapranges}{part-#2:first-chap}{\the\numexpr#1+1\relax}
    \@newl@bel{chapranges}{part-\the\numexpr#2-1:last-chap}{#1}
}

\newcommand*\savelastchapnum[2]{
    \@newl@bel{chapranges}{part-#2:last-chap}{#1}
}

\AddToHook{cmd/part/before}{%
    \write\@auxout{%
        \protect\savechapnum{\the\c@chapter}{\the\c@part}
    }
}
\AddToHook{enddocument}{%
    \write\@auxout{%
        \protect\savelastchapnum{\the\c@chapter}{\the\c@part}
    }
}
\newcommand*\chpterrange{%
    \csname chapranges@part-\the\c@part:first-chap\endcsname
    --%
    \csname chapranges@part-\the\c@part:last-chap\endcsname
}
\makeatother

\begin{document}
    \part{Introduction}
    \chapter{Algebra}
    \section{sec one} \subsection{subsec one} \subsection{subsec two} \section{sec two}
    \chapter{Geometry}
    \section{sec one} \section{sec two} \subsection{subsec one} \subsection{subsec two}
    \part{Introduction}
    \chapter{Algebra}
    \section{sec one} \subsection{subsec one} \subsection{subsec two} \section{sec two}
    \chapter{Geometry}
    \section{sec one} \section{sec two} \subsection{subsec one} \subsection{subsec two}
    \part{Introduction}
    \chapter{Algebra}
    \section{sec one} \subsection{subsec one} \subsection{subsec two} \section{sec two}
    \chapter{Geometry}
    \section{sec one} \section{sec two} \subsection{subsec one} \subsection{subsec two}
\end{document}

Edit

With the following code you can change the format of the chapter range. The macro \chaprangeformat accepts three arguments. The first is the format when there are no chapters in the current part, the second is the format when there is only one chapter, inside the second argument #1 represent this chapter number, and the third is the format otherwise, with #1 representing the first chapter inside the part, and #2 the last.

Here are two examples:

\documentclass{book}

\usepackage{titlesec}
\titleformat{\part}[display]
{\centering\huge}
{\partname~\thepart}
{0pt}
{}
[\Large \chpterrange]

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\makeatletter

\newcommand*\save@chapnum[2]{
    \@newl@bel{chapranges}{part-\the\numexpr#2-1:last-chap}{#1}
}

\newcommand*\save@last@chapnum[2]{
    \@newl@bel{chapranges}{part-#2:last-chap}{#1}
}

\AddToHook{cmd/part/before}{%
    \write\@auxout{%
        \protect\save@chapnum{\the\c@chapter}{\the\c@part}
    }
}
\AddToHook{enddocument}{%
    \write\@auxout{%
        \protect\save@last@chapnum{\the\c@chapter}{\the\c@part}
    }
}

\newcommand\chaprangeformat[3]{
    \def\chap@range@format@a{#1}
    \def\chap@range@format@b##1{#2}
    \def\chap@range@format@c##1##2{#3}
}

\chaprangeformat{}{#1}{#1--#2} % default format

\newcommand*\chpterrange{%
    \ifnum\csname chapranges@part-\the\numexpr\c@part-1\relax:last-chap\endcsname=
    \csname chapranges@part-\the\c@part:last-chap\endcsname\relax
        \chap@range@format@a
    \else
        \ifnum\numexpr\csname chapranges@part-\the\numexpr\c@part-1\relax:last-chap\endcsname+1=
        \csname chapranges@part-\the\c@part:last-chap\endcsname\relax
            \chap@range@format@b{\csname chapranges@part-\the\c@part:last-chap\endcsname}%
        \else
        \chap@range@format@c{\the\numexpr1+\csname chapranges@part-\the\numexpr\c@part-1\relax:last-chap\endcsname\relax}%
                            {\csname chapranges@part-\the\c@part:last-chap\endcsname}%
        \fi
    \fi
}
\makeatother
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%



\begin{document}
    \part{Introduction}
    \section{sec one} \subsection{subsec one} \subsection{subsec two} \section{sec two}
    \chapter{Geometry}
    \section{sec one} \section{sec two} \subsection{subsec one} \subsection{subsec two}
    \part{Introduction}
    \chapter{Algebra}
    \section{sec one} \subsection{subsec one} \subsection{subsec two} \section{sec two}
    \chapter{Geometry}
    \section{sec one} \section{sec two} \subsection{subsec one} \subsection{subsec two}
    \part{Introduction}
    \chapter{Algebra}
    \section{sec one} \subsection{subsec one} \subsection{subsec two} \section{sec two}
    \chapter{Geometry}
    \section{sec one} \section{sec two} \subsection{subsec one} \subsection{subsec two}
    \part{Introduction}
    some text
\end{document}
\documentclass{book}

\usepackage{titlesec}
\titleformat{\part}[display]
{\centering\huge}
{\partname~\thepart}
{0pt}
{}
[\Large \chpterrange]

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\makeatletter

\newcommand*\save@chapnum[2]{
    \@newl@bel{chapranges}{part-\the\numexpr#2-1:last-chap}{#1}
}

\newcommand*\save@last@chapnum[2]{
    \@newl@bel{chapranges}{part-#2:last-chap}{#1}
}

\AddToHook{cmd/part/before}{%
    \write\@auxout{%
        \protect\save@chapnum{\the\c@chapter}{\the\c@part}
    }
}
\AddToHook{enddocument}{%
    \write\@auxout{%
        \protect\save@last@chapnum{\the\c@chapter}{\the\c@part}
    }
}

\newcommand\chaprangeformat[3]{
    \def\chap@range@format@a{#1}
    \def\chap@range@format@b##1{#2}
    \def\chap@range@format@c##1##2{#3}
}

\chaprangeformat{}{#1}{#1--#2} % default format

\newcommand*\chpterrange{%
    \ifnum\csname chapranges@part-\the\numexpr\c@part-1\relax:last-chap\endcsname=
    \csname chapranges@part-\the\c@part:last-chap\endcsname\relax
        \chap@range@format@a
    \else
        \ifnum\numexpr\csname chapranges@part-\the\numexpr\c@part-1\relax:last-chap\endcsname+1=
        \csname chapranges@part-\the\c@part:last-chap\endcsname\relax
            \chap@range@format@b{\csname chapranges@part-\the\c@part:last-chap\endcsname}%
        \else
        \chap@range@format@c{\the\numexpr1+\csname chapranges@part-\the\numexpr\c@part-1\relax:last-chap\endcsname\relax}%
                            {\csname chapranges@part-\the\c@part:last-chap\endcsname}%
        \fi
    \fi
}
\makeatother
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\makeatletter
\chaprangeformat{No Chapters}{Only Chapter \@Alph{#1}}{Chapters \@Alph{#1}--\@Alph{#2}} % changing the format
\makeatother

\begin{document}
    \part{Introduction}
    \section{sec one} \subsection{subsec one} \subsection{subsec two} \section{sec two}
    \chapter{Geometry}
    \section{sec one} \section{sec two} \subsection{subsec one} \subsection{subsec two}
    \part{Introduction}
    \chapter{Algebra}
    \section{sec one} \subsection{subsec one} \subsection{subsec two} \section{sec two}
    \chapter{Geometry}
    \section{sec one} \section{sec two} \subsection{subsec one} \subsection{subsec two}
    \part{Introduction}
    \chapter{Algebra}
    \section{sec one} \subsection{subsec one} \subsection{subsec two} \section{sec two}
    \chapter{Geometry}
    \section{sec one} \section{sec two} \subsection{subsec one} \subsection{subsec two}
    \part{Introduction}
    some text
\end{document}

You must log in to answer this question.

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