1

I come from this question: How can I automatically calculate sums in a LaTeX table?

I want to do the same but with "timing" calculations. So my example would look like this:

\documentclass{article}
\usepackage{spreadtab}
\begin{document}
  \begin{spreadtab}{{tabular}{llll|l}}
  @ a    & @ xyz      & @ duration      & @ starttime & @ endtime \\
  @ a1   & 5          & 00:03           & 00:00       & add(c2, d2) \\
  @ a2   & 9          & 00:01           & (automatically value of previous endtime)       & add(c3, d3) \\
  % and so on
  \end{spreadtab}
\end{document}

I have to do do dozens of These which will change from time to time, and i only want to change the Duration and every other will be calculated.

I would stronly prefere a solution without luaTeX.

3

1 Answer 1

3

Based on Time calculations:

\documentclass{article}

\usepackage{}

\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  & xyz & duration & starttime & endtime\\
        \hline
        a1 & 5   & \newStartTime{00:03}
        a2 & 9   & \newStartTime{00:01}
    \end{tabular}           
\end{document}

enter image description here

2
  • Very nice! Could you expand the example so that there is always the formittag xx:xx. If I use your example i get for the first starttime xx:xx and in the following lines: x:xx - is this fixable?
    – user69453
    Jul 26, 2016 at 18:43
  • 2
    @user69453 yes, it's fixable. See the updated code. Jul 26, 2016 at 19:36

You must log in to answer this question.

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