4

I'm writing a document class that uses the datetime2 package to write a month-year date in the title of the document. Here is a MWE:

\begin{filecontents}[overwrite]{customclass.cls}
    \NeedsTeXFormat{LaTeX2e}
    \ProvidesClass{customclass}[2014/12/16 Custom class]
    \LoadClassWithOptions{article}
    \RequirePackage{babel}
    \RequirePackage[useregional]{datetime2}
    \DTMlangsetup*{showdayofmonth=false}
    \renewcommand{\maketitle}[0]{\today}
\end{filecontents}

\documentclass[<language>]{customclass}

\begin{document} \maketitle \end{document}

I have run into the problem that many languages don't capitalize month names, whereas the desired behavior would be to capitalize them since it is a title (e.g. "Marzo de 2022" instead of "marzo de 2022").

I have tried using \expandafter\MakeUppercase and also the mfirstuc package as explained here, but none of those work (I assume it's because the \today command is pretty complex so it's not easy to expand?). I also cannot hard-code month names because I expect this class to work independently of language, and because I regularly use four languages to typeset documents (French, Italian, Spanish and English, three of which display this issue).

I would love a bit of help with this!

JamesT
  • 3,169
Pedro
  • 43

1 Answers1

4

By creating a new style (e.g. mydate) which display month date with a capital (with \DTMMonthname) you obtain you target. We need also the package datetime-calc which convert the numeric value of the current month date to the month in letters.

In \DTMdisplaydate, the second parameter is the number of the month (3 for March), and the first is the year.

Edit: Bug fix provided because in italian language, the datetime2 package don't provide uppercased month names.

Edit 2: I have also managed the spanish case, replacing the "Month Year" output by "Month de Year" (e.g. Marzo de 2023).

\begin{filecontents}[overwrite]{customclass.cls}
    \NeedsTeXFormat{LaTeX2e}
    \ProvidesClass{customclass}[2014/12/16 Custom class]
    \LoadClassWithOptions{article}
    \RequirePackage{babel}
    \RequirePackage{datetime2}
    \RequirePackage{datetime2-calc}
    %% Correction of a bug: in italian, Uppercase months are missing in datetime2
\newcommand*{\DTMitalianMonthname}[1]{%
 \ifcase#1
 \or
 Gennaio%
 \or
 Febbraio%
 \or
 Marzo%
 \or
 Aprile%
 \or
 Maggio%
 \or
 Giugno%
 \or
 Luglio%
 \or
 Agosto%
 \or
 Settembre%
 \or
 Ottobre%
 \or
 Novembre%
 \or
 Dicembre%
 \fi
 }
%% End of bug correction
    \DTMnewdatestyle{mydate}{%
    \renewcommand*{\DTMdisplaydate}[4]{%
    \DTMMonthname{##2} \iflanguage{spanish}{de }{}##1%
    }}
    \DTMsetdatestyle{mydate}
    \renewcommand{\maketitle}{\today}
\end{filecontents}

\documentclass[french]{customclass}

\begin{document} \maketitle \end{document}

enter image description here

With \documentclass[spanish]{customclass}:

enter image description here

With \documentclass[english]{customclass}:

enter image description here

quark67
  • 4,166
  • I need an adaptation, as you expect "Marzo de 2022". What is "de" in this date? – quark67 Mar 23 '23 at 00:52
  • Yup, was about to comment on that. Many languages have particles in between dates (in Spanish it is literally "march of 2023", in Catalan it would be "march of the 2023", etc.), and omitting them is usually incorrect. This is the best solution so far though, and it works in three out of my four common languages! – Pedro Mar 23 '23 at 00:59
  • Nevermind, italian refuses to work and prints out "marzo 2023" instead of "Marzo 2023"... Why is multi-language typesetting so complicated? – Pedro Mar 23 '23 at 01:03
  • @Pedro Yes, seems to be a bug in the datetime2 package. On http://mirrors.ctan.org/macros/latex/contrib/datetime2-contrib/datetime2-italian/datetime2-italian.pdf, \DTMitalianmonthname is defined 2 times in the code (line 5 page 1 and line 99 page 4). But for spanish (http://mirrors.ctan.org/macros/latex/contrib/datetime2-contrib/datetime2-spanish/datetime2-spanish.pdf) we have \DTMspanishmonthname line 5 page 2 and \DTMspanishMonthname line 33 page 2 (the former lowercase, the latter uppercase). Will file a bug report. – quark67 Mar 23 '23 at 01:34
  • 1
    @Pedro I have added a bug fix for the italian months. – quark67 Mar 23 '23 at 01:49
  • @Pedro Case for spanish language corrected (Marzo de 2023 instead of Marzo 2023, using the \iflanguage command from babel). – quark67 Mar 23 '23 at 02:50
  • Months in Italian are always lowercase, unless at the start of a sentence, of course, but this will be quite unlikely for a date. – egreg Mar 23 '23 at 10:40
  • @egreg Yes, it's the same in the French language. But I have meet with the OP requirement, the document class is perhaps a sort of log, by months. And if the title contains only the month and the year, the month need to be uppercased, isn't it? – quark67 Mar 23 '23 at 11:11
  • @egreg It is not uncommon for monthly assignments to be dated by month-year, and if the date is isolated in a paragraph, then it should be capitalized no matter what the capitalization would be in running text. This is usually not necessary when dated by day-month-year, since the day (number) always comes first, but when dated by month-year, the month always needs to be capitalized. – Pedro Mar 23 '23 at 13:02
  • @quark67 This is a great solution for now, although other languages also have date separators (Portuguese also uses "de", for example). I have also run into further problems when sharing this with a Hungarian friend, who claims dates in Hungarian should be written in the "1999. augusztus" form. I don't see a way of accommodating for all these subtleties without writing a separate package, and I'm not sure how much demand there is for it. – Pedro Mar 23 '23 at 13:09
  • 1
    @Pedro If we forgot the uppercase problem, there is a fundamental problem: with datetime2, the output of \today with \DTMlangsetup*{showdayofmonth=false} is for english: "March 23, 2023" (why the day ???) and for hungarian: "március 2023", not "2023. március". It's probably better to ask a new question (forgetting the uppercase problem) about how to obtain the good string with only month and year (and "de" when needed). Then for the uppercase: stringstrings and \capitalize{\today} adds space for spanish (Marzo de 2023 instead Marzo de 2023), but there is a solution with xstring. – quark67 Mar 23 '23 at 16:35
  • \DTMlangsetup*{showdayofmonth=false} is the warning-suppressed version of \DTMlangsetup{showdayofmonth=false}, which indeed warns that english datetime does not support showdayofmonth=false (which is weird, because both british and american support it). I agree that this question has opened up a bunch of edge cases and has highlighted some bugs that I didn't think of originally. I'm not sure there currently is a way of doing what I'm looking for without writing a separate package, but lmk if you find anything! I'm going to leave this marked as solution for now. – Pedro Mar 23 '23 at 16:52
  • 1
    @Pedro If you can provide a list of output for "March 2023" in all language you would support, I can try to modify my code with conditionals. E.g. adapting my code so for the hungarian language, it displays year, dot, space, month. And so on for other particular languages. Try to edit you question with this information. – quark67 Mar 23 '23 at 16:56