0

My current code generates a result as shown in the image below.

My problem is: how to cumulate the "Car" durations?.

enter image description here

When the line 95

% \global\let\routeCarTotalDuration\tempd%

is uncommented, I get the exception

TeX capacity exceeded, sorry [expansion depth=10000]. [\end{routeDetail}]

\documentclass{article}    

\usepackage[a4paper,top=2cm,bottom=2cm]{geometry}
\usepackage[T1]{fontenc}%output font encoding that has support for the accented characters
\usepackage[utf8]{inputenc}%allows the user to input accented characters directly from the keyboard
\usepackage{textcomp}%€
\usepackage[greek,francais]{babel}
\usepackage{subfiles}
\usepackage{enumitem,amssymb}
\usepackage{pifont}
\usepackage{wasysym}
\usepackage{marvosym}
\usepackage{multicol}
\usepackage{multirow}
\usepackage{bookmark}%loads hyperref
\usepackage{tabularx}%loads array
\usepackage{color,colortbl}
\usepackage[table]{xcolor}
\usepackage{fp}
\usepackage{ifthen}
\usepackage{fontawesome}
\usepackage[alpine]{ifsym}
\usepackage{scalerel,graphicx}

\newcommand{\routeHome}{\faicon{home}}
\newcommand{\routeCarburant}{\faicon{beer}}
\newcommand{\routeRestaurant}{\faicon{cutlery}}
\newcommand{\routeWC}{\faicon{female}}
\newcommand{\routeHotel}{\faicon{hotel}}

\newenvironment{routeDetail}[1]{%{Date}
    \subsection{#1}
    \vspace{0.5cm}
    \begin{tabular}{rlrcl}
}
{%
    \\
    \hline%Summary
    \multicolumn{2}{l}{Journée}% 1) Day
    & \timeDif{\routeDayStart}{\routeDayStop}%Duration
    & {\scalerel*{\includegraphics{Technical/day}}{\strut}}%Icon
    & \\[2 pt]%Text
    \multicolumn{2}{l}{Trajet}% 2) Car
    & \routeCarTotalDuration%Duration
    & \faicon{car}%Icon
    & \routeCarTotalKm km\\[2 pt]%Text

    \end{tabular}
}


\newcommand{\routeStart}[3]{%{icon}{startTime}{Text}
    \multirow{1}{*}[-1.5ex]{#2}%Time
    & > %Col. Sep.
    & \cellcolor{lightgray}%Duration
    & \cellcolor{lightgray}#1%Icon
    & \cellcolor{lightgray}#3\\[2 pt]%Text
    \gdef\routePreviousTime{#2}%
    \gdef\routeDayStart{#2}%
    \gdef\routeCarTotalKm{0.0}%
    \gdef\routeCarTotalDuration{00:00}%
}

\newcommand{\routeStep}[3]{%{icon}{toTime}{Text}
    \multirow{1}{*}[-1.5ex]{#2}%Time
    & > %Colon Separator
    & \cellcolor{lightgray}\timeDif{\routePreviousTime}{#2}%Duration
    & \cellcolor{lightgray}#1%Icon
    & \cellcolor{lightgray}#3\\[2 pt]%Text
    \gdef\routePreviousTime{#2}%
}

\newcommand{\routeCar}[2]{%{toTime}{km}
    % Car Duration
    \gdef\routeDuration{\timeDif{\routePreviousTime}{#1}}

    % UI
    \multirow{1}{*}[-1.5ex]{#1}%Time
    & > %Colon Separator
    & \routeDuration%Duration
    & \faicon{car}%Icon
    & #2 km\\[2 pt]%km

    % Sum(Car Km)
    \FPadd{\temp}{\routeCarTotalKm}{#2}%
    \FPround{\temp}{\temp}{1}%
    \global\let\routeCarTotalKm\temp%

    % Sum(Car Duration)
    \def\tempd{\expandafter\timeAdd{\routeCarTotalDuration}{\routeDuration}}%
    % \global\let\routeCarTotalDuration\tempd%

    % Backup
    \gdef\routePreviousTime{#1}%
}

\newcommand{\routeStop}[2]{%{icon}{Text}
    \gdef\routeDayStop{\routePreviousTime}%
    %Time
    & > %Colon Separator
    & \cellcolor{lightgray}%Duration
    & \cellcolor{lightgray}#1%Icon
    & \cellcolor{lightgray}#2\\[2 pt]%Text
}

% Duration
\makeatletter
% 1) TimeDif: Expand arguments
\newcommand{\timeDif}[2]%
  {\romannumeral0\expandafter\timeDif@h\expandafter
     {\romannumeral-`0#2}{#1}}%
\def\timeDif@h #1#2{\expandafter\timeDif@i\romannumeral-`0#2:#1:}
% TimeDif: evaluate time difference in minutes
\def\timeDif@i #1:#2:#3:#4:%
   {\expandafter\timeDif@ii\the\numexpr#3*60+#4-#1*60-#2.}%
% TimeDif: correct modulo 24*60 if difference turns out negative
\def\timeDif@ii#1{\expandafter\timeTotalMinutesToHHMM@iii\the\numexpr
                    \if-#11440\fi #1}%
% 2) TimeAdd: Expand arguments
\newcommand{\timeAdd}[2]%
  {\romannumeral0\expandafter\timeAdd@h\expandafter
     {\romannumeral-`0#2}{#1}}%
\def\timeAdd@h #1#2{\expandafter\timeAdd@i\romannumeral-`0#2:#1:}
% TimeAdd: evaluate duration addition in minutes
\def\timeAdd@i #1:#2:#3:#4:%
   {\expandafter\timeTotalMinutesToHHMM@iii\the\numexpr#3*60+#4+#1*60+#2.}%
% 3) TimeTotalMinutesToHHMM: do Euclidean division by 60. Curse \numexpr rounding in passing.
\def\timeTotalMinutesToHHMM@iii#1.{\expandafter\timeTotalMinutesToHHMM@iv\the\numexpr
                    (#1 + 30)/60 -1.#1.}%
% TimeTotalMinutesToHHMM: Get the remainder too, prepare for zero padding
\def\timeTotalMinutesToHHMM@iv #1.#2.{\expandafter\timeTotalMinutesToHHMM@v\the\numexpr
                    100+#2-60*#1\expandafter.\the\numexpr100+#1.}
% TimeTotalMinutesToHHMM: Output final result 
\def\timeTotalMinutesToHHMM@v 1#1.1#2.{ #2:#1}
\makeatother

\begin{document}
\shorthandoff{:}

\begin{routeDetail}{Mardi 1 août}
    \routeStart{\routeHome}{16:03}{}
    \routeCar{17:46}{159}
    \routeStep{\routeCarburant}{17:58}{Octa+}
    \routeCar{18:23}{38.2}
    \routeStep{\routeRestaurant}{20:20}{Royal Wok}
    \routeCar{22:44}{198.2}
    \routeStep{\routeWC}{22:50}{Snack}
    \routeCar{01:27}{224}
    \routeStop{\routeHotel}{Seligweiler}
\end{routeDetail}

\end{document}

1 Answers1

2

You can use \global\edef instead of \global\let, or alternatively \xdef (which is a shortcut for \global\edef, see What are the differences between \def, \edef, \gdef and \xdef?).

I shortened the MWE a bit to remove packages which are already loaded (e.g., colortbl is loaded by \usepackage[table]{xcolor}), packages that are not used at all, and newcommands that are not actually shorter than the original.

\documentclass{article}    

\usepackage[T1]{fontenc}%output font encoding that has support for the accented characters
\usepackage[utf8]{inputenc}%allows the user to input accented characters directly from the keyboard
\usepackage{multirow}
\usepackage[table]{xcolor}
\usepackage{fp}
\usepackage{fontawesome}

\newenvironment{routeDetail}[1]{%{Date}
    \subsection{#1}
    \vspace{0.5cm}
    \begin{tabular}{rlrcl}
}
{%
    \\
    \hline%Summary
    \multicolumn{2}{l}{Journée}% 1) Day
    & \timeDif{\routeDayStart}{\routeDayStop}%Duration
    & \faClockO%Icon
    & \\[2 pt]%Text
    \multicolumn{2}{l}{Trajet}% 2) Car
    & \routeCarTotalDuration%Duration
    & \faicon{car}%Icon
    & \routeCarTotalKm km\\[2 pt]%Text

    \end{tabular}
}


\newcommand{\routeStart}[3]{%{icon}{startTime}{Text}
    \multirow{1}{*}[-1.5ex]{#2}%Time
    & > %Col. Sep.
    & \cellcolor{lightgray}%Duration
    & \cellcolor{lightgray}#1%Icon
    & \cellcolor{lightgray}#3\\[2 pt]%Text
    \gdef\routePreviousTime{#2}%
    \gdef\routeDayStart{#2}%
    \gdef\routeCarTotalKm{0.0}%
    \gdef\routeCarTotalDuration{00:00}%
}

\newcommand{\routeStep}[3]{%{icon}{toTime}{Text}
    \multirow{1}{*}[-1.5ex]{#2}%Time
    & > %Colon Separator
    & \cellcolor{lightgray}\timeDif{\routePreviousTime}{#2}%Duration
    & \cellcolor{lightgray}#1%Icon
    & \cellcolor{lightgray}#3\\[2 pt]%Text
    \gdef\routePreviousTime{#2}%
}

\newcommand{\routeCar}[2]{%{toTime}{km}
    % Car Duration
    \gdef\routeDuration{\timeDif{\routePreviousTime}{#1}}

    % UI
    \multirow{1}{*}[-1.5ex]{#1}%Time
    & > %Colon Separator
    & \routeDuration%Duration
    & \faicon{car}%Icon
    & #2 km\\[2 pt]%km

    % Sum(Car Km)
    \FPadd{\temp}{\routeCarTotalKm}{#2}%
    \FPround{\temp}{\temp}{1}%
    \global\let\routeCarTotalKm\temp%

    % Sum(Car Duration)
    \def\tempd{\expandafter\timeAdd{\routeCarTotalDuration}{\routeDuration}}%
    \global\edef\routeCarTotalDuration{\tempd}%

    % Backup
    \gdef\routePreviousTime{#1}%
}

\newcommand{\routeStop}[2]{%{icon}{Text}
    \gdef\routeDayStop{\routePreviousTime}%
    %Time
    & > %Colon Separator
    & \cellcolor{lightgray}%Duration
    & \cellcolor{lightgray}#1%Icon
    & \cellcolor{lightgray}#2\\[2 pt]%Text
}

% Duration
\makeatletter
% 1) TimeDif: Expand arguments
\newcommand{\timeDif}[2]%
  {\romannumeral0\expandafter\timeDif@h\expandafter
     {\romannumeral-`0#2}{#1}}%
\def\timeDif@h #1#2{\expandafter\timeDif@i\romannumeral-`0#2:#1:}
% TimeDif: evaluate time difference in minutes
\def\timeDif@i #1:#2:#3:#4:%
   {\expandafter\timeDif@ii\the\numexpr#3*60+#4-#1*60-#2.}%
% TimeDif: correct modulo 24*60 if difference turns out negative
\def\timeDif@ii#1{\expandafter\timeTotalMinutesToHHMM@iii\the\numexpr
                    \if-#11440\fi #1}%
% 2) TimeAdd: Expand arguments
\newcommand{\timeAdd}[2]%
  {\romannumeral0\expandafter\timeAdd@h\expandafter
     {\romannumeral-`0#2}{#1}}%
\def\timeAdd@h #1#2{\expandafter\timeAdd@i\romannumeral-`0#2:#1:}
% TimeAdd: evaluate duration addition in minutes
\def\timeAdd@i #1:#2:#3:#4:%
   {\expandafter\timeTotalMinutesToHHMM@iii\the\numexpr#3*60+#4+#1*60+#2.}%
% 3) TimeTotalMinutesToHHMM: do Euclidean division by 60. Curse \numexpr rounding in passing.
\def\timeTotalMinutesToHHMM@iii#1.{\expandafter\timeTotalMinutesToHHMM@iv\the\numexpr
                    (#1 + 30)/60 -1.#1.}%
% TimeTotalMinutesToHHMM: Get the remainder too, prepare for zero padding
\def\timeTotalMinutesToHHMM@iv #1.#2.{\expandafter\timeTotalMinutesToHHMM@v\the\numexpr
                    100+#2-60*#1\expandafter.\the\numexpr100+#1.}
% TimeTotalMinutesToHHMM: Output final result 
\def\timeTotalMinutesToHHMM@v 1#1.1#2.{ #2:#1}
\makeatother

\begin{document}

\begin{routeDetail}{Mardi 1 août}
    \routeStart{\faHome}{16:03}{}
    \routeCar{17:46}{159}
    \routeStep{\faBeer}{17:58}{Octa+}
    \routeCar{18:23}{38.2}
    \routeStep{\faCutlery}{20:20}{Royal Wok}
    \routeCar{22:44}{198.2}
    \routeStep{\faFemale}{22:50}{Snack}
    \routeCar{01:27}{224}
    \routeStop{\faHotel}{Seligweiler}
\end{routeDetail}

\end{document}

Result:

enter image description here

Marijn
  • 37,699