7

I like to print dates formatted in this way: 6 October 2018. Usually, I just type the date by hand. But I tried to set things up so that \today would format dates the way I like. I must have only tested it when the day number was two digits. I have this:

\documentclass{article}
\usepackage[english]{datetime2}
\DTMnewdatestyle{strunkdate}{%
  \renewcommand*{\DTMdisplaydate}[4]{##3 \DTMenglishmonthname{##2} ##1}
  \renewcommand*{\DTMDisplaydate}{\DTMdisplaydate}}
\AtBeginDocument{%
  \DTMsetdatestyle{strunkdate}}% after babel loaded, or babel stomps on this
\begin{document}
Today is \today.
\end{document}

Unfortunately, this prints 06 October 2018, with a leading zero to force a two-digit day. How do I get 6 October 2018, without the leading zero?

8

The package may have a specific command, but you can use the TeX \number command

\documentclass{article}
\usepackage[english]{datetime2}
\DTMnewdatestyle{strunkdate}{%
  \renewcommand*{\DTMdisplaydate}[4]{\number##3\ \DTMenglishmonthname{##2} ##1}
  \renewcommand*{\DTMDisplaydate}{\DTMdisplaydate}}
\AtBeginDocument{%
  \DTMsetdatestyle{strunkdate}}% after babel loaded, or babel stomps on this
\begin{document}
Today is \today.
\end{document}
  • 1
    That's actually the method recommended by the manual of datetime2, page 95. No caveat about the problem of following space, unfortunately. – egreg Oct 6 '18 at 19:48
  • 1
    @egreg didn't occur to me to look in a manual! – David Carlisle Oct 6 '18 at 19:56
  • In order to preserve full expandability to character tokens, \number\numexpr##3\relax\space\DTMenglishmonthname{##2} ##1 could be better. – egreg Oct 6 '18 at 20:04
  • @egreg I did wonder about that but expansion unlikely to be needed here I would guess and \ looks nicer in the code and doesn't require an essay on what it does:-) – David Carlisle Oct 6 '18 at 20:09
  • Works great! Is the expandability that you're worried about, @egreg, that of the characters in the day number itself? – dedded Oct 6 '18 at 21:08

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.