4

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.

2
  • Note that the month names defined by datetime for French have lowercase initial.
    – egreg
    Sep 13, 2012 at 18:57
  • @egreg: I did note that, and it's been another issue….
    – raphink
    Sep 13, 2012 at 19:22

1 Answer 1

5

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}
5
  • My main reason for using datetime is to support multiple languages so I can create a generic journal class and rely on babel+datetime for month and dates translations. For this reason, redefining \monthname doesn't really help me.
    – raphink
    Sep 13, 2012 at 17:56
  • It's possible to make an expandable test for optional arguments, eg bm package has one but there are always some restrictions, If you could make your MWE a bit less minimal and have a language switch or some such it should be possible to redefine it to cope. You have to redefine \monthnaame or use something else as the definition you have doesn't work in expansion contexts Sep 13, 2012 at 18:32
  • Just adding \usepackage[french]{babel} to the beginning of the preamble switches the month names to French.
    – raphink
    Sep 13, 2012 at 18:36
  • I edited the question.
    – raphink
    Sep 13, 2012 at 18:42
  • I added a french answer Sep 13, 2012 at 18:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.