For an otherwise regular LaTeX document, I'd like to use dates to enumerate the sections and have the dates typeset properly in the table of contents and in the section headers. For example:
Tue Apr 28 2015 — Swimming ...................... 22
Tue May 19 2015 — Running ....................... 24
Fri Oct 23 2015 — Biking ........................ 26
In my current method, I use the following:
\setcounter{secnumdepth}{0} % No section numbering
\setcounter{tocdepth}{1} % Display only sections
and I define my sections simply:
\section{Tue Apr 28 2015 — Swimming}
This disables section numbering and I simply put the dates as strings in the section titles themselves, but the problem with this is the dates and the rest of the titles are not aligned properly in the table of contents. Any way to fix this?
Reading more about this problem, I understand that I can redefine thesection
to change the rendering of the section numbering, e.g.:
\renewcommand{\thesection}{\arabic{section}-}
Is there any way to use this to achieve what I want? For example, is it possible for me to redefine my sections or set a variable when starting a new section that will allow me to "map" section numbers to dates?
Best Answer
I would use the
datetime2
package to format the dates and then define a\datesec
macro which takes two arguments, one, an ISO date, and the second the section title. [Update: I've redefined the\datesec
command using thexparse
package so that we can preserve the optional argument that the\section
command uses for different TOC headings. This way\datesec
will also take such an optional argument.]There are two big advantages to this approach: first, you are using the dates as the section number itself, and so regular methods for formatting the table of contents and headers etc., work without modification (other than leaving enough space in the TOC for the longer labels). Second, you never hard code the date format itself, but format it using the
datetime2
package. This way if you decide you don't like the format, you can replace it with another one without changing the rest of the document.You can use the
titlesec
package to format the section headings as you like them. I've used thetitletoc
package to format the TOC entries, but you could also use thetocloft
package to do the same.Getting the date format in the way you have in your question requires adding some code to the
en-US
date definition, since it doesn't allow day of week as an option. Here's a new version of the document which also includes the day of the week: