我正在根据我的另一个问题使用宏自动生成一个表格(使用电子表格计算时间)。将会有三种(也许是四种,也许n
——不知道这个问题的答案是否可以轻松实现通用)元类型的条目(例如,假设它们是,,A
):B
C
因此我的表格看起来是这样的:
所以我想根据持续时间生成一个饼图,例如上面的例子:
答:00:30
乙:00:30
拍摄地:00:10
因此我需要根据为特定行指定的元标记添加持续时间。这不是问题的一部分,但是为了全面了解我的问题:我的最终目标是将这些值传递给此处描述的内容:(饼图包,例如这个回答)。
那么我该如何进行基于元标记的添加(和除以总和)并存储值呢?MWE:
\documentclass{article}
%% time calc
\def\InitialStartTime{09:00}
\newcount\hours
\newcount\minutes
\def\gettime#1:#2\relax#3#4{\def#3{#1}\def#4{#2}}
\newcommand{\add}[2]{%
\expandafter\gettime#1\relax{\hrs}{\mins}
\expandafter\gettime#2\relax{\addhrs}{\addmins}%
\hours=\hrs\relax
\advance\hours by \addhrs\relax
\minutes=\mins\relax
\advance\minutes by \addmins\relax
\ifnum\minutes>59\relax
\advance\minutes by -60\relax
\advance\hours by 1\relax
\else
\ifnum\minutes<0\relax
\advance\minutes by 60\relax
\advance\hours by -1\relax
\fi
\fi
\ifnum\hours>23\relax
\advance\hours by -24\relax
\else
\ifnum\hours<0\relax
\advance\hours by 24\relax
\fi
\fi
\ifnum\minutes<10\relax
\ifnum\hours<10\relax
\xdef#1{0\number\hours:0\number\minutes}%
\else
\xdef#1{\number\hours:0\number\minutes}%
\fi
\else
\ifnum\hours<10\relax
\xdef#1{0\number\hours:\number\minutes}%
\else
\xdef#1{\number\hours:\number\minutes}%
\fi
\fi
}
\newcommand\newStartTime[1]{#1 h~~ & \startTime{} & \add\startTime{#1}\startTime\\}
\begin{document}
\xdef\startTime{\InitialStartTime}
\begin{tabular}{lllll}
a & duration & starttime & endtime\\
\hline
One & \newStartTime{00:20} %adding the meta-data of this row?
Two & \newStartTime{00:30} %adding the meta-data of this row?
Three & \newStartTime{00:10} %adding the meta-data of this row?
Four & \newStartTime{00:10} %adding the meta-data of this row?
\end{tabular}
%here comes the wished output
A: %\timeOfMeta{A}
B: %\timeOfMeta{B}
C: %\timeOfMeta{C}
%
\end{document}
答案1
对于这个答案,我修改了你的\add
宏以便它接受作为输入的不是宏也不是明确的时间,而是存储明确时间的宏的名称(实际上真正的名称使用来@
隔离代码以免与其他包冲突。)
此外,我决定行构造函数将接收持续时间作为数据,以便每行都必须按照正确的顺序给出,否则开始时间和结束时间将全部被修改(但每个元标记的累积时间是可以的)。
最后,为了更容易地检查它是否正常工作,我让宏明确显示元标记作为每行的最后一个单元格。
\documentclass{article}
%% time calc
\def\InitialStartTime{09:00}
\newcount\hours
\newcount\minutes
\def\gettime#1:#2\relax#3#4{\def#3{#1}\def#4{#2}}
\def\nameuse #1{\csname user@#1\endcsname}
\def\namexdef #1{\expandafter\xdef\csname user@#1\endcsname}
\newcommand{\addtimeregisters}[2]{%
\expandafter\gettime \romannumeral-`0\nameuse{#1}\relax{\hrs}{\mins}% <- added missing %
\expandafter\gettime \romannumeral-`0\nameuse{#2}\relax{\addhrs}{\addmins}%
\hours=\hrs\relax
\advance\hours by \addhrs\relax
\minutes=\mins\relax
\advance\minutes by \addmins\relax
\ifnum\minutes>59\relax
\advance\minutes by -60\relax
\advance\hours by 1\relax
\else
\ifnum\minutes<0\relax
\advance\minutes by 60\relax
\advance\hours by -1\relax
\fi
\fi
\ifnum\hours>23\relax
\advance\hours by -24\relax
\else
\ifnum\hours<0\relax
\advance\hours by 24\relax
\fi
\fi
\ifnum\minutes<10\relax
\ifnum\hours<10\relax
\namexdef{#1}{0\number\hours:0\number\minutes}%
\else
\namexdef{#1}{\number\hours:0\number\minutes}%
\fi
\else
\ifnum\hours<10\relax
\namexdef{#1}{0\number\hours:\number\minutes}%
\else
\namexdef{#1}{\number\hours:\number\minutes}%
\fi
\fi
}
\newcommand\newDuration[2]{%
\namexdef{duration}{#1}%
% check if meta tag already encountered, if not initialize it
\ifcsname user@tag#2\endcsname\else\namexdef{tag#2}{00:00}\fi
% update it
\addtimeregisters{tag#2}{duration}%
#1% do we really want h here ? and ~~ ?
&\nameuse{startTime}\addtimeregisters{startTime}{duration}
&\nameuse{startTime}\\}
\newcommand\timeOfMeta [1]{\nameuse{tag#1}}
\begin{document}
\namexdef{startTime}{\InitialStartTime }
\begin{tabular}{lllll}
a & duration & starttime & endtime & meta\\
\hline
One & \newDuration{00:20}{A}
Two & \newDuration{00:30}{B}
Three & \newDuration{00:10}{C}
Four & \newDuration{00:10}{A}
Five & \newDuration{00:15}{C}
Six & \newDuration{01:05}{B}
Seven & \newDuration{00:25}{C}
Eight & \newDuration{01:35}{A}
\end{tabular}
\bigskip
%here comes the wished output
\begin{tabular}{ll}
Meta&Cumul.\\\hline
A&\timeOfMeta{A}\\
B&\timeOfMeta{B}\\
C&\timeOfMeta{C}
\end{tabular}
\end{document}