LaTeX Recreating a \monthname macro from scratch using \ifcase

  • Thread starter Thread starter Eclair_de_XII
  • Start date Start date
  • Tags Tags
    scratch
AI Thread Summary
The discussion revolves around the use of the \ifcase and \or macros in LaTeX for defining a month based on the current month number. The main issue identified is that the \ifcase macro does not evaluate conditions but rather selects a case based on the index value of \month, which leads to an off-by-one error. When \month is set to 3, it executes the fourth condition, resulting in an unexpected output. To resolve this, the code should be adjusted to ensure that the correct month names correspond to their respective indices, starting from 0. An example modification is provided, illustrating how to correctly implement the month selection logic.
Eclair_de_XII
Messages
1,082
Reaction score
91
TL;DR Summary
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]
 
Back
Top