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!

0

1 Answer 1

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

14
  • I need an adaptation, as you expect "Marzo de 2022". What is "de" in this date?
    – quark67
    Commented Mar 23, 2023 at 0: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
    Commented Mar 23, 2023 at 0:59
  • 1
    @Pedro I have added a bug fix for the italian months.
    – quark67
    Commented Mar 23, 2023 at 1:49
  • 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
    Commented Mar 23, 2023 at 16:35
  • 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
    Commented Mar 23, 2023 at 16:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .