4

I am trying to create an academic calendar for myself. I am trying to fill the fall semester in orange color but the edges of the blocks are skewed. Please help me in positioning the edges either in vertically or horizontally. I would be happy, if you can help me in simplifying the code if I have some superfluous parts.

\documentclass[10pt]{standalone}

\usepackage{tikz}

\usetikzlibrary{calendar}

\begin{document}

\begin{tikzpicture}
\matrix[column sep=1em, row sep=1em]{
  \calendar (m09) [dates=2018-09-01 to 2018-09-last,week list,month label above centered]; &
  \calendar (m10) [dates=2018-10-01 to 2018-10-last,week list,month label above centered]; &
  \calendar (m11) [dates=2018-11-01 to 2018-11-last,week list,month label above centered]; &
  \calendar (m12) [dates=2018-12-01 to 2018-12-last,week list,month label above centered]; \\
  \calendar (m01) [dates=2019-01-01 to 2019-01-last,week list,month label above centered]; &
  \calendar (m02) [dates=2019-02-01 to 2019-02-last,week list,month label above centered]; &
  \calendar (m03) [dates=2019-03-01 to 2019-03-last,week list,month label above centered]; &
  \calendar (m04) [dates=2019-04-01 to 2019-04-last,week list,month label above centered]; \\
};
\draw[red,thick,fill=orange,opacity=0.5] (m09-2018-09-17.north west) -- (m09-2018-09-23.north east)
  -- (m09-2018-09-30.south east) -- (m09-2018-09-24.south west) -- cycle;
\draw[red,thick,fill=orange,opacity=0.5] (m10-2018-10-01.north west) -- (m10-2018-10-07.north east)
  -- (m10-2018-10-28.south east) -- (m10-2018-10-24.south east) -- (m10-2018-10-31.south east) -- (m10-2018-10-29.south west) -- cycle;
\draw[red,thick,fill=orange,opacity=0.5] (m11-2018-11-05.north west) -- (m11-2018-11-08.north west) -- (m11-2018-11-01.north west)
  -- (m11-2018-11-04.north east) -- (m11-2018-11-25.south east) -- (m11-2018-11-23.south east) -- (m11-2018-11-30.south east)
  -- (m11-2018-11-26.south west)-- cycle;
\draw[red,thick,fill=orange,opacity=0.5] (m12-2018-12-03.north west) -- (m12-2018-12-08.north west) -- (m12-2018-12-01.north west)
  -- (m12-2018-12-02.north east) -- (m12-2018-12-30.south east) -- (m12-2018-12-24.south east) -- (m12-2018-12-31.south east)
  -- (m12-2018-12-31.south west)-- cycle;
\draw[red,thick,fill=orange,opacity=0.5] (m01-2019-01-07.north west) -- (m01-2019-01-08.north west) -- (m01-2019-01-01.north west)
  -- (m01-2019-01-06.north east) -- (m01-2019-01-27.south east) -- (m01-2019-01-24.south east) -- (m01-2019-01-31.south east)
  -- (m01-2019-01-28.south west)-- cycle;
\draw[red,thick,fill=orange,opacity=0.5] (m02-2019-02-01.north west) -- (m02-2019-02-03.north east) -- (m02-2019-02-03.south east)
  -- (m02-2019-02-01.south west)-- cycle;
\end{tikzpicture}

\end{document} 

enter image description here

  • 2
    The problem is the 1 narrower than the 31. Add a minimum width to the matrix: \matrix[column sep=1em, row sep=1em, minimum width=2em]. – Phelype Oleinik Oct 5 '18 at 11:19
  • Is it possible to use 01 instead of 1? – bkarpuz Oct 5 '18 at 11:21
  • Yes, it is. The calendar library defines some goodies to format the date. See the TikZ manual for some examples. – Phelype Oleinik Oct 5 '18 at 11:52
6

The problem is that the first days make a narrower node than the last ones.

The TikZy way of doing this would be to require a minimum width for the nodes:

\matrix[column sep=1em, row sep=1em, minimum width = 2em]{...}

this, of course, requires some trial-and-error to get the width right. No one likes that, right?

So browsing the code of tikzlibrarycalendar.code.tex I found that the calendar printing routine has some hooks before and after the day. The one that we want here is the day text option, which defines some formats for the day (there are equivalents for the month and the year).

The solution that would do (approximately) the same as the minimum width option would be:

\matrix[column sep=1em, row sep=1em, day text = {\%d=}]{...}

This makes the date printed in a constant width box. If you want days less than 10 prepended with a 0, you can use:

\matrix[column sep=1em, row sep=1em, day text = {\%d0}]{...}

With either solution, the filled regions are correct:

enter image description here

Full code:

\documentclass[10pt]{standalone}

\usepackage{tikz}

\usetikzlibrary{calendar}

\begin{document}

\begin{tikzpicture}%                 v---------------v Just added this
\matrix[column sep=1em, row sep=1em, day text = {\%d0}]{
  \calendar (m09) [dates=2018-09-01 to 2018-09-last,week list,month label above centered]; &
  \calendar (m10) [dates=2018-10-01 to 2018-10-last,week list,month label above centered]; &
  \calendar (m11) [dates=2018-11-01 to 2018-11-last,week list,month label above centered]; &
  \calendar (m12) [dates=2018-12-01 to 2018-12-last,week list,month label above centered]; \\
  \calendar (m01) [dates=2019-01-01 to 2019-01-last,week list,month label above centered]; &
  \calendar (m02) [dates=2019-02-01 to 2019-02-last,week list,month label above centered]; &
  \calendar (m03) [dates=2019-03-01 to 2019-03-last,week list,month label above centered]; &
  \calendar (m04) [dates=2019-04-01 to 2019-04-last,week list,month label above centered]; \\
};
\draw[red,thick,fill=orange,opacity=0.5] (m09-2018-09-17.north west) -- (m09-2018-09-23.north east)
  -- (m09-2018-09-30.south east) -- (m09-2018-09-24.south west) -- cycle;
\draw[red,thick,fill=orange,opacity=0.5] (m10-2018-10-01.north west) -- (m10-2018-10-07.north east)
  -- (m10-2018-10-28.south east) -- (m10-2018-10-24.south east) -- (m10-2018-10-31.south east) -- (m10-2018-10-29.south west) -- cycle;
\draw[red,thick,fill=orange,opacity=0.5] (m11-2018-11-05.north west) -- (m11-2018-11-08.north west) -- (m11-2018-11-01.north west)
  -- (m11-2018-11-04.north east) -- (m11-2018-11-25.south east) -- (m11-2018-11-23.south east) -- (m11-2018-11-30.south east)
  -- (m11-2018-11-26.south west)-- cycle;
\draw[red,thick,fill=orange,opacity=0.5] (m12-2018-12-03.north west) -- (m12-2018-12-08.north west) -- (m12-2018-12-01.north west)
  -- (m12-2018-12-02.north east) -- (m12-2018-12-30.south east) -- (m12-2018-12-24.south east) -- (m12-2018-12-31.south east)
  -- (m12-2018-12-31.south west)-- cycle;
\draw[red,thick,fill=orange,opacity=0.5] (m01-2019-01-07.north west) -- (m01-2019-01-08.north west) -- (m01-2019-01-01.north west)
  -- (m01-2019-01-06.north east) -- (m01-2019-01-27.south east) -- (m01-2019-01-24.south east) -- (m01-2019-01-31.south east)
  -- (m01-2019-01-28.south west)-- cycle;
\draw[red,thick,fill=orange,opacity=0.5] (m02-2019-02-01.north west) -- (m02-2019-02-03.north east) -- (m02-2019-02-03.south east)
  -- (m02-2019-02-01.south west)-- cycle;
\end{tikzpicture}

\end{document}

As Marmot noticed, you are drawing with perpendicular lines, so you can switch from -- to the -| syntax. You can also define some styles to avoid repetition:

\documentclass[10pt]{standalone}

\usepackage{tikz}

\usetikzlibrary{calendar}

\begin{document}

\begin{tikzpicture}[
  calendar fill/.style = {
    red,thick,fill=orange,opacity=0.5
  },
  fullmonth/.style = {
    dates = #1-01 to #1-last
  },
  every calendar/.style = {week list, month label above centered},
]
\matrix[column sep=1em, row sep=1em, day text = {\%d0}]{
  \calendar (m09) [fullmonth = 2018-09]; &
  \calendar (m10) [fullmonth = 2018-10]; &
  \calendar (m11) [fullmonth = 2018-11]; &
  \calendar (m12) [fullmonth = 2018-12]; \\
  \calendar (m01) [fullmonth = 2019-01]; &
  \calendar (m02) [fullmonth = 2019-02]; &
  \calendar (m03) [fullmonth = 2019-03]; &
  \calendar (m04) [fullmonth = 2019-04]; \\
};
\draw[calendar fill] (m09-2018-09-17.north west) -| (m09-2018-09-30.south east) -| cycle;
\draw[calendar fill] (m10-2018-10-01.north west) -| (m10-2018-10-28.south east) -| (m10-2018-10-31.south east) -| cycle;
\draw[calendar fill] (m11-2018-11-05.north west) -| (m11-2018-11-01.north west) -| (m11-2018-11-25.south east) -| (m11-2018-11-30.south east) -| cycle;
\draw[calendar fill] (m12-2018-12-03.north west) -| (m12-2018-12-01.north west) -| (m12-2018-12-30.south east) -| (m12-2018-12-31.south east) -| cycle;
\draw[calendar fill] (m01-2019-01-07.north west) -| (m01-2019-01-01.north west) -| (m01-2019-01-27.south east) -| (m01-2019-01-31.south east) -| cycle;
\draw[calendar fill] (m02-2019-02-01.north west) -| (m02-2019-02-03.south east) -| cycle;
\end{tikzpicture}

\end{document}
  • Note that every other coordinate is redundant (I also noticed that just now) if you switch to the -| and |- syntax instead of --. – user121799 Oct 5 '18 at 12:07
  • @PhelypeOleinik Sorry for having the same idea for the \ContinuedFloat question! I also have a second approach, if you would undelete your nice answer I can simply remove the part about \ContinuedFloat from my answer. – user36296 Oct 5 '18 at 12:10
  • @marmot Good catch! I updated my answer with these changes. Thanks – Phelype Oleinik Oct 5 '18 at 12:26
  • @samcarter No worries, really. If I recall correctly, I also answered a question seconds before you some time ago. We can call it even :) – Phelype Oleinik Oct 5 '18 at 12:28
6

Here is a rather clumsy way to fix it. The strategy is that, whenever a 1-digit node is used at the left, I mix its coordinate with the x coordinate of a 2-digit node in the same column. E.g. (m12-2018-12-03.north west) becomes (m12-2018-12-03.north -| m12-2018-12-10.west).

\documentclass[10pt]{standalone}

\usepackage{tikz}

\usetikzlibrary{calendar,backgrounds}

\begin{document}

\begin{tikzpicture}
\matrix[column sep=1em, row sep=1em]{
  \calendar (m09) [dates=2018-09-01 to 2018-09-last,week list,month label above centered]; &
  \calendar (m10) [dates=2018-10-01 to 2018-10-last,week list,month label above centered]; &
  \calendar (m11) [dates=2018-11-01 to 2018-11-last,week list,month label above centered]; &
  \calendar (m12) [dates=2018-12-01 to 2018-12-last,week list,month label above centered]; \\
  \calendar (m01) [dates=2019-01-01 to 2019-01-last,week list,month label above centered]; &
  \calendar (m02) [dates=2019-02-01 to 2019-02-last,week list,month label above centered]; &
  \calendar (m03) [dates=2019-03-01 to 2019-03-last,week list,month label above centered]; &
  \calendar (m04) [dates=2019-04-01 to 2019-04-last,week list,month label above centered]; \\
};
\begin{scope}[on background layer]
 \draw[red,thick,fill=orange,opacity=0.5] (m09-2018-09-17.north west) -- (m09-2018-09-23.north east)
   -- (m09-2018-09-30.south east) -- (m09-2018-09-24.south west) -- cycle;
 \draw[red,thick,fill=orange,opacity=0.5] (m10-2018-10-01.north -|
 m10-2018-10-29.west) -- (m10-2018-10-07.north east)
   -- (m10-2018-10-28.south east) -- (m10-2018-10-24.south east) -- (m10-2018-10-31.south east) -- (m10-2018-10-29.south west) -- cycle;
 \draw[red,thick,fill=orange,opacity=0.5] (m11-2018-11-05.north -|
 m11-2018-11-26.west) -- (m11-2018-11-08.north west) -- (m11-2018-11-01.north west)
   -- (m11-2018-11-04.north east) -- (m11-2018-11-25.south east) -- (m11-2018-11-23.south east) -- (m11-2018-11-30.south east)
   -- (m11-2018-11-26.south west)-- cycle;
 \draw[red,thick,fill=orange,opacity=0.5] (m12-2018-12-03.north -|
 m12-2018-12-10.west) -- (m12-2018-12-08.north west) -- (m12-2018-12-01.north west)
   -- (m12-2018-12-02.north east) -- (m12-2018-12-30.south east) -- (m12-2018-12-24.south east) -- (m12-2018-12-31.south east)
   -- (m12-2018-12-31.south west)-- cycle;
 \draw[red,thick,fill=orange,opacity=0.5] (m01-2019-01-07.north -|
 m01-2019-01-14.west) -- (m01-2019-01-08.north west) -- (m01-2019-01-01.north west)
   -- (m01-2019-01-06.north east) -- (m01-2019-01-27.south east) -- (m01-2019-01-24.south east) -- (m01-2019-01-31.south east)
   -- (m01-2019-01-28.south west)-- cycle;
 \draw[red,thick,fill=orange,opacity=0.5] (m02-2019-02-01.north west) -- (m02-2019-02-03.north east) -- (m02-2019-02-03.south east)
   -- (m02-2019-02-01.south west)-- cycle;
\end{scope}
\end{tikzpicture}
\end{document}

A simpler solution derives from the fact that anyway every other coordinate in your contour is unnecessary. So by making your code shorter you automatically fix the problem.

\documentclass[10pt]{standalone}

\usepackage{tikz}

\usetikzlibrary{calendar,backgrounds}

\begin{document}

\begin{tikzpicture}
\matrix[column sep=1em, row sep=1em]{
  \calendar (m09) [dates=2018-09-01 to 2018-09-last,week list,month label above centered]; &
  \calendar (m10) [dates=2018-10-01 to 2018-10-last,week list,month label above centered]; &
  \calendar (m11) [dates=2018-11-01 to 2018-11-last,week list,month label above centered]; &
  \calendar (m12) [dates=2018-12-01 to 2018-12-last,week list,month label above centered]; \\
  \calendar (m01) [dates=2019-01-01 to 2019-01-last,week list,month label above centered]; &
  \calendar (m02) [dates=2019-02-01 to 2019-02-last,week list,month label above centered]; &
  \calendar (m03) [dates=2019-03-01 to 2019-03-last,week list,month label above centered]; &
  \calendar (m04) [dates=2019-04-01 to 2019-04-last,week list,month label above centered]; \\
};
\begin{scope}[on background layer]
 \draw[red,thick,fill=orange,opacity=0.5] (m09-2018-09-17.north west) -- (m09-2018-09-23.north east)
   -- (m09-2018-09-30.south east) -- (m09-2018-09-24.south west) -- cycle;
 \draw[red,thick,fill=orange,opacity=0.5]  (m10-2018-10-07.north east)
   |- (m10-2018-10-24.south east) |- (m10-2018-10-29.south west) |- cycle;
 \draw[red,thick,fill=orange,opacity=0.5]  (m11-2018-11-08.north west) 
 |- (m11-2018-11-04.north east) |- (m11-2018-11-23.south east) 
 |- (m11-2018-11-26.south west) |- cycle;
 \draw[red,thick,fill=orange,opacity=0.5]  (m12-2018-12-08.north west)
 |- (m12-2018-12-02.north east) |- (m12-2018-12-24.south east) |- (m12-2018-12-31.south west) |- cycle;
 \draw[red,thick,fill=orange,opacity=0.5]  (m01-2019-01-08.north west) 
 |- (m01-2019-01-06.north east) |- (m01-2019-01-24.south east) 
 |- (m01-2019-01-28.south west) |- cycle;
 \draw[red,thick,fill=orange,opacity=0.5] (m02-2019-02-01.north west) -- (m02-2019-02-03.north east) -- (m02-2019-02-03.south east)
   -- (m02-2019-02-01.south west)-- cycle;
\end{scope}
\end{tikzpicture}
\end{document} 

enter image description here

  • 1
    requires \end{document} – bkarpuz Oct 5 '18 at 11:58

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.