2

I try to use a "Switch Case" macro in my LaTex File. But I want to put in the "Switch Case" macro the month not a number. Unfortunately a get nada as output instead of 31.01.22.

Source:Implementing switch cases

\documentclass{article}
\usepackage{pdftexcmds}
\makeatletter
\newcommand*{\dothis}[1]{%
    \stringcases
    {#1}%
    {%      
        {1}{31.01.\the\year}
        {2}{28.02.\the\year}
        {3}{31.03.\the\year}
        {4}{30.04.\the\year}
        {5}{31.05.\the\year}
        {6}{30.06.\the\year}
        {7}{31.07.\the\year}
        {8}{31.08.\the\year}
        {9}{30.09.\the\year}
        {10}{31.10.\the\year}
        {11}{30.11.\the\year}
        {12}{31.12.\the\year}
    }%
    {[nada]}%
}
\newcommand{\stringcases}[3]{%
    \romannumeral
    \str@case{#1}#2{#1}{#3}\q@stop
}
\newcommand{\str@case}[3]{%
    \ifnum\pdf@strcmp{\unexpanded{#1}}{\unexpanded{#2}}=\z@
    \expandafter\@firstoftwo
    \else
    \expandafter\@secondoftwo
    \fi
    {\str@case@end{#3}}
    {\str@case{#1}}%
}
\newcommand{\str@case@end}{}
\long\def\str@case@end#1#2\q@stop{\z@#1}
\makeatother

\begin{document}

\dothis{\the\month}

\dothis{2}

\dothis{3}

\dothis{4}

\end{document}

IH Pro
  • 141
  • If there is a better way to get the last day of the month, I am also open to read your proposals. – IH Pro Jan 06 '22 at 01:34

3 Answers3

3

I'd not reinvent the wheel…

\documentclass{article}

\ExplSyntaxOn

\NewExpandableDocumentCommand{\dothis}{m} { \str_case_e:nnF {#1} { {1}{31.01.\the\year} {2}{28.02.\the\year} {3}{31.03.\the\year} {4}{30.04.\the\year} {5}{31.05.\the\year} {6}{30.06.\the\year} {7}{31.07.\the\year} {8}{31.08.\the\year} {9}{30.09.\the\year} {10}{31.10.\the\year} {11}{30.11.\the\year} {12}{31.12.\the\year} } {[nada]}% } \ExplSyntaxOff

\begin{document}

\dothis{\the\month}

\dothis{2}

\dothis{3}

\dothis{4}

\dothis{15}

\end{document}

enter image description here

An extended version to show the potential of expl3.

\documentclass{article}

\ExplSyntaxOn

\NewExpandableDocumentCommand{\dothis}{om} { \ihpro_dothis:ee { \int_eval:n { \IfNoValueTF { #1 } { \c_sys_year_int } { #1 } } } { \int_eval:n { #2 } } }

\cs_new:Nn \ihpro_dothis:nn { \str_case_e:nnF {#2} { {1}{31.01.#1} {2}{28.02.#1} {3}{31.03.#1} {4}{30.04.#1} {5}{31.05.#1} {6}{30.06.#1} {7}{31.07.#1} {8}{31.08.#1} {9}{30.09.#1} {10}{31.10.#1} {11}{30.11.#1} {12}{31.12.#1} } {[nada]}% } \cs_generate_variant:Nn \ihpro_dothis:nn { ee }

\ExplSyntaxOff

\begin{document}

\dothis{\month}

\dothis{2}

\dothis{3}

\dothis{4}

\dothis{15}

\dothis[2010]{8}

\end{document}

enter image description here

Here's an extended code for leap years.

\documentclass{article}

\ExplSyntaxOn

\NewExpandableDocumentCommand{\dothis}{om} { \ihpro_dothis:ee { \int_eval:n { \IfNoValueTF { #1 } { \c_sys_year_int } { #1 } } } { \int_eval:n { #2 } } }

\cs_new:Nn \ihpro_dothis:nn { \str_case_e:nnF {#2} { {1}{31.01.#1} {2}{\ihpro_if_leap:nTF{#1}{29}{28}.02.#1} {3}{31.03.#1} {4}{30.04.#1} {5}{31.05.#1} {6}{30.06.#1} {7}{31.07.#1} {8}{31.08.#1} {9}{30.09.#1} {10}{31.10.#1} {11}{30.11.#1} {12}{31.12.#1} } {[nada]}% } \cs_generate_variant:Nn \ihpro_dothis:nn { ee }

\prg_new_conditional:Nnn \ihpro_if_leap:n { T, F, TF, p } { \int_compare:nTF { \int_mod:nn { #1 } { 400 } == 0 } { \prg_return_true: } { \bool_lazy_and:nnTF { \int_compare_p:n { \int_mod:nn { #1 } { 4 } == 0} } % divisible by 4 { !\int_compare_p:n { \int_mod:nn { #1 } { 100 } == 0 } } % but not by 100 { \prg_return_true: } { \prg_return_false: } } }

\ExplSyntaxOff

\begin{document}

\dothis{\month}

\dothis{2}

\dothis{3}

\dothis{4}

\dothis{15}

\dothis[2010]{8}

\dothis[2010]{2}

\dothis[2020]{2}

\dothis[2000]{2}

\dothis[1900]{2}

\end{document}

enter image description here

egreg
  • 1,121,712
1

You can omit \unexpanded and for the sake of complicatedness add detecting whether \the\year denotes a leap-year in case the month is 2/February:

\documentclass{article}
\usepackage{pdftexcmds}

\makeatletter @ifdefinable\stopromannumeral{\chardef\stopromannumeral=`^^00}% \newcommand{\dothis}[1]{% \romannumeral\str@case{#1}% {1}{31.01.\the\year}% {2}{.02.\the\year}% {3}{31.03.\the\year}% {4}{30.04.\the\year}% {5}{31.05.\the\year}% {6}{30.06.\the\year}% {7}{31.07.\the\year}% {8}{31.08.\the\year}% {9}{30.09.\the\year}% {10}{31.10.\the\year}% {11}{30.11.\the\year}% {12}{31.12.\the\year}% {#1}{[nada]}% \str@case@end }% \newcommand{\str@case}[3]{% \ifnum\pdf@strcmp{#1}{#2}=0 \expandafter@firstoftwo\else\expandafter@secondoftwo\fi {\str@case@end{#2}{#3}}{\str@case{#1}}% }% @ifdefinable\str@case@end{% \long\def\str@case@end#1#2#3\str@case@end{% \ifnum\pdf@strcmp{#1}{2}=0 \expandafter@firstoftwo\else\expandafter@secondoftwo\fi {% \ifnum\the\numexpr((\the\numexpr\the\year\relax)/4)4\relax=@firstofone{\the\numexpr\the\year\relax} % \expandafter@firstoftwo\else\expandafter@secondoftwo\fi {% \ifnum\the\numexpr((\the\numexpr\the\year\relax)/25)25\relax=@firstofone{\the\numexpr\the\year\relax} % \expandafter@firstoftwo\else\expandafter@secondoftwo\fi {% \ifnum\the\numexpr((\the\numexpr\the\year\relax)/400)400\relax=@firstofone{\the\numexpr\the\year\relax} % \expandafter@firstoftwo\else\expandafter@secondoftwo\fi }{@firstoftwo}% }{@secondoftwo}% {\stopromannumeral29}{\stopromannumeral28}% }% {\stopromannumeral}% #2% }% }% \makeatother

\begin{document}

\begingroup
\newcount\tempcnt
\let\year=\tempcnt
\year=1983
\dothis{2}\par
\year=1984
\dothis{2}\par
\year=1700
\dothis{2}\par
\year=1800
\dothis{2}\par
\year=1900
\dothis{2}\par
\year=2000
\dothis{2}\par
\year=2100
\dothis{2}\par
\endgroup    
\dothis{\the\month}\par
\dothis{2}\par
\dothis{3}\par
\dothis{4}\par

\end{document}

enter image description here

Ulrich Diez
  • 28,770
1

The following is inspired by egreg's answer:

If you don't need a leap-year conditional, then you can incorporate the leap-year-calculation into the code for calculating and displaying the last day of February—the following code is a wild mixture of \int_eval:n (integers) and \fp_eval:n (floating point)—with \fp_eval:n you can evaluate comparison-operators like = / != and boolean operators like && / ||:

\documentclass{article}

\ExplSyntaxOn

\NewExpandableDocumentCommand{\dothis}{om} { \ihpro_dothis:ee { \int_eval:n { \IfNoValueTF { #1 } { \c_sys_year_int } { #1 } } } { \int_eval:n { #2 } } }

\cs_new:Nn \ihpro_dothis:nn { \str_case_e:nnF {#2} { {1}{31.01.#1} {2}{ \int_eval:n{ % the \fp_eval-expression yields 1 for leap-years, 0 otherwise. \fp_eval:n { ( \int_mod:nn {#1}{4}=0 && \int_mod:nn{#1}{100}!=0 ) || \int_mod:nn {#1}{400}=0 } + 28 }.02.#1 } {3}{31.03.#1} {4}{30.04.#1} {5}{31.05.#1} {6}{30.06.#1} {7}{31.07.#1} {8}{31.08.#1} {9}{30.09.#1} {10}{31.10.#1} {11}{30.11.#1} {12}{31.12.#1} } {[nada]}% } \cs_generate_variant:Nn \ihpro_dothis:nn { ee }

\ExplSyntaxOff

\begin{document}

\dothis{\month}

\dothis{\the\month}

\dothis{2}

\dothis{3}

\dothis{4}

\dothis{15}

\dothis[2010]{8}

\dothis[2000]{2}

\dothis[1900]{2}

\dothis[1984]{2}

\dothis[1700]{2}

\dothis[1800]{2}

\dothis[1900]{2}

\dothis[2000]{2}

\dothis[2010]{2}

\dothis[2020]{2}

\dothis[2100]{2}

% This yields an error-message % ! Missing number, treated as zero : % \dothis{no number}

\end{document}

enter image description here

Ulrich Diez
  • 28,770