4

I am generating a table automated with a macro based on my other question (Time calculation with spreadtab). There will be three (maybe four, maybe n - don’t know if the answer to this question can easily be implemented generic) meta-types of entries (for the example let’s say they are A, B, C):

So my table would look like this:


data table

So I want to generate a pie chart based on the duration times, e.g. for the example above:


A: 00:30

B: 00:30

C: 00:10


Therefore I need to add the durations based on a meta-tag given to a specific row. Not part of the question, but to get an overall look at my problem: My final goal is to pass these values to something like described here: (Package for pie charts, e.g. this answer).

So how do I do this meta-tag based adding (and dividing through the sum), and storing the values? 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}
3
  • Are my itensions clear? Should I add some Information?
    – user69453
    Commented Aug 9, 2016 at 17:01
  • 1
    still interested into an answer? possible syntax could be \newtiming{<tag>}{<starttime>}{<endtime>} although we can spare the <starttime> part if all rows are in order. The macro would create the remaining cells in the row (printing or not the tag, depending on what one wants) and accumulate total times for each tag, for later use.
    – user4686
    Commented Sep 10, 2016 at 18:40
  • Of course! Syntax looks good to me :)
    – user69453
    Commented Sep 11, 2016 at 15:49

1 Answer 1

4

enter image description here

For this answer I modified a bit your \add macro in order for it to accept as inputs not macros neither explicit times, but the names of the macros storing the explicit times (actually the real names use the @ to isolate a bit the code from possibly clash with other packages.)

Also, I decided that the row constructor would receive as data the duration, so that each row has to be given in the correct order, else start times and end times would be all modified (but cumulative times per meta tag would be ok).

Finally, for the purpose of checking easier if it works ok I let the macro display explicitely the meta tag as last cell in each row.

\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}&#2\\}

\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}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .