如何使用宏将日期传递给 tikz 日历

如何使用宏将日期传递给 tikz 日历

我想使用个人命令将一些星期名称传递给日历,以便用户可以轻松更改星期几并创建个人日历。

我用一个参数定义了命令\mydays,该参数被传递给列表\mydayslist

如何在日历中使用此列表?

平均能量损失

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

\newcommand*{\mydays}[1]{%
 \def\mydayslist{#1}%
}
\mydays{Monday,Friday}

\begin{document}

I want to pass the days \mydayslist\ to the calendar.

\noindent\centering
\begin{tikzpicture}
[every day/.style={anchor=mid},every node/.style={inner sep=2pt,rectangle,thick}]
\calendar (cal) [dates=2014-02-01 to 2014-07-last, month list, month label left,%
month text=\textcolor{black}{\%mt}, month yshift=1.7em, black!20]
%
if (Tuesday) [black] 
% if (\mydayslist) [black] %% <-- insert \mydayslist
;
\end{tikzpicture}

\end{document}

答案1

您可以使用if按键来定义要测试的样式天(myif在我的例子中)。然后,您可以使用.expand once处理程序(或.expanded处理程序)来扩展宏,并.list使用处理程序来应用列表。

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

\newcommand*{\mydays}[1]{%
 \def\mydayslist{#1}%
}
\mydays{Monday,Friday}

\begin{document}

I want to pass the days \mydayslist\ to the calendar.

\noindent\centering
\begin{tikzpicture}
[every day/.style={anchor=mid},every node/.style={inner sep=2pt,rectangle,thick}]

\calendar (cal) [dates=2014-02-01 to 2014-07-last,month list,month label left,
month text=\textcolor{black}{\%mt},month yshift=1.7em, black!20,
myif/.style={if=(#1)[black]},
myif/.list/.expand once={\mydayslist},
];
\end{tikzpicture}

\end{document}

第一次编辑

and带有条件的示例TikZ 日历和条件测试

\documentclass{report}
\usepackage[margin=1cm,landscape]{geometry}
\usepackage{tikz}
\usetikzlibrary{calendar}
% code from https://tex.stackexchange.com/a/141027/14500
\makeatletter
\def\pgfcalendar@matchesfalse{\global\let\ifpgfcalendar@matches\iffalse}
\def\pgfcalendar@matchestrue{\global\let\ifpgfcalendar@matches\iftrue}
\pgfcalendar@matchesfalse
\pgfqkeys{/pgf/calendar}{and/.code 2 args={%
    \begingroup
      \ifdate{#1}{\ifdate{#2}{\pgfcalendar@matchestrue}{}}{}%
    \endgroup
    \ifpgfcalendar@matches\pgfcalendarmatchestrue\pgfcalendar@matchesfalse\fi}}
\makeatother
%------
\newcommand*{\mydays}[1]{%
 \def\mydayslist{#1}%
}
\mydays{Monday,Friday}

\begin{document}

I want to pass the days \mydayslist\ to the calendar.

\noindent\centering
\begin{tikzpicture}
[every day/.style={anchor=mid},every node/.style={inner sep=2pt,rectangle,thick}]

\calendar (cal) [
dates=2014-02-01 to 2014-07-last,
month list,month label left,
month text=\textcolor{black}{\%mt},
month yshift=1.7em, black!20,
myif2/.style={if={(and={between=2014-04-01 and 2014-05-32}{#1})[red]}},
myif2/.list/.expanded={\mydayslist},
];
\end{tikzpicture}

\end{document}

第二次编辑

这是溶液混合Guho 的回答和之前的解决方案一样。我定义了一个新键day来测试日期列表:

\pgfkeys{
  /pgf/calendar/@day/.style={#1},
  /pgf/calendar/day/.style={@day/.list/.expanded=#1},
}

使用这些键,您可以始终使用规范语法(pgfmanual 的第 524 页)。

and带条件的示例TikZ 日历和条件测试

在此处输入图片描述

\documentclass{report}
\usepackage[margin=1cm,landscape]{geometry}
\usepackage{tikz}
\usetikzlibrary{calendar}
% code from https://tex.stackexchange.com/a/141027/14500
\makeatletter
\def\pgfcalendar@matchesfalse{\global\let\ifpgfcalendar@matches\iffalse}
\def\pgfcalendar@matchestrue{\global\let\ifpgfcalendar@matches\iftrue}
\pgfcalendar@matchesfalse
\pgfqkeys{/pgf/calendar}{and/.code 2 args={%
    \begingroup
      \ifdate{#1}{\ifdate{#2}{\pgfcalendar@matchestrue}{}}{}%
    \endgroup
    \ifpgfcalendar@matches\pgfcalendarmatchestrue\pgfcalendar@matchesfalse\fi}}
\makeatother
%------
\pgfkeys{
  /pgf/calendar/@day/.style={#1},
  /pgf/calendar/day/.style={@day/.list/.expanded=#1},
}
\newcommand*{\mydays}[1]{%
 \def\mydayslist{#1}%
}
\mydays{Monday,Friday}

\begin{document}

I want to pass the days \mydayslist\ to the calendar.

\noindent\centering
\begin{tikzpicture}
[every day/.style={anchor=mid},every node/.style={inner sep=2pt,rectangle,thick}]

\calendar (cal) [
dates=2014-02-01 to 2014-07-last,
month list,month label left,
month text=\textcolor{black}{\%mt},
month yshift=1.7em, black!20,
]
if (day=Sunday) [black,font=\itshape]
if (day=\mydayslist) [green]
if (and={between=2014-04-01 and 2014-05-32}{day=\mydayslist}) [red]
;
\end{tikzpicture}

\end{document}

第三次编辑

我的第三个解决方案定义了宏(以或但带前缀的\calkeys方式管理键值对)。使用此宏,您可以将任何日历选择器定义为命名样式:\pgfkeys\tikzset/pgf/calendar/

\def\calkeys#1{\pgfkeys{/pgf/calendar/.cd,#1}}
\calkeys{
  my day list/.style={Friday,Saturday},
  april/.style={between=2014-04-01 and 2014-04-30},
  my day list in april/.style={and={april}{my day list}},
}

例子:

\documentclass[margin=3mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{calendar}
% code from https://tex.stackexchange.com/a/141027/14500
\makeatletter
\def\pgfcalendar@matchesfalse{\global\let\ifpgfcalendar@matches\iffalse}
\def\pgfcalendar@matchestrue{\global\let\ifpgfcalendar@matches\iftrue}
\pgfcalendar@matchesfalse
\pgfqkeys{/pgf/calendar}{and/.code 2 args={%
    \begingroup
      \ifdate{#1}{\ifdate{#2}{\pgfcalendar@matchestrue}{}}{}%
    \endgroup
    \ifpgfcalendar@matches\pgfcalendarmatchestrue\pgfcalendar@matchesfalse\fi}}
\makeatother
% -----
\def\calkeys#1{\pgfkeys{/pgf/calendar/.cd,#1}}
\calkeys{
  my day list/.style={Friday,Saturday},
  april 2014/.style={between=2014-04-01 and 2014-04-30},
  my day list in april 2014/.style={and={april 2014}{my day list}},
}

\begin{document}
\begin{tikzpicture}
[every day/.style={anchor=mid},every node/.style={inner sep=2pt,rectangle,thick}]
\calendar (cal) [dates=2014-02-01 to 2014-07-last,month list,month label left,
month text=\textcolor{black}{\%mt},month yshift=1.7em, black!20]
if (my day list) [red]
if (my day list in april 2014) [green]
;
\end{tikzpicture}
\end{document}

答案2

这是一个带有代码包装器命令的版本tikz,它确实预先扩展了参数(此处 \mydayslist)——键值接口的常见问题是缺少键值的扩展,这里就是这种情况!

\documentclass{report}
\usepackage[margin=1cm,landscape]{geometry}
\usepackage{tikz}

\usetikzlibrary{calendar}


\def\mydayslist{}
\newcommand*{\mydays}[1]{%
 \def\mydayslist{#1}%
}

\mydays{Monday,Tuesday,Friday}

\newcommand{\sigurslittlehelper}[1]{%
  if (#1) [black]
  ;
}


\newcommand{\tikzdo}[1]{% Expand the list first, then use the tikz code
  \expandafter\sigurslittlehelper\expandafter{#1}%
}



\begin{document}

I want to pass the days \mydayslist\ to the calendar.

\noindent\centering
\begin{tikzpicture}
[every day/.style={anchor=mid},every node/.style={inner sep=2pt,rectangle,thick}]
\calendar (cal) [dates=2014-02-01 to 2014-07-last, month list, month label left,%
month text=\textcolor{black}{\%mt}, month yshift=1.7em, black!20]
\tikzdo{\mydayslist}
\end{tikzpicture}

\end{document}

在此处输入图片描述

答案3

这似乎有效。它定义了一个键,用于在将控制序列传递给内部之前对其进行扩展\ifdate。派生自这个答案

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

\newcommand*{\mydays}[1]{%
 \def\mydayslist{#1}%
}
\mydays{Monday,Friday}
\pgfqkeys{/pgf/calendar}{fromcs/.code={\expandafter\ifdate\expandafter{#1}{}{}}}%<=====added
\begin{document}

I want to pass the days \mydayslist\ to the calendar.

\noindent\centering
\begin{tikzpicture}
[every day/.style={anchor=mid},every node/.style={inner sep=2pt,rectangle,thick}]
\calendar (cal) [dates=2014-02-01 to 2014-07-last, month list, month label left,%
month text=\textcolor{black}{\%mt}, month yshift=1.7em, black!20]
%
if (Tuesday) [black] 
if (fromcs={\mydayslist}) [black] %% <-- insert \mydayslist
;
\end{tikzpicture}

\end{document}

产生的日历

相关内容