Text FormattingCustomization of a section number in the table of contents

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
Post Reply
fsoedjede
Posts: 3
Joined: Sat Jun 25, 2022 11:12 pm

Customization of a section number in the table of contents

Post by fsoedjede »

Hello Everyone,

Using the code below, I want to be able to change the numbering in the toc just by changing the value passed to the command \newcommand*{\CPHHeadingLevelIINumberStyle}{Decimal}

In the code,
- when I use \renewcommand{\thechapter}{\HeadingNumberStyleDisplay{chapter}{\CPHHeadingLevelIINumberStyle}}, I see 0 in the TOC without an error raised
- when I use \renewcommand{\thechapter}{\arabic{chapter}}, it works

I think I'm missing something about expanding args but my tests are not conclusive.

Does anyone know what my code is missing?
Thanks in advance.

Code: Select all

\documentclass{book}

% Allows usage of "\IfStrEq"
\usepackage{xstring}
% Allows usage of "\NewDocumentCommand"
\usepackage{xparse}
\usepackage{titletoc}
\usepackage{lipsum}

\titlecontents{chapter}% <section-type>
    [0pt]% <left>
    {\addvspace{0.25pc}\normalsize}% <above-code>
    {\thecontentslabel~:\,}% <numbered-entry-format>
    {}% <numberless-entry-format>
    {\normalfont\dotfill\contentspage}% <filler-page-format>

%%%%% Sections counter style
\NewDocumentCommand{\HeadingNumberStyleDisplay}{m m}{%
    \IfStrEq{#2}{Decimal}{\arabic{#1}}{\relax}%
    \IfStrEq{#2}{LowerRoman}{\roman{#1}}{\relax}%
    \IfStrEq{#2}{UpperRoman}{\Roman{#1}}{\relax}%
}

%% Heading level 2 options
\newcommand*{\CPHHeadingLevelIINumberStyle}{Decimal}

\renewcommand{\thechapter}{\HeadingNumberStyleDisplay{chapter}{\CPHHeadingLevelIINumberStyle}}% This does not work
%\renewcommand{\thechapter}{\arabic{chapter}} % This works

\begin{document}

\setcounter{tocdepth}{1}
\tableofcontents

\chapter{Test chapter}

\lipsum[1]

\end{document}

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org
LaTeX Beginner's Guide LaTeX Cookbook LaTeX TikZ graphics TikZによるLaTeXグラフィックス
Post Reply