6

I tried using the package datetime2 to get the whole name of today’s day of week and then xstring to trim it as desired. But when I run the following code, the result is not trimmed at all:

\documentclass{article}

\usepackage[calc]{datetime2}
\usepackage{xstring}

\newcommand{\mytoday}{%
    \DTMcomputedayofweekindex{\DTMtoday}{\todaydowindex}
    \DTMtoday-\StrLeft{\DTMweekdayname{\todaydowindex}}{3}
    }

\title{Example}
\date{\mytoday}

\begin{document}

    \maketitle

\end{document}

I know it can also be done with a \ifcase, but I would like to understand why my code does not work and returns this untrimmed result.

1
  • 1
    you would need to expand to a sequence of characters before trimming but these operations are not expandable. \def\tmp{T}\def\tmpb{uesday}\tmp\tmpb prints as Tuesday but if you trim the first three tokens you don't get what you want. Commented Feb 27 at 10:44

1 Answer 1

7

datetime2 already has \DTMshortweekdayname for this, no need to reinvent the wheel.

\documentclass[english]{article}

\usepackage[calc]{datetime2}

\newcommand{\mytoday}{%
    \DTMcomputedayofweekindex{\DTMtoday}{\todaydowindex}%
    \DTMtoday-\DTMshortweekdayname{\todaydowindex}}

\title{Example}
\date{\mytoday}

\begin{document}

    \maketitle

\end{document}

enter image description here

Edit: An alternative, given the comment specifying that the document is actually in French and that datetime2 adds an abbreviation dot to the weekdays in this case, which is undesired. The manual for datetime2 states the following about month and weekday names:

If you want the month name or weekday name to appear in a section or chapter heading, it’s best to use the expandable commands provided by the language modules rather than the robust commands provided by datetime2-calc. Remember that you can’t use robust commands in PDF bookmarks and such commands may prevent case-changing in headers for page styles that use \MakeUppercase.

So, if you can specify the language in use, you can get an expandable weekday name, and do what you initially intended to do.

\documentclass[french]{article}

\usepackage[calc]{datetime2}
\usepackage{xstring}

\newcommand{\myfrenchtoday}{%
  \DTMcomputedayofweekindex{\DTMtoday}{\todaydowindex}%
  % or \DTMfrenchshortWeekdayname
  \DTMtoday-\StrLeft{\DTMfrenchshortweekdayname{\todaydowindex}}{3}}

\title{Example}
\date{\myfrenchtoday}

\begin{document}

\maketitle

\end{document}

enter image description here

2
  • I removed it in my example because I thought it made no difference, but my document is in French and in this language, \DTMshortweekdayname adds a period to the first three letters, so it is not exactly what I want.
    – Myvh
    Commented Feb 28 at 19:05
  • 1
    @Myvh Ah, well, in this case the example happened to be too minimal. But, agreed, it was hard to predict the language would be relevant. See the edit for an alternative.
    – gusbrs
    Commented Feb 29 at 10:00

You must log in to answer this question.

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