Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I need to create a document that has section titles named "October 1, 2012", "October 2, 2012", etc., like this:

\section{October 1, 2012}
    This is text for the first day.
\section{October 2, 2012}
    This is text for the second day.

Is there a simple way to create a counter, set to the initial date, and then to increment it? E.g.:

 \section{\insertdate\incrementdate}
     This is text for the first day.
 \section{\insertdate\incrementdate}
     This is text for the second day.
  • This would need to know about leap years.
  • When displayed in the table of contents, this should match the sections.

In some situations, I may need to query the current value of year, month, and day, e.g.:

This is month \month and year \year.

This would display:

This is month 10 and year 2012.

In other situations, I may need to use the current value of year, month, and day, in a conditional, e.g.:

IF day IS 1, THEN:
    "This is the start of a new month."
ELSE:
    "It is not the start of a new month."

How can such a counter be created in ConTeXt?

share|improve this question
add comment

1 Answer 1

up vote 4 down vote accepted

Here is a simple implementation. The command \setupdate sets the current date (its syntax is same as \date command); the command \incrementdate increments the date; and the command \getdate typesets the current date.

You can access the current day, month, and year using \getdate[...], which accepts the same specification as \date. So \getdate[month] gives the name of the month (in the current language), \getdate[m] gives the number of the current month, \getdate[weekday] gives the day of the week, etc.

\unprotect

\newcount\daycounter
\newcount\monthcounter
\newcount\yearcounter

\unexpanded\def\setupdate
  {\dosingleargument\setup_date_aux}

\def\setup_date_aux[#1]%
  {\letdummyparameter\c!d\normalday
   \letdummyparameter\c!m\normalmonth
   \letdummyparameter\c!y\normalyear
   \getdummyparameters[#1]%
   \daycounter  \directdummyparameter\c!d\relax
   \monthcounter\directdummyparameter\c!m\relax
   \yearcounter \directdummyparameter\c!y\relax
  }

\unexpanded\def\incrementdate
  {\getdayspermonth\yearcounter\monthcounter
   \doifelse\numberofdays{\the\daycounter}
      {\daycounter\plusone
       \doifelse\!!twelve{\the\monthcounter}
          {\monthcounter\plusone
           \advance\yearcounter\plusone}
          {\advance\monthcounter\plusone}}
      {\advance\daycounter\plusone}}

\def\getdate
    {\date[d=\the\daycounter, m=\the\monthcounter, y=\the\yearcounter]}

\protect

\setupdate[d={25},m={12},y={2011}]

\starttext
\placecontent
\dorecurse{200}
{\expanded{\section{\getdate}}
 The current month is \getdate[month] \getdate[weekday]
 \incrementdate}
\stoptext

As can be seen from the output of the above example, this implementation correctly handles leap years and works in section titles (provided you use \expanded).

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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