15

I have done my best to understand \expandafter, \edef and friends, but I can't find a way of using them to force expansion of the parameter passed in to the macro \FormatDate in the following example.

\documentclass{article}
\makeatletter
\def\@FormatDateAuxA#1/#2/#3 #4.{%
    % Day
            \number#3\relax\space
    % Month
            \ifcase#2{}?#2?\or
            January\or February\or March\or April\or
            May\or June\or July\or August\or
            September\or October\or November\or December\else
            ?#2?\fi\space
    % Year
            \ifnum#1>99{}#1\else\ifnum#1>9{}19#1\else190#1\fi\fi}
\def\@FormatDateAuxB#1$#2.{{
    \typeout{\noexpand#1}
    \@FormatDateAuxA#1 X.}}
\def\FormatDate#1{\@FormatDateAuxB#1$X.}
\makeatother
\begin{document}
\FormatDate{2015/4/15}
\def\Date{2015/4/15}
\FormatDate{\Date}
\end{document}

The above gives the following output.

2015/4/15
\Date 
! Argument of \@FormatDateAuxA has an extra }.

\Date hasn't been expanded and that has (presumably) stopped the pattern-matching for the parameter of \@FormatDateAuxA working. Any help with this will be greatly appreciated.

1 Answer 1

12
\documentclass{article}
\makeatletter
\def\@FormatDateAuxA#1/#2/#3 #4.{%
    % Day
            \number#3\relax\space
    % Month
            \ifcase#2{}?#2?\or
            January\or February\or March\or April\or
            May\or June\or July\or August\or
            September\or October\or November\or December\else
            ?#2?\fi\space
    % Year
            \ifnum#1>99{}#1\else\ifnum#1>9{}19#1\else190#1\fi\fi}
\def\@FormatDateAuxB#1$#2.{{
    \typeout{\noexpand#1}
    \@FormatDateAuxA#1 X.}}
\def\FormatDate#1{\expandafter\@FormatDateAuxB#1$X.}
                  %%%%%%%%%%%%%
\makeatother
\begin{document}
\FormatDate{2015/4/15}
\def\Date{2015/4/15}
\FormatDate{\Date}
\end{document}
3
  • Thanks. That certainly works. Unfortunately, it means that I will have to change several style files. Is it not possible to solve this without changing \FormatDate?
    – Rob Arthan
    Commented Apr 15, 2015 at 13:30
  • @RobArthan perhaps I misunderstood, I thought you wanted to change that. You could do \expandafter\FormatDate\expandafter{\Date} without changing the macro, but then your document source looks uglier:-) Commented Apr 15, 2015 at 13:33
  • 1
    I probably do want to change the style files eventually, but I also wanted to know if there was a quick local fix. Thanks for providing that
    – Rob Arthan
    Commented Apr 15, 2015 at 13:44

You must log in to answer this question.

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