带有 TIKZ-PGF 的日历:如何突出显示当前日期?

带有 TIKZ-PGF 的日历:如何突出显示当前日期?

我正在尝试生成一个包含当前日期和当前月份小日历的 365 天笔记本。我想在小日历中突出显示当前日期,例如在其周围添加一个圆圈或将其设为粗体。例如,如果左侧列出的日期是 1 月 10 日,我希望在右侧的日历上有一个标记,突出显示 1 月 10 日的日期。在花了几个小时阅读 pgf/tikz 手册和本论坛上与日历相关的主题后,我没有找到任何解决方案。

下面是我的代码:

\documentclass[b5paper,  fontsize=12pt, parskip=half, DIV=12,BCOR=2cm]{scrartcl} 
%pagesize,
\usepackage[T1]{fontenc}
\usepackage[romanian]{babel}
\usepackage{translator, tikz, pgfcalendar, array, libertine}
\usepackage{ucs}
\usepackage[utf8x]{inputenc}
\usepackage{xcolor}
\usepackage{tabu}
\usepackage{epstopdf}
\usepackage{color}
\usepackage{pgf}

%=========================================================
%== Page characteristics (Page Setup) ====================
\setlength{\textwidth}{126mm}
\setlength{\textheight}{230.0mm}
\setlength{\topmargin}{-30mm}
\setlength{\hoffset}{-20mm}
\setlength{\marginparwidth}{-20mm}
\setlength{\footskip}{12mm}
\setlength{\headheight}{10.5mm}
\setlength{\headsep}{4mm}
\setlength{\parindent}{0cm}

\usetikzlibrary{calendar,shapes.geometric}
%== Define colors =================================
\definecolor{rozmam}{RGB}{242,172,184}
\newcommand{\troz}{\textcolor{rozmam}}


\makeatletter%
\tikzoption{day headings}{\tikzstyle{day heading}=[#1]}
\tikzstyle{day heading}=[]
\tikzstyle{day letter headings}=[
    execute before day scope={ \ifdate{day of month=1}{%
      \pgfmathsetlength{\pgf@ya}{\tikz@lib@cal@yshift}%
      \pgfmathsetlength\pgf@xa{\tikz@lib@cal@xshift}%
      \pgftransformyshift{-\pgf@ya}
      \foreach \d/\l in {0/M,1/T,2/W,3/T,4/F,5/S,6/S} {
        \pgf@xa=\d\pgf@xa%
        \pgftransformxshift{\pgf@xa}%
        \pgftransformyshift{\pgf@ya}%
        \node[every day,day heading]{\l};%
      }
    }{}%
  }%
]
\makeatother

\newcommand{\PaginaMea}{
\minisec{\textcolor{gray}{Notes}}
\begin{tabu}{@{}p{0.12\textwidth}p{0.32\textwidth}p{0.32\textwidth}p{0.20\textwidth}c}\taburulecolor{gray}\hline
$\diamond$ \textbf{ } &  &   &  & \\\hline
$\diamond$ & & & & \\\hline
$\diamond$ & & & & \\\hline
$\diamond$ & & & & \\\hline
$\diamond$ & & & & \\\hline
$\diamond$ & & & & \\\hline
$\diamond$ & & & & \\\hline
$\diamond$ & & & & \\\hline
$\diamond$ & & & & \\\hline
$\diamond$ & & & & \\\hline
$\diamond$ & & & & \\\hline
$\diamond$ & & & & \\\hline
$\diamond$ & & & & \\\hline
$\diamond$ & & & & \\\hline
$\diamond$ & & & & \\\hline
$\diamond$ & & & & \\\hline
$\diamond$ & & & & \\\hline
$\diamond$ & & & & \\\hline
$\diamond$ & & & & \\\hline
$\diamond$ & & & & \\\hline
$\diamond$ & & & & \\\hline
$\diamond$ & & & & \\\hline
$\diamond$ & & & & \\\hline
$\diamond$ & & & & \\\hline
$\diamond$ & & & & \\\hline
$\diamond$ & & & & \\\hline
$\diamond$ & & & & \\\hline
$\diamond$ & & & & \\\hline

\end{tabu}
}


\begin{document}
\foreach \n in {1,...,1}{

\pgfcalendar{cal}{2018-\n-01}{2018-\n-last}
{
\thispagestyle{empty}
\noindent{\Huge \bfseries
\pgfcalendarcurrentday{}\\[20pt]
\Huge \pgfcalendarweekdayname{\pgfcalendarcurrentweekday}
}

\begin{tikzpicture}[remember picture, overlay, transform shape]
  \node [anchor=north east, inner sep=8pt, xshift = 30pt]
    at (current page.north east)
    {
      \begin{tikzpicture}[every node/.append style = {anchor=center}]
        \calendar[dates=2018-\n-01 to 2018-\n-last,
        week list,
        day text=\%d0,
        month label above centered,
        month text={\%mt} \%y-,
        day xshift = 0.8cm,
        day headings=gray,
        day letter headings
    ]
    if (Saturday) [rozmam]
    if (Sunday)   [rozmam];
      \end{tikzpicture}
    };
\end{tikzpicture}
\\[30pt]
\PaginaMea
\pagebreak
{}
 }
}
\end{document}

enter image description here

答案1

有两种方法可以标记当前日期。对于这两种方法,都必须知道外日历的日期和月份。这可以通过以下方法实现

\edef\pagedayofmonth{\pgfcalendarcurrentday}%
\edef\pagemonth{\pgfcalendarcurrentmonth}%

在外层日历的定义中。这\edef是必要的,因为在小日历中,这两个\pgfcalendarcurrent...宏将为后者提供值。

然后if (day of month=\pagedayofmonth) [cyan]可以添加到小日历来改变数字的颜色。nodes={draw}可以添加,但这会画一个矩形,太大了。

或者,可以使用为每一天定义的节点来绘制数字的周围或上方。这是在小日历之后完成的。要使其工作,必须命名日历。然后,将以 的形式命名这些天的节点calendarname-year-month-day,例如mycal-2018-02-07

\draw[green,thick,rounded corners]
        ($(mycal-2018-\pagemonth-\pagedayofmonth.south east) + (-1.5mm,1.5mm)$) rectangle 
        ($(mycal-2018-\pagemonth-\pagedayofmonth.north west) + (1.5mm,-1.5mm)$);

进行计算是为了获得合理的矩形尺寸,因为仅使用锚点会导致矩形太大。

结果(显示两种方法):

enter image description here

代码如下:

这里我也把outdated改成了\tikzstyle\tikzset见代码里的注释。

\documentclass[b5paper,  fontsize=12pt, parskip=half, DIV=12,BCOR=2cm]{scrartcl} 
%pagesize,
\usepackage[T1]{fontenc}
\usepackage[romanian]{babel}
%\usepackage{libertine} % fonts not installed here
\usepackage{translator, tikz, array} % pgfcalendar loaded later with \usetikzlibrary{calendar}
\usepackage{ucs}
\usepackage[utf8x]{inputenc}
%\usepackage{xcolor} % loaded by tikz
\usepackage{tabu}
\usepackage{epstopdf}
%\usepackage{color} % unnecessary, because xcolor already loaded
%\usepackage{pgf} % loaded by tikz

%=========================================================
%== Page characteristics (Page Setup) ====================
% using geometry would be better
\setlength{\textwidth}{126mm}
\setlength{\textheight}{230.0mm}
\setlength{\topmargin}{-30mm}
\setlength{\hoffset}{-20mm}
\setlength{\marginparwidth}{-20mm}
\setlength{\footskip}{12mm}
\setlength{\headheight}{10.5mm}
\setlength{\headsep}{4mm}
\setlength{\parindent}{0cm}

\usetikzlibrary{calendar,shapes.geometric}
\usetikzlibrary{calc}
%== Define colors =================================
\definecolor{rozmam}{RGB}{242,172,184}
\newcommand{\troz}{\textcolor{rozmam}}


\makeatletter%
% these days, \tikzset is used
\tikzset{%
    day headings/.style={day heading=#1}, % \tikzoption{day headings}{\tikzstyle{day heading}=[#1]}
    day heading/.style={}, % \tikzstyle{day heading}=[]
    day letter headings/.style={% \tikzstyle{day letter headings} ...
        execute before day scope={ \ifdate{day of month=1}{%
          \pgfmathsetlength{\pgf@ya}{\tikz@lib@cal@yshift}%
          \pgfmathsetlength\pgf@xa{\tikz@lib@cal@xshift}%
          \pgftransformyshift{-\pgf@ya}
          \foreach \d/\l in {0/M,1/T,2/W,3/T,4/F,5/S,6/S} {
            \pgf@xa=\d\pgf@xa%
            \pgftransformxshift{\pgf@xa}%
            \pgftransformyshift{\pgf@ya}%
            \node[every day,day heading]{\l};%
          }
        }{}%
      }
    }
}
\makeatother

\newcommand{\PaginaMea}{
\minisec{\textcolor{gray}{Notes}}
\begin{tabu}{@{}p{0.12\textwidth}p{0.32\textwidth}p{0.32\textwidth}p{0.20\textwidth}c}\taburulecolor{gray}\hline
$\diamond$ \textbf{ } &  &   &  & \\\hline
$\diamond$ & & & & \\\hline
$\diamond$ & & & & \\\hline
$\diamond$ & & & & \\\hline
$\diamond$ & & & & \\\hline
$\diamond$ & & & & \\\hline
$\diamond$ & & & & \\\hline
$\diamond$ & & & & \\\hline
$\diamond$ & & & & \\\hline
$\diamond$ & & & & \\\hline
$\diamond$ & & & & \\\hline
$\diamond$ & & & & \\\hline
$\diamond$ & & & & \\\hline
$\diamond$ & & & & \\\hline
$\diamond$ & & & & \\\hline
$\diamond$ & & & & \\\hline
$\diamond$ & & & & \\\hline
$\diamond$ & & & & \\\hline
$\diamond$ & & & & \\\hline
$\diamond$ & & & & \\\hline
$\diamond$ & & & & \\\hline
$\diamond$ & & & & \\\hline
$\diamond$ & & & & \\\hline
$\diamond$ & & & & \\\hline
$\diamond$ & & & & \\\hline
$\diamond$ & & & & \\\hline
$\diamond$ & & & & \\\hline
$\diamond$ & & & & \\\hline

\end{tabu}
}


\begin{document}
\foreach \n in {2,...,2}{

\pgfcalendar{cal}{2018-\n-01}{2018-\n-last}
{
% added to ensure every day starts on a new page
\newpage
\thispagestyle{empty}
% remember day and month
\edef\pagedayofmonth{\pgfcalendarcurrentday}%
\edef\pagemonth{\pgfcalendarcurrentmonth}%
\noindent{\Huge \bfseries
\pgfcalendarcurrentday{}\\[20pt]
\Huge \pgfcalendarweekdayname{\pgfcalendarcurrentweekday}
}

\begin{tikzpicture}[remember picture, overlay, transform shape]
  \node [anchor=north east, inner sep=8pt, xshift = 30pt]
    at (current page.north east)
    {
      \begin{tikzpicture}[every node/.append style = {anchor=center}]
        \calendar (mycal) [dates=2018-\n-01 to 2018-\n-last,
        week list,
        day text=\%d0,
        month label above centered,
        month text={\%mt} \%y-,
        day xshift = 0.8cm,
        day headings=gray,
        day letter headings
    ]
    if (Saturday) [rozmam]
    if (Sunday)   [rozmam]
    % just set another text color; nodes={draw} could be added, but the rectangle is too large
    if (day of month=\pagedayofmonth) [cyan]
    ;
    % draw a frame around the day
    \draw[green,thick,rounded corners]
        ($(mycal-2018-\pagemonth-\pagedayofmonth.south east) + (-1.5mm,1.5mm)$) rectangle 
        ($(mycal-2018-\pagemonth-\pagedayofmonth.north west) + (1.5mm,-1.5mm)$);
      \end{tikzpicture}
    };
\end{tikzpicture}
\\[30pt]
\PaginaMea
\pagebreak
{}
 }
}
\end{document}

相关内容