我当前的代码生成的结果如下图所示。
我的问题是:如何累积“汽车”的持续时间?
当 95 号线
% \全局\让\routeCarTotalDuration\tempd%
取消注释,我得到异常
TeX 容量超出,抱歉 [扩展深度=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
您可以使用\global\edef
代替\global\let
,或者\xdef
(这是 的快捷方式\global\edef
,请参阅\def、\edef、\gdef 和 \xdef 之间有什么区别?)。
我稍微缩短了 MWE,删除了已经加载的包(例如,colortbl
由 加载的包\usepackage[table]{xcolor}
)、根本没有使用的包以及实际上并不比原始命令短的新命令。
\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}
结果: