在当前部分的标题页上打印该部分的章节范围

在当前部分的标题页上打印该部分的章节范围

我在类文档中有几个部分book。我想在其标题页中打印当前部分的章节范围。

我知道该如何做吗?

我知道该\tableofcontents命令会创建一个toc文件,但我完全不知道如何提取其章节行来打印特定的章节范围\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}

在此处输入图片描述

答案1

以下代码在文档末尾插入适当的\label和,然后检索它们以构建章节范围。它假设使用 numberic ( ) 章节编号,并且不考虑单章部分。\part\arabic

在此处输入图片描述

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

答案2

这是一种可能的方法,您需要两次编译才能获得正确的结果。

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

编辑

使用以下代码可以更改章节范围的格式。该宏\chaprangeformat接受三个参数。第一个是当前部分中没有章节时的格式,第二个是只有一个章节时的格式,第二个参数#1表示此章节编号,第三个是其他格式,#1表示部分中的第一个章节,#2最后一个。

以下有两个示例:

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

相关内容