Using xstring with translator

I wrote the following code to extract the first letter of weekdays, as provided by the translator package:

\documentclass{article}
\usepackage{xstring}
\usepackage{tikz}
\usetikzlibrary{calendar}

\begin{document}
\StrLeft{\pgfcalendarweekdayname{1}}{1}
\end{document}


The output would be "T" (for Tuesday) as expected. I'd like this to work with multilingual documents, so I added the translator package and a language option:

\documentclass[german]{article}
\usepackage{translator}
\usepackage{xstring}
\usepackage{tikz}
\usetikzlibrary{calendar}

\begin{document}
\StrLeft{\pgfcalendarweekdayname{1}}{1}
\end{document}


Compiling the document will now give the following error:

! Use of \\translate  doesn't match its definition.
\kernel@ifnextchar ...d@d =#1\def \reserved@a {#2}
\def \reserved@b {#3}\futu...
l.10 \StrLeft{\pgfcalendarweekdayname{1}}{1}

?


Reading the xstring documentation, I tried \noexpandarg, \expandarg, \normalexpandarg, but all caused errors.

Is there any way I can get around this?

• This seems to be an expansion problem – user31729 May 16 '15 at 13:08
• The \translate command is not expandable and it can't work with \StrLeft – egreg May 16 '15 at 13:13

You're out of luck. The macro \pgfcalendarweekdayname means

\translate{%
\ifcase #1Monday\or Tuesday\or Wednesday\or Thursday\or Friday\or Saturday\or Sunday\fi
}


and, without translator, \translate just returns its argument.

When translator is loaded, the macro \translate changes its meaning and becomes a set of instructions for printing the week day name. And it does it in a non expandable way, so it cannot be used in the argument of \StrLeft.

Here's a workaround that uses only expandable commands.

\documentclass[english,ngerman]{article}
\usepackage{babel}
\usepackage{translator}
\usepackage{xstring}
\usepackage{tikz}
\usepackage{xpatch}

\usetikzlibrary{calendar}
\usedictionary{months}

\let\xpgfcalendarweekdayname\pgfcalendarweekdayname
\xpatchcmd{\xpgfcalendarweekdayname}
{\translate}
{\xtranslate}
{}{\ddt}
\makeatletter
\newcommand{\xtranslate}[1]{%
\csname tr@@@\csname tr@ml@\languagename\endcsname @#1\endcsname
}
\makeatother

\begin{document}

Dienstag: \pgfcalendarweekdayname{1} \StrLeft{\xpgfcalendarweekdayname{1}}{1}

\selectlanguage{english}

Tuesday: \pgfcalendarweekdayname{1} \StrLeft{\xpgfcalendarweekdayname{1}}{1}

\end{document}


• This is brilliant! Thank you so much -- I'm afraid I still can't wrap my head around expansions much. – LianTze Lim May 16 '15 at 14:25