TikZ 矩阵倾斜

TikZ 矩阵倾斜

我正在尝试为自己创建一个学术日历。我试图用橙色填充秋季学期,但块的边缘歪斜的。请帮我将边缘定位在垂直或水平方向上。如果我有一些多余的部分,如果您能帮助我简化代码,我会很高兴。

\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} 

在此处输入图片描述

答案1

问题是,第一天的节点比最后几天的节点更窄。

这样做的 Zy 方式是要求minimum width节点:

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

当然,这需要反复试验才能得到正确的宽度。没人喜欢这样,对吧?

因此,浏览代码后,tikzlibrarycalendar.code.tex我发现日历打印例程在日期之前和之后有一些挂钩。我们这里想要的是选项day text,它定义了日期的一些格式(月份和年份有等价的格式)。

与该选项(大致)相同的解决方案minimum width是:

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

这会使日期打印在宽度恒定的框中。如果您希望在少于 10 天的日期前面加上0,则可以使用:

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

无论采用哪种解决方案,填充区域都是正确的:

在此处输入图片描述

完整代码:

\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}

正如 Marmot 所注意到的,您正在使用垂直线进行绘制,因此您可以从语法切换---|语法。您还可以定义一些样式以避免重复:

\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}

答案2

这是一种相当笨拙的修复方法。策略是,每当左侧使用 1 位节点时,我都会将其坐标与同一列中 2 位节点的 x 坐标混合。例如(m12-2018-12-03.north west)变成(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}

一个更简单的解决方案源于这样一个事实:轮廓中的其他每个坐标都是不必要的。因此,通过缩短代码,您可以自动解决问题。

\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} 

在此处输入图片描述

相关内容