如何使用巨集將天數傳遞給 tikz 日曆

如何使用巨集將天數傳遞給 tikz 日曆

我想使用個人命令將某一天的名稱傳遞給日曆,以便用戶可以輕鬆更改日期並建立個人日曆。

\mydays用一個參數定義了該命令,該參數被傳遞到​​ list \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}

第二次編輯

這是溶液混合古浩的回答和以前的解決方案。我定義了一個新鍵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(將鍵值對管理為\pgfkeysor\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}

結果日曆

相關內容