![Tikz 日历-如何使用'\def'传递参数](https://linux22.com/image/466865/Tikz%20%E6%97%A5%E5%8E%86-%E5%A6%82%E4%BD%95%E4%BD%BF%E7%94%A8'%5Cdef'%E4%BC%A0%E9%80%92%E5%8F%82%E6%95%B0.png)
给出以下代码
\documentclass[a4paper,20pt]{extarticle}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[czech]{babel}
\usepackage[czech]{translator}
\usepackage{graphicx, comment, tikz}
\usetikzlibrary{calendar, shapes.misc}
\usepackage{pgfkeys,pgfcalendar}
\usepackage{geometry}
\geometry{
left=9mm,
top=13mm,
bottom=9mm,
right=10mm
}
\newcounter{tgmymo}
\setcounter{tgmymo}{0}
\newcommand{\mymo}[4]{%
\begin{minipage}{\textwidth}%
\centering%
\stepcounter{tgmymo}
\includegraphics[width=#4\textwidth]{#3}
\end{minipage}
\vfill
\begin{center}
{\textsc{\pgfcalendarmonthname{\value{tgmymo}}} 2024}
\end{center}
\vfill
\noindent \begin{minipage}[t][10cm][t]{.99\textwidth}\centering
\begin{tikzpicture}[thick]
\pgfcalendar{cal}{#1}{#2}{%
\ifdate{workday}%
{\tikzset{filling/.style={fill=black!05}}}%
{\tikzset{filling/.style={fill=orange!20}}}
%%
\def\normal{\node (\pgfcalendarsuggestedname) at (\pgfcalendarcurrentweekday*2.52,0) [filling, rectangle,rounded corners, text width=.105\textwidth,text height=9.796mm ,align=right] {\\ \pgfcalendarcurrentday};}%
%%
\def\bday#1{\node (\pgfcalendarsuggestedname) at (\pgfcalendarcurrentweekday*2.52,0) [filling, rectangle, rounded corners, text width=.105\textwidth,text height=9.796mm, align=right] {\\ \textcolor{red}{#1} \textcolor{red}{\pgfcalendarcurrentday}};}%
%% birthdays will come here
\ifdate{equals=09-02}{\bday(mum)}{\normal}%
%---end of birthdays
\ifdate{Sunday}{\pgftransformyshift{-2.35cm}}{}%
};
\end{tikzpicture}\end{minipage}
\clearpage}
\begin{document}
\setlength\parindent{0pt}
\pagestyle{empty}
\mymo{2024-01-01}{2024-01-last}{example-image}{.55}
\mymo{2024-02-01}{2024-02-last}{example-image}{.9}
\mymo{2024-03-01}{2024-03-last}{example-image}{.55}
\mymo{2024-04-01}{2024-04-last}{example-image}{.55}
\mymo{2024-05-01}{2024-05-last}{example-image}{.9}
\mymo{2024-06-01}{2024-06-last}{example-image}{.9}
\mymo{2024-07-01}{2024-07-last}{example-image}{.55}
\mymo{2024-08-01}{2024-08-last}{example-image}{.6}
\mymo{2024-09-01}{2024-09-last}{example-image}{.485}
\mymo{2024-10-01}{2024-10-last}{example-image}{.55}
\mymo{2024-11-01}{2024-11-last}{example-image}{.55}
\mymo{2024-12-01}{2024-12-last}{example-image}{.475}
\end{document}
我希望\bday
命令能够发挥作用。我想传递一个参数 - 生日那天的人的名字,但我不知道正确的语法。谢谢大家♥
答案1
让我们来看看...
- 用于
\newcommand
你的\normal
和\bday
(它是 LaTeX;如果可以的话,尽量避免使用内部 TeX 的东西)。 - 当您调用时
\bday
使用{}
来界定参数(不是()
)。 - 在顶层定义宏;可以在宏内定义宏,但在这里很棘手且没有必要。
- 我调整了宏
\bday
以产生两行输出思考您正在寻找的;不确定我是否理解正确。
\documentclass[a4paper,20pt]{extarticle}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[czech]{babel}
\usepackage[czech]{translator}
\usepackage{graphicx, comment, tikz}
\usetikzlibrary{calendar, shapes.misc}
\usepackage{pgfkeys,pgfcalendar}
\usepackage{geometry}
\geometry{ left=9mm, top=13mm, bottom=9mm, right=10mm }
\newcounter{tgmymo}
\setcounter{tgmymo}{0}
\newcommand\normal{%
\node (\pgfcalendarsuggestedname) at (\pgfcalendarcurrentweekday*2.52,0) [filling, rectangle,rounded corners, text width=.105\textwidth,text height=9.796mm ,align=right] {\\ \pgfcalendarcurrentday};
}%
%%
\newcommand\bday[1]{%
\node (\pgfcalendarsuggestedname) at (\pgfcalendarcurrentweekday*2.52,0) [filling, rectangle, rounded corners, text width=.105\textwidth,text height=9.796mm, align=right] { \textcolor{red}{#1}\\ \textcolor{red}{\pgfcalendarcurrentday}};
}%
\newcommand{\mymo}[4]{%
\begin{minipage}{\textwidth}%
\centering%
\stepcounter{tgmymo}
\includegraphics[width=#4\textwidth]{#3}
\end{minipage}
\vfill
\begin{center}
{\textsc{\pgfcalendarmonthname{\value{tgmymo}}} 2024}
\end{center}
\vfill
\noindent
\begin{minipage}[t][10cm][t]{.99\textwidth}
\centering
\begin{tikzpicture}[thick]
\pgfcalendar{cal}{#1}{#2}{%
\ifdate{workday}%
{\tikzset{filling/.style={fill=black!05}}}%
{\tikzset{filling/.style={fill=orange!20}}}
%%
%% birthdays will come here
\ifdate{equals=09-02}{\bday{mum}}{\normal}%
%---end of birthdays
\ifdate{Sunday}{\pgftransformyshift{-2.35cm}}{}%
};
\end{tikzpicture}
\end{minipage}
\clearpage
}
\begin{document}
\setlength\parindent{0pt}
\pagestyle{empty}
\mymo{2024-01-01}{2024-01-last}{example-image}{.55}
\mymo{2024-02-01}{2024-02-last}{example-image}{.9}
\mymo{2024-03-01}{2024-03-last}{example-image}{.55}
\mymo{2024-04-01}{2024-04-last}{example-image}{.55}
\mymo{2024-05-01}{2024-05-last}{example-image}{.9}
\mymo{2024-06-01}{2024-06-last}{example-image}{.9}
\mymo{2024-07-01}{2024-07-last}{example-image}{.55}
\mymo{2024-08-01}{2024-08-last}{example-image}{.6}
\mymo{2024-09-01}{2024-09-last}{example-image}{.485}
\mymo{2024-10-01}{2024-10-last}{example-image}{.55}
\mymo{2024-11-01}{2024-11-last}{example-image}{.55}
\mymo{2024-12-01}{2024-12-last}{example-image}{.475}
\end{document}
答案2
改编
- 定义命令
\normal
并\bday
在外部\mymo
使用\newcommand
- 此处使用花括号
\bday{mum}
\bday
在名称和日期之间添加换行符- 我将参数 1 和 2 合并
\mymo
为yyyy-mm
代码
\documentclass[a4paper,20pt]{extarticle}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[czech]{babel}
\usepackage[czech]{translator}
\usepackage{graphicx, comment, tikz}
\usetikzlibrary{calendar, shapes.misc}
\usepackage{pgfkeys,pgfcalendar}
\usepackage{geometry}
\geometry{
left=9mm,
top=13mm,
bottom=9mm,
right=10mm
}
\newcounter{tgmymo}
\setcounter{tgmymo}{0}
\newcommand{\normal}{%
\node (\pgfcalendarsuggestedname) at (\pgfcalendarcurrentweekday*2.52,0) [filling, rectangle,rounded corners, text width=.105\textwidth,text height=9.796mm ,align=right] {\\ \pgfcalendarcurrentday};
}
\newcommand{\bday}[1]{%
\node (\pgfcalendarsuggestedname) at (\pgfcalendarcurrentweekday*2.52,0) [filling, rectangle, rounded corners, text width=.105\textwidth,text height=9.796mm, align=right] {\textcolor{red}{#1}\\ \textcolor{red}{\pgfcalendarcurrentday}};
}
\newcommand{\mymo}[3]{%
\begin{minipage}{\textwidth}%
\centering%
\stepcounter{tgmymo}
\includegraphics[width=#3\textwidth]{#2}
\end{minipage}
\vfill
\begin{center}
{\textsc{\pgfcalendarmonthname{\value{tgmymo}}} 2024}
\end{center}
\vfill
\noindent
\begin{minipage}[t][10cm][t]{.99\textwidth}\centering
\begin{tikzpicture}[thick]
\pgfcalendar{cal}{#1-01}{#1-last}{%
\ifdate{workday}%
{\tikzset{filling/.style={fill=black!05}}}%
{\tikzset{filling/.style={fill=orange!20}}}
%% birthdays will come here
\ifdate{equals=09-02}{\bday{mum}}{\normal}%
%---end of birthdays
\ifdate{Sunday}{\pgftransformyshift{-2.35cm}}{}%
};
\end{tikzpicture}
\end{minipage}
\clearpage
}
\begin{document}
\setlength\parindent{0pt}
\pagestyle{empty}
\mymo{2024-01}{example-image}{.55}
\mymo{2024-02}{example-image}{.9}
\mymo{2024-03}{example-image}{.55}
\mymo{2024-04}{example-image}{.55}
\mymo{2024-05}{example-image}{.9}
\mymo{2024-06}{example-image}{.9}
\mymo{2024-07}{example-image}{.55}
\mymo{2024-08}{example-image}{.6}
\mymo{2024-09}{example-image}{.485}
\mymo{2024-10}{example-image}{.55}
\mymo{2024-11}{example-image}{.55}
\mymo{2024-12}{example-image}{.475}
\end{document}