Recreating a \monthname macro from scratch using \ifcase

  • Context: LaTeX 
  • Thread starter Thread starter Eclair_de_XII
  • Start date Start date
  • Tags Tags
    scratch
Click For Summary
SUMMARY

The discussion focuses on recreating a \monthname macro in LaTeX using the \ifcase command. The primary issue arises from the incorrect use of the \ifcase macro, which selects cases based on the value of \month, leading to an off-by-one error. The solution involves redefining the macro to correctly map the month values to their corresponding names, starting the case list at zero. The corrected macro definition is provided, ensuring accurate month representation.

PREREQUISITES
  • Understanding of LaTeX macro definitions
  • Familiarity with the \ifcase and \or commands in LaTeX
  • Basic knowledge of conditional expressions in programming
  • Experience with debugging LaTeX code
NEXT STEPS
  • Study LaTeX macro programming techniques
  • Learn about the \ifcase command and its proper usage
  • Explore debugging strategies for LaTeX documents
  • Investigate other conditional commands in LaTeX, such as \ifthenelse
USEFUL FOR

LaTeX users, macro developers, and anyone looking to enhance their skills in conditional programming within LaTeX documents.

Eclair_de_XII
Messages
1,082
Reaction score
91
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]
 

Similar threads

Replies
5
Views
3K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 75 ·
3
Replies
75
Views
7K
  • · Replies 67 ·
3
Replies
67
Views
16K
Replies
12
Views
3K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 27 ·
Replies
27
Views
5K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 7 ·
Replies
7
Views
4K
  • · Replies 7 ·
Replies
7
Views
2K