[Tex/LaTex] Expanding command in \addchaptertocentry

datetimeexpansionscrbook

I'm using a counter to increment months in a journal and the datetime package to turn the counter into months. The months then become chapter titles in the document, so I have something like this:

\documentclass{scrbook}

\usepackage[french]{babel}
\usepackage{datetime}
\usepackage{hyperref}
\usepackage{lipsum}

\newcounter{month}

\newcommand{\newmonth}{%
  \addtocounter{month}{1}%
  \chapter{\monthname[\themonth]}
} 

\begin{document}

\newmonth

\lipsum

\newmonth

\lipsum

\end{document}

The month appears ok in the text, but not in the PDF index. I tracked down this problem to \addchaptertocentry, since I can reproduce it by calling \addchaptertocentry{\monthname[\themonth]}.

The .aux file contains the command verbatim:

\@writefile{toc}{\contentsline {chapter}{\numberline {1}\monthname [1]}{1}{chapter.1}}

How can I make it so that the \monthname command is expanded so that the PDF index looks right?

Edit:

My reason for using datetime here is to support multiple languages (French in this example), so I would rather avoid redefining the month list.

Best Answer

If you don't need the optional argument which are making it non-expandable you could do

This version with language tests and french

\documentclass{scrbook}

\usepackage[french]{babel}

\usepackage{datetime}
\usepackage{hyperref}
\usepackage{lipsum}



\newcounter{month}

\makeatletter

\def\@orgargctr=#1\relax\ifcase\@orgargctr{\ifcase#1 }
\expandafter\def\expandafter\monthname\expandafter{\csname\string\monthname\endcsname[\the\c@month]}

\expandafter\def\csname\string\monthname\endcsname[#1]{%
\if@dt@multilingual
\@ifundefined {monthname\languagename}%
{\csname \string\monthname english \endcsname [#1]}%
{\csname \string\monthname\languagename \endcsname [#1]}%
\else 
\csname\string\monthnameenglish\endcsname [#1]\fi}

%\expandafter\def\expandafter\monthnameenglish\expandafter{\csname\string\monthnameenglish\endcsname}
\makeatother

\newcommand{\newmonth}{%
  \addtocounter{month}{1}%
  \chapter{\monthname}
} 

\begin{document}



\newmonth

\lipsum

\newmonth


\lipsum

\end{document}
Related Question