0

I want to format a date using \printdate from isodate, but I want to compute on the fly the date I'm formatting. Both parts of this work individually: I can compute a date and I can format one, but when I try to format the computed date I get an error.

Here's an MWE:

\documentclass[a4paper,12pt]{article}
\usepackage[UKenglish]{isodate}
\usepackage{xstring}
\begin{document}
\StrLeft{2017-03-11}{4}-08-31\par
\origdate\printdate{2017-08-31}\par
%\origdate\printdate{\StrLeft{2017-03-11}{4}-08-31}
\end{document}

If I uncomment the commented-out line I get the following error:

! Argument of \@xs@StrLeft has an extra }.
<inserted text> 
                \par 
l.7 ...te\printdate{\StrLeft{2017-03-11}{4}-08-31}

The error message suggests I've got unbalanced braces but unless I'm being particularly unobservant, that doesn't seem to be the case. Any ideas? I'm using LaTeX, but the same problem is exhibited with XeLaTeX.

1

The xstring macros do not store or return a value, but instead they output the result string to the document. Because \printdate expects a value (and not a code definition producing a piece of output document) this does not work.

However, xstring allows to store the value in a macro instead, using an optional argument. This macro can be used in \printdate.

MWE:

\documentclass[a4paper,12pt]{article}
\usepackage[UKenglish]{isodate}
\usepackage{xstring}
\begin{document}
\StrLeft{2017-03-11}{4}[\myyear]
\origdate\printdate{\myyear-08-31}
\end{document}

Note that LaTeX is designed as a macro expansion language, this means that there are no functions with a return value that can be used as input argument in other functions (as in other programming languages).

Instead, the definition of a macro is inserted at the position of the macro call. When using macro A in the arguments of macro B, this works only if the definition of macro A contains elements that macro B expects - in this example the \myyear macro contains a simple string, whereas \StrLeft contains complicated string processing code.

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.