5

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?

  • What date format would you like to use? The one in the question is odd, because you list: Day Month DayN° Year. – Alenanno Jan 16 '16 at 17:17
  • 1
    @Alenanno What's so odd about that? It's a perfectly normal N. American date format. – Alan Munn Jan 16 '16 at 17:19
  • @AlanMunn I knew about the M D Y format, but not the D M D Y. – Alenanno Jan 16 '16 at 17:20
  • @Alenanno Really it doesn't matter what date format is used as long as it is dated. The format above is what I get out of the UNIX date command with the time removed. – Ana Jan 16 '16 at 17:24
  • @Ana Oh sure. I wasn't complaining, just making sure because I had never seen it. :D Thanks for clarifying. – Alenanno Jan 16 '16 at 17:24
5

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

| improve this answer | |
  • I can add your modification to the en-US style in the next version of datetime2-english if that's the standard way of formatting the US date with the day of week name. (I didn't include it as I've never come across a US date with the day of week name, so I didn't know how to deal with it.) – Nicola Talbot Jan 16 '16 at 19:36
  • @NicolaTalbot I'm not necessarily the best person to judge, being Canadian, but living in the US. But other than punctuation (perhaps some would prefer a comma after the day, so that should be added to the code) this is a fairly standard way to add the day of the week to a date in N. America, so I think it would be fine to add it. E.g. it's the second format shown for MSWord's Insert Date. – Alan Munn Jan 16 '16 at 19:49
  • Thanks. Okay, I'll add the week day name in the en-US and en-CA styles but replace the hard-coded \space with a user-configurable separator. – Nicola Talbot Jan 16 '16 at 19:53
  • Done. I've just uploaded datetime2-english v1.02 to CTAN, so it should be in the TeX distributions within the next few days. – Nicola Talbot Jan 18 '16 at 10:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.