Recreating a \monthname macro from scratch using \ifcase

  • Context: LaTeX 
  • Thread starter Thread starter Eclair_de_XII
  • Start date Start date
  • Tags Tags
    scratch
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
2 replies · 2K views
Eclair_de_XII
Messages
1,085
Reaction score
92
TL;DR
Basically I want to recreate a \monthname macro from scratch using the (\ifcase,\or,\fi) set of macros. The problem is that my macro keeps implying that the month is April on the second input line, when TeX says that the month number is 3 from the first output line.
[CODE highlight="18,20"]\def\themonth{%
\ifcase%
\month=1 {\the\month}%
\or \month=2 {\the\month}%
\or \month=3 {\the\month}%
\or \month=4 {\the\month}%
\or \month=5 {\the\month}%
\or \month=6 {\the\month}%
\or \month=7 {\the\month}%
\or \month=8 {\the\month}%
\or \month=9 {\the\month}%
\or \month=10 {\the\month}%
\or \month=11 {\the\month}%
\or \month=12 {\the\month}%
\fi%
}

This is Month \the\month\ of the Year \the\year.

This is Month \themonth{} of the Year \the\year.[/CODE]

What is the problem, here? Do the (\ifcase,\or) macros not work with numerical Boolean tests or something? Using the \month parameter in this macro seems to increment it by one for some reason, and adding a line to \advance\month by -1 within the macro definition does not seem to have any effect. Using the parameter in a generic macro (like \def\three{\the\month}) does not seem to affect the printed value of \month. Why is that?

Edit: Changed all month names to {\the\month} for easier trouble-shooting.
 
Last edited:
Physics news on Phys.org
You are using ifcase incorrectly. You do not test for conditions, but test for the value after ifcase and it selects the corresponding case depending on its value. See https://tex.stackexchange.com/questions/17676/conditional-cases-expression

In your case, you are checking the value of \month, which is 3, so it executes the 4th condition (counting starts at 0), which is
Code:
\month=4 {\the\month}
so its sets month to 4 before returning its value.

Your code needs to be modified to something like
Code:
\def\themonth{%
\ifcase\month%
impossible%
\or January%
\or February%
...
\fi%
}
 
Oh, so it basically just chooses the case from the case list corresponding to the index \month.

I don't know how to explain this well. This is my best interpretation of it.

Python:
month=3
list_of_months=[None,1,2,3,4,5,6,7,8,9,10,11,12]

def ifcase(month):
    return list_of_months[month]