[Tex/LaTex] Using dates as section numbering

sectioning

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 the xparse 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 the titletoc package to format the TOC entries, but you could also use the tocloft package to do the same.

\documentclass{article}
\usepackage[english]{babel}
\usepackage[calc,useregional,showdow]{datetime2}
\usepackage{titletoc}
\dottedcontents{section}[1.5in]{\bfseries\large}{1.5in}{10pt}

\usepackage{xparse}
\NewDocumentCommand{\datesec}{o m m}{%
    \renewcommand\thesection{\DTMdate{#2}}
    \IfNoValueTF{#1}
        {\section{#3}}
        {\section[#1]{#3}}
}
\begin{document}
\tableofcontents
\datesec{2016-01-16}{Running}
\datesec{2016-01-18}{Swimming}
\end{document}

output of code

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:

\documentclass{article}
\usepackage[english]{babel}
\usepackage[calc,useregional,showdow,en-US]{datetime2}
\DTMrenewdatestyle
 {en-US}% label
 {% date style
   \renewcommand*{\DTMenglishfmtordsuffix}{\DTMenUSfmtordsuffix}%
   \renewcommand*\DTMdisplaydate[4]{%
   \ifDTMshowdow
       \ifnum##4>-1
        \DTMifbool{en-US}{abbr}%
         {\DTMenglishshortweekdayname{##4}}%
         {\DTMenglishweekdayname{##4}}%
        \space
       \fi
     \fi
     \DTMifbool{en-US}{abbr}%
     {\DTMenglishshortmonthname{##2}}%
     {\DTMenglishmonthname{##2}}%
     \DTMifbool{en-US}{showdayofmonth}%
     {%
       \DTMenUSmonthdaysep
       \DTMenglishordinal{##3}%
       \DTMifbool{en-US}{showyear}%
       {%
         \DTMenUSdayyearsep
         \number##1 % intended
       }%
       {}%
     }%
     {%
       \DTMifbool{en-US}{showyear}%
       {%
         \DTMenUSmonthdaysep
         \number##1 % intended
       }%
       {}%
     }%
   }%
   \renewcommand*{\DTMDisplaydate}[4]{\DTMdisplaydate{##1}{##2}{##3}{##4}}%
 }
\DTMlangsetup[en-US]{abbr=true}
\usepackage{titletoc}
\dottedcontents{section}[1.75in]{\bfseries\large}{1.75in}{10pt}
\usepackage{xparse}
\NewDocumentCommand{\datesec}{o m m}{%
    \renewcommand\thesection{\DTMdate{#2}}
    \IfNoValueTF{#1}
        {\section{#3}}
        {\section[#1]{#3}}
}
\begin{document}
\tableofcontents
\datesec{2016-01-16}{Running}
\datesec{2016-01-18}{Swimming}
\end{document}

output of code

Related Question