我正在寻找一个包,甚至是一个宏,它允许我简单地为年轻学生编写持续时间计算。例如这个(Lebossé-Hemery 1957 六年级):
请注意,这个问题紧接着这个问题这里。
以下是@DavidCarlisle 在聊天中提供的宏:
https://chat.stackexchange.com/transcript/message/52634394#52634394
\documentclass{article}
\begin{document}
\def\zz#1#2#3#4{%
\begin{tabular}{rrr}
\,h\,min\\
$-$\,h\,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}
这是我使用 TikZ 矩阵得到的结果
以及\hms
@egreg 定义的宏。如您所见,代码很多,但由于没有更好的方法,我对它很满意。使用 TikZ 可以让我轻松放置箭头。
\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}