GeneralExtracting number from macro parameter string

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
wild-heart
Posts: 7
Joined: Mon Sep 12, 2016 7:40 am

Extracting number from macro parameter string

Post by wild-heart »

I am working on a document class that has some rather odd requirements for the layout of the document. Also the document class must not have any other dependent packages to generate the required layout of the document. Users of the class can add additional packages for features not affecting the overall document layout.

For the Table of Contents (ToC) I need to add additional information and formatting for chapter entries.

From looking at the latex.ltx file I discovered that .toc file has entries that is passed to the macro as a parameter for formatting the ToC entry. A typical .toc file entry is as follows:
\contentsline {chapter}{\numberline {1}Exploring the page layout}{1}

Resulting in macro parameter being as follows for the above .toc file entry:
\numberline {1}Exploring the page layout

I need to extract the number that appears within the braces { } for additional formatting. Is there a way to obtain the number without using additional packages such as xstring?

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org
LaTeX Beginner's Guide LaTeX Cookbook LaTeX TikZ graphics TikZによるLaTeXグラフィックス
User avatar
nlct
Posts: 276
Joined: Thu Nov 06, 2008 11:15 am

Extracting number from macro parameter string

Post by nlct »

Here's one way to do it:

Code: Select all

\documentclass{article}

\begin{document}

\newcommand{\parsetocentry}[1]{%
  \iparsetocentry#1\empty\empty\empty\endparsetocentry
}

\def\iparsetocentry#1#2#3\endparsetocentry{%
 \ifx#1\numberline\relax
   \def\theentrynum{#2}%
 \else
   \def\theentrynum{}%
 \fi
}

\parsetocentry{\numberline{1}Exploring the page layout}
Number: \theentrynum.

\parsetocentry{Unnumbered section}
Number: \theentrynum.

\end{document}
This has a high-level user command \parsetocentry which then passes its argument to a low-level command defined using TeX's \def to allow for the end placeholder token \endparsetocentry which is used to markup the end of the content. This allows the third argument (anything following \numberline) to be discarded. The three \empty commands protect against an unnumbered entry with a title that's less than three characters long.

Regards
Nicola Talbot
wild-heart
Posts: 7
Joined: Mon Sep 12, 2016 7:40 am

Extracting number from macro parameter string

Post by wild-heart »

Thank you very much Nicola for the very short simple solution, yet interesting to how it works, to a challenging issue I was having.

I am still very new to LaTeX and TeX. Been hacking up various classes to create required classes for specific projects.

Oh, by the way I started learning LaTeX using your Dickimaw Latex books, before obtaining the Stefan Kottwitz books.

I am really enjoying using LaTeX, and will be using it in all my future projects and a current project of mine for documentation. Why did I not tackle using LaTeX much earlier...
Post Reply