6

I am typesetting a book which contains daily meditation exercises. There are 366 exercises (one for each day, including February 29).

The default styling for chapter titles has the text "Chapter 3" (for example) over the chapter title.

What I would like is for the "Chapter 3" to be replaced with the day of the year that corresponds with the exercise, in that case "January 3". Going forward it would reach "Chapter 31" => "January 31" and then "Chapter 32" => "February 1".

If there's a clever way to generate the chapter names so I don't have to type out 366 of them, that would be cool, but I'm fine with hardcoding them.

The bigger concern for me is how this shows up in the table of contents. I'd like it to show up "Date ChapterTitle PageNumber".

And finally, to complicate things, there is some titled material before and after the dated chapters which I want to show up in the TOC, but without a date associated with it (Introductions and About before, Appendices and Glossary after).

I'm not sure it's relevant, but the chapters are each one page.

Here's what I have so far:

\documentclass[openany]{book}
\usepackage{advdate}
\usepackage{datetime}

\newdateformat{noyear}{\monthname[\THEMONTH] \THEDAY
\SetDate[01/01/2020] % January 1 in a year with a leap day

\renewcommand{\chaptername}{}
\renewcommand{\thechapter}{\noyear\today \AdvanceDate[1]}

\begin{document}

\maketitle

\tableofcontents

\chapter*{Introduction}
\addcontentsline{toc}{chapter}{Introduction}

\chapter{Ham}
\chapter{Spam}
\chapter{Eggs}
\chapter{Ham}
\chapter{Spam}
\chapter{Eggs}
\chapter{Ham}
\chapter{Spam}
\chapter{Eggs}
\chapter{Ham}
\chapter{Spam}
\chapter{Eggs}
\chapter{Ham}
\chapter{Spam}
\chapter{Eggs}
\chapter{Ham}
\chapter{Spam}
\chapter{Eggs}
\chapter{Ham}
\chapter{Spam}
\chapter{Eggs}
\chapter{Ham}
\chapter{Spam}
\chapter{Eggs}
\chapter{Ham}
\chapter{Spam}
\chapter{Eggs}
\chapter{Ham}
\chapter{Spam}
\chapter{Eggs}
\chapter{Ham}
\chapter{Spam}
\chapter{Eggs}
\chapter{Ham}
\chapter{Spam}
\chapter{Eggs}
\chapter{Ham}
\chapter{Spam}
\chapter{Eggs}
\chapter{Ham}
\chapter{Spam}
\chapter{Eggs}
\chapter{Ham}
\chapter{Spam}
\chapter{Eggs}
\chapter{Ham}
\chapter{Spam}
\chapter{Eggs}

\chapter*{Appendices}
\addcontentsline{toc}{chapter}{Appendices}

\end{document}

I'm running into two problems:

  1. The text "January 1" is running over the chapter title in the table of contents.
  2. The dates aren't incrementing. I tried including \AdvanceDate[1] in the body (after each call to \chapter) but that only increments the chapter header dates, it doesn't increment the dates in the table of contents.
2
  • 1
    Good description. / Can you please add the beginning (\documentclass ...), which was probably lost during c&p ?
    – MS-SPO
    Jul 9, 2022 at 15:22
  • It's \documentclass{book}! And yeah, looks like it got lost in copy/paste. I'll fix it. Jul 12, 2022 at 2:53

1 Answer 1

5

The package projlib-datetime, as part of the ProjLib package, provides among others a command \ProjLibDate (the name \TheDate also works if the name is not taken, if you prefer) for converting numeric datetime string to natural languages. For example, in your case, you can use \ProjLibDate{01-01} (1-1 will also work) to get "January 1".

Thus one only needs to define a macro that can convert a number like 80 to the corresponding date string, for this example it is 3-20, and translate it to natural language. This is provided below as \GetDateStringFromNumber (the code between \ExplSyntaxOn and \ExplSyntaxOff).

The rest is basically the setup of your document. I only added a little use of titletoc to make sure that the dates and the titles do not overlap in the table of contents.

enter image description here

enter image description here

Below is the complete code.

\documentclass{book}

\usepackage{projlib-datetime}
\ExplSyntaxOn

\int_new:N \g_kerkeslager_month_int
\int_new:N \g_kerkeslager_day_int
\bool_new:N \g_kerkeslager_need_update_bool

\cs_new:Nn \__kerkeslager_get_date_update:n
  {
    \bool_if:NT \g_kerkeslager_need_update_bool
      {
        \int_compare:nNnTF { \g_kerkeslager_day_int } > { #1 }
          {
            \int_gincr:N \g_kerkeslager_month_int
            \int_gsub:Nn \g_kerkeslager_day_int { #1 }
          }
          {
            \bool_set_false:N \g_kerkeslager_need_update_bool
          }
      }
  }

\NewDocumentCommand \GetDateStringFromNumber { m }
  {
    \int_gset:Nn \g_kerkeslager_day_int { #1 }
    \int_gset:Nn \g_kerkeslager_month_int { 1 }
    \bool_set_true:N \g_kerkeslager_need_update_bool
    \__kerkeslager_get_date_update:n { 31 }
    \__kerkeslager_get_date_update:n { 29 }
    \__kerkeslager_get_date_update:n { 31 }
    \__kerkeslager_get_date_update:n { 30 }
    \__kerkeslager_get_date_update:n { 31 }
    \__kerkeslager_get_date_update:n { 30 }
    \__kerkeslager_get_date_update:n { 31 }
    \__kerkeslager_get_date_update:n { 31 }
    \__kerkeslager_get_date_update:n { 30 }
    \__kerkeslager_get_date_update:n { 31 }
    \__kerkeslager_get_date_update:n { 30 }
    \__kerkeslager_get_date_update:n { 31 }
    \ProjLibDate { \int_use:N \g_kerkeslager_month_int - \int_use:N \g_kerkeslager_day_int }
  }

\ExplSyntaxOff

\usepackage{titletoc}
\titlecontents{chapter}
  [8em]
  {}
  {\contentslabel{7.5em}}
  {\hspace*{-7.5em}}
  {\titlerule*[1pc]{}\contentspage}

\renewcommand{\chaptername}{}
\renewcommand{\thechapter}{\GetDateStringFromNumber{\arabic{chapter}}}

\begin{document}

\tableofcontents

\chapter*{Introduction}
\addcontentsline{toc}{chapter}{Introduction}

\chapter{Ham}
\chapter{Spam}
\chapter{Eggs}
\chapter{Ham}
\chapter{Spam}
\chapter{Eggs}
\chapter{Ham}
\chapter{Spam}
\chapter{Eggs}
\chapter{Ham}
\chapter{Spam}
\chapter{Eggs}
\chapter{Ham}
\chapter{Spam}
\chapter{Eggs}
\chapter{Ham}
\chapter{Spam}
\chapter{Eggs}
\chapter{Ham}
\chapter{Spam}
\chapter{Eggs}
\chapter{Ham}
\chapter{Spam}
\chapter{Eggs}
\chapter{Ham}
\chapter{Spam}
\chapter{Eggs}
\chapter{Ham}
\chapter{Spam}
\chapter{Eggs}
\chapter{Ham}
\chapter{Spam}
\chapter{Eggs}
\chapter{Ham}
\chapter{Spam}
\chapter{Eggs}
\chapter{Ham}
\chapter{Spam}
\chapter{Eggs}
\chapter{Ham}
\chapter{Spam}
\chapter{Eggs}
\chapter{Ham}
\chapter{Spam}
\chapter{Eggs}
\chapter{Ham}
\chapter{Spam}
\chapter{Eggs}
\chapter{Ham}
\chapter{Spam}
\chapter{Eggs}
\chapter{Ham}
\chapter{Spam}
\chapter{Eggs}
\chapter{Ham}
\chapter{Spam}
\chapter{Eggs}
\chapter{Ham}
\chapter{Spam}
\chapter{Eggs}
\chapter{Ham}
\chapter{Spam}
\chapter{Eggs}

\chapter*{Appendices}
\addcontentsline{toc}{chapter}{Appendices}

\end{document}

Also, if you speak French, for example, adding \usepackage[french]{babel}, and you will get:

enter image description here

You must log in to answer this question.

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