tikzcalendar 日期名称和锚点

tikzcalendar 日期名称和锚点

我正在使用tikzcalendar并且我想要:

  1. 在顶部插入星期几的名称(可以缩写为星期一、星期二、星期三等等)
  2. 自动地插入垂直虚线来分隔周末各天。

在接下来的 MWE 中,我在(2)上取得了一些进展,但这并不好,因为我必须通知何时第一个星期五日历发生并使用该节点作为参考。

当日期改变时,我必须再次编辑代码。

在此处输入图片描述

平均能量损失

\documentclass[12pt]{report}
\usepackage[a4paper,landscape,margin=1cm]{geometry}
\usepackage{tikz}
\usetikzlibrary{calendar,calc}

\begin{document}\centering
\begin{tikzpicture}[%
  every day/.style={anchor=mid,opacity=1,minimum width=2.5ex},%
  every node/.style={inner sep=2pt,rectangle}%
]
\calendar (cal) [dates=2018-06-01 to 2018-12-10,
  month list,%
  month label left,%
  month text=\textcolor{black}{\%mt},%
  month yshift=1.7em,%
  day xshift=3.5ex,%
  ]
  if (Sunday) [every day/.append style={font=\slshape},red]
  ;
  %%
  %% we need to use 1st Friday node to compute the (up) node
  %%
  \node (up) at ($(cal-2018-06-01.north east)!.5!(cal-2018-06-02.north west)$ ) {};
  %%
  %% we need to use last month 1st Friday node to compute the (down) node
  %%
  \node (down) at (%
   %% repeat (up) node
   {$(cal-2018-06-01.north east)!.5!(cal-2018-06-02.north west)$}%
   |-%
   {$(cal-2018-12-01.south east)!.5!(cal-2018-12-02.south west)$}%
   ) {};
  %%
  %% dashed lines from (up) to (down)
  %%
  \foreach \i in {0,...,4}{% <-- the number could change
  \draw[dashed]%
    ([xshift=3.5*7*\i ex]up.center)--([xshift=3.5*7*\i ex]down.center)
    ([xshift=3.5*7*\i ex + 7ex]up.center)--([xshift=3.5*7*\i ex + 7ex]down.center)
  ;
  }
\end{tikzpicture}
\end{document}

答案1

改编:

  • 添加参数\dateFrom, \dateTo,\xDist
  • 添加了函数\dayOfWeek,可以进行模数计算并选择星期几
  • 到处使用新参数
  • 定义coordinate(up)(down)

评论:

因为一周总是从星期一开始,而且如果所选月份开始时没有星期一,还会有剩余空间,所以有一个神奇的数字4.5用于获取第一个星期五的 x 位置。如果从另一天开始,则必须更改此值。

代码:

\documentclass[12pt]{report}
\usepackage[a4paper,landscape,margin=1cm]{geometry}
\usepackage{tikz}
\usetikzlibrary{calendar,calc}

\usepackage{ifthen}
\usepackage[nomessages]{fp}% http://ctan.org/pkg/fp

\newcommand{\dayOfWeek}[1]{%
    % give back day of week (here in german)
    % calc #1 modulo 7
    \FPeval{\result}{trunc(#1-(7*trunc(#1/7,0)),0)}
    \ifthenelse{\equal{\result}{0}}{
        Mo
    }{\ifthenelse{\equal{\result}{1}}{
        Di
    }{\ifthenelse{\equal{\result}{2}}{
        Mi
    }{\ifthenelse{\equal{\result}{3}}{
        Do
    }{\ifthenelse{\equal{\result}{4}}{
        Fr
    }{\ifthenelse{\equal{\result}{5}}{
        Sa
    }{\ifthenelse{\equal{\result}{6}}{
        So
    }{}}}}}}}
}

% Parameters %%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcommand{\dateFrom}{2018-06-01}
\newcommand{\dateTo}{2018-12-10}
\newcommand{\xDist}{3.5ex}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{document}
\centering
\begin{tikzpicture}[%
    every day/.style={anchor=mid,opacity=1,minimum width=2.5ex},%
    every node/.style={inner sep=2pt,rectangle}%
]

    \calendar (cal) [
        dates=\dateFrom to \dateTo,
        month list,%
        month label left,%
        month text=\textcolor{black}{\%mt},%
        month yshift=1.7em,%
        day xshift=\xDist,%
    ]
    if (Sunday) [every day/.append style={font=\slshape},red];

    % Friday is on a fix position (4), because week starts always with monday.
    % Therefore 4.5 for line between friday and saturday.
    \coordinate (up) at (4.5*\xDist,0 |- cal-\dateFrom.north);
    \coordinate (down) at (4.5*\xDist,0 |- cal-\dateTo.south);

    % dashed lines from (up) to (down)
    \foreach \i in {0,...,4}{% <-- the number could change
    \draw[dashed]%
        ([xshift=7*\i*\xDist]up.center)--([xshift=7*\i*\xDist]down.center)
        ([xshift=7*\i*\xDist + 2*\xDist]up.center)--([xshift=7*\i*\xDist + 2*\xDist]down.center);
    }

    % write day of week above:  A month starting with sunday (day 6) and
    % having 31 days would have the maximum x-position 6+31=37.
    % Therefore the range goes from 0 to 36.
    \foreach \i in {0,...,36}{
        \node[] at (\i*\xDist,0 |- cal-\dateFrom.north) [above=2mm]{\dayOfWeek{\i}};
    }
\end{tikzpicture}

\end{document}

结果:

在此处输入图片描述

相关内容