TikZ 日历,类似 Google 日历的多日实线

TikZ 日历,类似 Google 日历的多日实线

我从这些例子开始:

我想在数字下方(可能的话是数字中间)添加一个红色条,表示多日活动。我想知道如何在一周内以及周末(环绕会成为问题)执行此操作。

答案1

这不是答案,而是试图澄清您的问题。我所做的只是从您的第二个链接复制代码,并添加一些我从 pgfmanual 第 526 页改编的基本内容。这允许我绘制一个红色条。

\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=2018-04-01 to 2018-04-last,draw year]; &
            \calendar[dates=2018-05-01 to 2018-05-last]; &
            \calendar[dates=2018-06-01 to 2018-06-last]; \\
            \calendar[dates=2018-07-01 to 2018-07-last]; &
            \calendar[dates=2018-08-01 to 2018-08-last]; &
            \calendar (mycal) [dates=2018-09-01 to 2018-09-last]; \\
            \calendar[dates=2018-10-01 to 2018-10-last]; &
            \calendar[dates=2018-11-01 to 2018-11-last]; &
            \calendar[dates=2018-12-01 to 2018-12-last]; \\
            \calendar[dates=2019-01-01 to 2019-01-last]; &
            \calendar[dates=2019-02-01 to 2019-02-last]; &
            \calendar[dates=2019-03-01 to 2019-03-last]; \\
        };
    \draw[red] (mycal-2018-09-20.south west) -- (mycal-2018-09-22.south east);  
    \draw[red] (mycal-2018-09-23.south west) -- (mycal-2018-09-24.south east);  
    \end{tikzpicture}
\end{document}

在此处输入图片描述

您的问题是如何用一个宏替换两个绘图命令?

相关内容