# LaTeX set and add durations as in primary school

I'm looking for a package, or even a macro that allows me to simply write duration calculations for young students. Like for example this (Lebossé-Hemery 1957 classe de sixième):

Please note that this question follows this one here.

### Here is a macro given by @DavidCarlisle in the chat here:

https://chat.stackexchange.com/transcript/message/52634394#52634394

\documentclass{article}

\begin{document}

\def\zz#1#2#3#4{%
\begin{tabular}{rrr}
&#1\,h&#2\,min\\
$-$&#3\,h&#4\,min\\[2pt]
\hline
&\the\numexpr#1-#3\ifnum#2<#4 -1\fi\relax\,h&
\the\numexpr#2 \ifnum#2<#4 +60\fi - #4\relax\,min
\end{tabular}}

\zz{7}{50}{4}{15}

\end{document}


### Here's what I get by using TikZ matrices

and the \hms macro defined by @egreg. As you can see, that's a lot of code, but for lack of anything better, I'm happy with it. Using TikZ allows me to place arrows easily.

\documentclass[tikz,border=5mm]{standalone}
\usepackage[T1]{fontenc}
\usepackage[french]{babel}
\usepackage{xparse}
\usepackage{siunitx,cancel}
\usetikzlibrary{matrix,babel}
\sisetup{locale=FR}

\ExplSyntaxOn

\NewDocumentCommand{\hms}{ o m }
{
\ensuremath
{
\IfNoValueTF { #1 }
{
\andrec_hms:Ve \l_andrec_hms_sep_str { #2 } % Used to say \tl_to_str:n { #2 }
}
{
\andrec_hms:ee { \tl_to_str:n { #1 } } { #2 } % Used to say \tl_to_str:n { #2 }
}
}
}
\NewDocumentCommand{\sethmssep}{m}
{
\str_set:Nn \l_andrec_hms_sep_str { #1 }
}

\cs_new_protected:Nn \andrec_hms:nn
{
\group_begin:
\seq_set_split:Nnn \l__andrec_hms_seq { #1 } { #2 }
% normalize the sequence to have three (or more!) items
\int_compare:nT { \seq_count:N \l__andrec_hms_seq = 1 }
{
\seq_put_right:Nn \l__andrec_hms_seq { 0 }
}
\int_compare:nT { \seq_count:N \l__andrec_hms_seq = 2 }
{
\seq_put_right:Nn \l__andrec_hms_seq { 0 }
}
% print
\bool_set_false:N \l__andrec_hms_thinspace_bool
\__andrec_hms_print:nn { 1 } { h }
\__andrec_hms_print:nn { 2 } { min }
\__andrec_hms_print:nn { 3 } { s }
\group_end:
}
\cs_generate_variant:Nn \andrec_hms:nn { Ve,ee }

\cs_new_protected:Nn \__andrec_hms_print:nn
{
\fp_compare:nF { 0\seq_item:Nn \l__andrec_hms_seq { #1 } = 0 }
{
\bool_if:NT \l__andrec_hms_thinspace_bool { \, }
\num{\fp_eval:n { 0\seq_item:Nn \l__andrec_hms_seq { #1 } }} \, \mathrm{#2}
\bool_set_true:N \l__andrec_hms_thinspace_bool
}
}
\ExplSyntaxOff

\sethmssep{:}

\begin{document}

\begin{tikzpicture}[every node/.style={anchor=east}]

\matrix (magic) [matrix of math nodes]
{
& \hms{3} & \hms{:48}&\\
+ &\hms{2} &  \hms{:20}& \\
&\cancel{\hms{5}} & \cancel{\hms{:68}}& \\
&|[blue]|\hms{6} &|[blue]| \hms{:8}& \\
};
\draw[thick,blue,->] (magic-3-2) to[out=180,in=180,looseness=3]node{$+\hms{1}$} (magic-4-2);
\draw[thick,blue,->] (magic-3-3) to[out=0,in=0,looseness=3]node[right]{$-\hms{:60}$} (magic-4-3);
\draw[thick] (magic-2-1.south west) -- (magic-2-3.south east);

\begin{scope}[xshift=7cm]
\matrix (magic) [matrix of math nodes]
{
&|[blue]|\hms{17} &|[blue]| \hms{:63}& \\
& \cancel{18}\,\text{h} & \cancel{3}\,\text{min}&\\
- &\hms{5} &  \hms{:54}& \\
&\hms{12} & \hms{:9}& \\
};
\draw[thick,blue,->] (magic-2-2) to[out=180,in=180,looseness=3]node{$-\hms{1}$} (magic-1-2);
\draw[thick,blue,->] (magic-2-3) to[out=0,in=0,looseness=3]node[right]{$+\hms{:60}$} (magic-1-3);
\draw[thick] (magic-3-1.south west) -- (magic-3-3.south east);
\end{scope}
\end{tikzpicture}

\end{document}

• There are several elements of the image that you showed that seems best adjusted by hand. First, the two tables are aligned at the horizontal lines: is that a necessity or can you live without? Second, in the first table, on the lines above the first horizontal line the + symbol intrudes into space that would normally be saved for the phantom "0 j". This I am guessing is also optional from your point of view. – Willie Wong Nov 18 '19 at 21:13
• What's not optional, I am guessing, seems to be the conversion of "124 m" to "2 h 4 m" which makes it more difficult to do something like what you have in your linked question, since you cannot just define a \hms command to convert three semicolon-separated numbers into an entire row of the table. It may be helpful if you can clarify which pieces of the output are necessary, and what the expected input is. // Also, Would you be happy with an answer using something other than LaTeX to generate the code for setting the table (for example, using Python)? – Willie Wong Nov 18 '19 at 21:20
• Indeed, it is a book that dates back to 1957, LaTeX did not yet exist and therefore the alignments were done by typographer workers. You are absolutely right, the alignment of horizontal lines is not, in my opinion, a necessity. As well as the positioning of the + signs is from my point of view just as optional. – AndréC Nov 18 '19 at 21:22
• What seems important to me in the first place is to be able to easily set up the operations by superimposing the durations. Then, either to have a detailed automatic calculation or to be able to convert these durations manually myself in the following line while respecting the alignment of units. I don't care if you use Python, the main thing is that it is somehow compiled with a LaTeX engine, pdflatex, etc. Translated with www.DeepL.com/Translator – AndréC Nov 18 '19 at 21:29
• I just added an example of a result I get using TikZ matrices. – AndréC Nov 20 '19 at 18:42