Recreating a \monthname macro from scratch using \ifcase

  • LaTeX
  • Thread starter Eclair_de_XII
  • Start date
  • Tags
    scratch
In summary: This is not entirely accurate or complete, but it gives an idea of how ifcase works. Essentially, it takes the value of \month and uses it as an index to select the corresponding case from the list_of_months. In your case, you are setting month to 4 before returning its value, which is why it increments by one.
  • #1
Eclair_de_XII
1,083
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:
\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.

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
  • #2
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%
}
 
  • #3
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]
 

FAQ: Recreating a \monthname macro from scratch using \ifcase

What is the purpose of recreating a \monthname macro from scratch using \ifcase?

The purpose of recreating a \monthname macro from scratch using \ifcase is to create a more flexible and efficient method for displaying the names of months in a document. This method allows for more customization and control over the formatting and presentation of the month names.

How does the \ifcase function work in recreating the \monthname macro?

The \ifcase function is a conditional statement that checks the value of a given variable and executes a specific code block based on that value. In recreating the \monthname macro, the \ifcase function is used to determine which month name to display based on the month number provided.

Can the \monthname macro be customized to display month names in a different language?

Yes, the \monthname macro can be customized to display month names in a different language by modifying the \ifcase statements and replacing the month names with the corresponding names in the desired language.

Are there any limitations to using \ifcase in recreating the \monthname macro?

One limitation of using \ifcase in recreating the \monthname macro is that it can only handle integer values for the month number. This means that if a non-integer value is provided, the macro will not function correctly.

How can I further improve the \monthname macro using \ifcase?

There are several ways to further improve the \monthname macro using \ifcase. One way is to add additional conditions to handle edge cases such as leap years or special formatting for certain months. Another way is to combine \ifcase with other functions or macros to add more functionality and versatility to the macro.

Similar threads

Replies
75
Views
5K
2
Replies
67
Views
12K
Replies
27
Views
4K
Replies
7
Views
3K
Replies
7
Views
3K
Replies
2
Views
7K
Back
Top