Example: Changing the default calendar layout

Published 2010-03-31 | Author: Berteun Damman

An example how to modify the calendar drawing code so Sundays are drawn as the first day of the week; it also shows how to make your own conditions to be used with ifdate. In this example I use matrices to group the months.

Download as: [PDF] [TEX]  •  [Open in Overleaf]

Changing the default calendar layout

Do you have a question regarding this example, TikZ or LaTeX in general? Just ask in the LaTeX Forum.
Oder frag auf Deutsch auf TeXwelt.de. En français: TeXnique.fr.

\documentclass{article}
% An example how to use the calendar library and modify the layout, i.e. put
% Sunday as the first week day.
%
% Author: Berteun Damman
\usepackage{tikz}
\usetikzlibrary{calendar}
\begin{document}
    \makeatletter

    % This way you can define your own conditions, for example, you
    % could make something as `full moon', `even week', `odd week',
    % et cetera. In principle. The math in TeX could be hard.
    \pgfkeys{/pgf/calendar/start of year/.code={%
        \ifnum\pgfcalendarifdateday=1\relax%
            \ifnum\pgfcalendarifdatemonth=1\relax\pgfcalendarmatchestrue\fi%
        \fi%
    }}%

    % Define our own style
    \tikzstyle{week list sunday}=[
        % Note that we cannot extend from week list,
        % the execute before day scope is cumulative
        execute before day scope={%
               \ifdate{day of month=1}{\ifdate{equals=\pgfcalendarbeginiso}{}{
               % On first of month, except when first date in calendar.
                   \pgfmathsetlength{\pgf@y}{\tikz@lib@cal@month@yshift}%
                   \pgftransformyshift{-\pgf@y}
               }}{}%
        },
        execute at begin day scope={%
            % Because for TikZ Monday is 0 and Sunday is 6,
            % we can't directly use \pgfcalendercurrentweekday,
            % but instead we define \c@pgf@counta (basically) as:
            % (\pgfcalendercurrentweekday + 1) % 7
            \pgfmathsetlength\pgf@x{\tikz@lib@cal@xshift}%
            \ifnum\pgfcalendarcurrentweekday=6
                \c@pgf@counta=0
            \else
                \c@pgf@counta=\pgfcalendarcurrentweekday
                \advance\c@pgf@counta by 1
            \fi
            \pgf@x=\c@pgf@counta\pgf@x
            % Shift to the right position for the day.
            \pgftransformxshift{\pgf@x}
        },
        execute after day scope={
            % Week is done, shift to the next line.
            \ifdate{Saturday}{
                \pgfmathsetlength{\pgf@y}{\tikz@lib@cal@yshift}%
                \pgftransformyshift{-\pgf@y}
            }{}%
        },
        % This should be defined, glancing from the source code.
        tikz@lib@cal@width=7
    ]

    % New style for drawing the year, it is always drawn
    % for January
    \tikzstyle{year label left}=[
        execute before day scope={
            \ifdate{start of year}{
                \drawyear
            }{}
        },
        % Right align
        every year/.append style={
            anchor=east,
        }
    ]

    % Style to force giving a month a year label.
    \tikzset{draw year/.style={
        execute before day scope={
            \ifdate{day of month=1}{\drawyear}{}
        }
    }}

    % This actually draws the year.
    \newcommand{\drawyear}{
        \pgfmathsetlength{\pgf@x}{\tikz@lib@cal@xshift}%
        \pgftransformxshift{-\pgf@x}
        % \tikzyearcode is defined by default
        \tikzyearcode
        \pgfmathsetlength{\pgf@x}{\tikz@lib@cal@xshift}%
        \pgftransformxshift{\pgf@x}
    }

    \makeatother

    % The actual calendar is now rather easy:
    \begin{tikzpicture}[every calendar/.style={
            month label above centered,
            month text={\textit{\%mt}},
            year label left,
            every year/.append style={font=\Large\sffamily\bfseries,
                green!50!black},
            if={(Sunday) [blue!70]},
            week list sunday,
        }]
        \matrix[column sep=1em, row sep=1em] {
            \calendar[dates=2010-04-01 to 2010-04-last,draw year]; &
            \calendar[dates=2010-05-01 to 2010-05-last]; &
            \calendar[dates=2010-06-01 to 2010-06-last]; \\
            \calendar[dates=2010-07-01 to 2010-07-last]; &
            \calendar[dates=2010-08-01 to 2010-08-last]; &
            \calendar[dates=2010-09-01 to 2010-09-last]; \\
            \calendar[dates=2010-10-01 to 2010-10-last]; &
            \calendar[dates=2010-11-01 to 2010-11-last]; &
            \calendar[dates=2010-12-01 to 2010-12-last]; \\
            \calendar[dates=2011-01-01 to 2011-01-last]; &
            \calendar[dates=2011-02-01 to 2011-02-last]; &
            \calendar[dates=2011-03-01 to 2011-03-last]; \\
        };
    \end{tikzpicture}
\end{document}

Comments

  • #1 CriS, March 31, 2010 at 6:02 p.m.

    Very impressive example, thanks for sharing. I also tried some customization of calendar, my humble attempts are here: http://teatimeandlayout.wordpress.com/2010/03/01/creare-un-calendario-con-latex-iii/ The text is in Italian, but the code is international, of course :-)

    Ciao, CriS

  • #2 tallador, April 8, 2010 at 11:20 p.m.

    Cris, Excellent your page and very useful the calendar code. Just that i need. thanks for your post

  • #3 Stephen Lange, August 26, 2010 at 9:33 p.m.

    Very, very impressive. I figured out how to add a circle around a specific date. I was wondering if someone could post how to add a square, triangle, diamond or any other shape you might think of around a date.

    I'll admit my attempts utterly failed. I want to use this to put highlight upcoming event types for my son's Cub Scout pack. Different colored circles isn't cutting it since I have to print them out in black and white.

    Really appreciate any help. Thanks in advance.

  • #4 CriS, September 10, 2010 at 3:09 p.m.

    @ Stephen Lange: I think there are two ways to solve your problem:

    1) since every day can be referred to as mycal-yyyy-mm-dd, where mycal is the name you gave to your calendar (example: \calendar(mycal)[dates=\the\year-#1-01 to \the\year-#1-last];), you can draw a shape around the day (example: \draw[thick] (mycal-2011-01-20) circle (8pt); or, using the shapes.geometric library, \node[star,draw,minimum size=.8cm,star point ratio=1.65,thick] at (mycal-2011-08-29) {}; )

    2) you can use the if specification of \calendar (example: if (equals=08-31) [nodes={circle,draw,thick}]).

    Hoping this helps,

    CriS

Adding comments is currently not enabled.