# How to typeset condition to show previous month?

\chead{\ifnum\day < 15
Mid-%
\else
End-%
\fi%
\monthname[\the\month]%
}


However, this isn't exactly what is required. What I need it to do is give the name of the previous month if it is the first 15 days of the month(ie. today would still be End-August) and give the current month if it is greater than 15.

Have tried creating a newcommand for \prevmonth but can't get that working.

\newcommand{\prevmonth}{\if\month=September%
August%
\fi%
}


Any help would be appreciated!

• Thank you both very much. I have accepted Steven's response because it allows for both prefixes which is a must. That said, both answers work and may be useful to other people in future. – swoop dewoop zoom Sep 14 '16 at 14:18
• There was no statement that Mid is still necessary. If this would have been explicitly stated, I would have provided another solution :-( – user31729 Sep 14 '16 at 15:59

Not sure about the revision of words like "Mid-" etc., but this variation of Christian's answer at the cited question may work:

\documentclass{article}

\usepackage{fancyhdr}
\usepackage[english]{babel}
\usepackage{datetime}
\usepackage{blindtext}

\pagestyle{fancy}
\fancyhf{}

End-%
\prevmonthname[\the\month]%
\else
End-%
\monthname[\the\month]%
\fi%
}
\def\prevmonthname[#1]{\ifcase#1%
\or December\or January\or February\or March\or April\or May%
\or June \or July \or August\or September\or October\or November\fi}

\begin{document}
\blindtext[5]
\clearpage
\day=10
\month=5
\blindtext[5]

\end{document}


Here is a version that takes Manuel's suggestion, but corrects for the "January" problem. By doing it this way, one is not locked into English.

\documentclass{article}

\usepackage{fancyhdr}
\usepackage[english]{babel}
\usepackage{datetime}
\usepackage{blindtext}

\pagestyle{fancy}
\fancyhf{}

End-%
\prevmonthname[\the\month]%
\else
End-%
\monthname[\the\month]%
\fi%
}
\def\prevmonthname[#1]{%
\ifnum#1=1\relax\prevmonthnamehelp[13]\else\prevmonthnamehelp[#1]\fi}
\def\prevmonthnamehelp[#1]{\monthname[\the\numexpr#1-1\relax]}

\begin{document}
\blindtext[5]
\clearpage
\day=10
\month=5
\blindtext[5]

\end{document}

• Wouldn't be more robust \def\prevmonthname[#1]{\monthname[\the\numexpr#1-1\relax]}? – Manuel Sep 14 '16 at 13:57
• @Manuel I think it would have the "January" problem that Christian struggle(s/ed) with. – Steven B. Segletes Sep 14 '16 at 14:09
• Is the "January" problem just a problem for people using other languages? – swoop dewoop zoom Sep 14 '16 at 14:21
• @swoopdewoopzoom No, the January problem is the fact that when using the syntax, \the\numexpr#1-1\relax (as suggested by Manuel), to obtain the previous month number, the month of January will generate a 0. Then, when trying to accomplish \monthname[0], the code breaks. The solution is to trap month number 1 as a special case, and treat it differently. – Steven B. Segletes Sep 14 '16 at 14:40

This assumes, that End - should always be the prefix ... and wraps around if the month is January and the number of day < 15.

\documentclass{article}

\usepackage{fancyhdr}
\usepackage[english]{babel}
\usepackage{datetime}
\usepackage{blindtext}

\newcounter{auxmonth}
\pagestyle{fancy}
\fancyhf{}

\setcounter{auxmonth}{\month}%
End - %
\ifnum\day < 15\relax
\ifnum\month = 1\relax
\setcounter{auxmonth}{12}% Wrap around
\else
\fi
\fi
\monthname[\theauxmonth]%
}

\begin{document}
\blindtext[5]
\clearpage

\day=10
\month=1
\blindtext[5]

\clearpage

\day=20
\month=1
\blindtext[5]

\end{document}

• Do you need the \expandafters? – Manuel Sep 14 '16 at 13:56
• @Manuel: It complained in a previous version, but I am using now another one – user31729 Sep 14 '16 at 13:59
• @ChristianHupfer I saw your profile, and am sad to see you go. best wishes. – Steven B. Segletes Oct 7 '16 at 0:04

if you are willing to switch to datetime2, the date can be calculated. Might be overkill, but it won't have any problems with January/Desember, and it will work with any language supported by the package.

### Code

\documentclass{article}

\usepackage{fancyhdr}
\usepackage[english]{babel}
\usepackage[calc]{datetime2}
\usepackage{lipsum}

\pagestyle{fancy}
\fancyhf{}

}

\newcount\daycount
\newcommand{\monthEnd}[1]{%
\DTMsaveddateoffsettojulianday{#1}{-15}\daycount
\DTMsavejulianday{newDate}{\number\daycount}
\DTMmonthname{\DTMfetchmonth{newDate}}
}
\DTMsavedate{ourDate}{2016-07-14}
\begin{document}
\lipsum[1-5]
\clearpage
\DTMsavedate{ourDate}{2016-07-16}
\lipsum[1-5]

\end{document}


Treat this as community wiki as the ideas here are mostly in the other solutions. Steven has created a \premonthname macro and Christian uses special counter auxmonth. Both of these seem unnecessary to me:

\documentclass{article}

\newcommand\MonthName[1]{%
\ifcase#1\or January\or February\or March\or April\or May\or June
\or July\or August\or September\or October\or November\or December%
\fi%
}
\newcommand\MyMonth{%
\the\day\space\MonthName\month $\longrightarrow$ % if illistration nly
\ifnum\day<15%
\ifnum\month=1 December
\else\MonthName{\numexpr\the\month-1}
\fi%
\else\MonthName\month\fi\newline%
}

\begin{document}\noindent
\day=3 \month=1   \MyMonth
\day=15 \month=1  \MyMonth
\day=3 \month=8   \MyMonth
\day=15 \month=8  \MyMonth
\day=3 \month=12  \MyMonth
\day=15 \month=12 \MyMonth
\end{document}

• One may object that your solution will only work for the English language, where as my 'unnecessary' counter approach will apply \monthname, which is language settings aware – user31729 Sep 14 '16 at 15:58
• @ChristianHupfer True enough, but easily addressed by using the datetime package and using the language aware \monthname It defines instead of my \MonthName. Your counter really is unnecessary:) – Andrew Sep 14 '16 at 21:48