# Runaway argument? with \def

I have a time value in the format HH:MM (i.e 03:00).

I need to compute the TotalMinutes of this time (expected result 180).

My current code is

\documentclass{article}

\makeatletter

\newcommand{\timeHHMMToTotalMinutes}[1]%
{\timeHHMMToTotalMinutes@h#1}%
\def\timeHHMMToTotalMinutes@h#1:#2{\the\numexpr#1*60+#2}%

\makeatother

\begin{document}
\def\tOne{03:00}%
\timeHHMMToTotalMinutes{\tOne}\\%
\end{document}


but unfortunatelly, it gives me error:

Runaway argument?

\tOne \\end {document}

! File ended while scanning use of \timeHHMMToTotalMinutes@h.

How to I solve this? What did I misunderstood about argument parsing?

• You have to expand \tOne before calling \timeHHMMToTotalMinutes. \expandafter\timeHHMMToTotalMinutes\expandafter{\tOne} would work. Adding an \expandafter to the command definition also works: \newcommand{\timeHHMMToTotalMinutes}[1]% {\expandafter\timeHHMMToTotalMinutes@h#1}% – Phelype Oleinik Jun 12 '18 at 11:33

According to your definition, the input

\timeHHMMToTotalMinutes{\tOne}


gets transformed into

\timeHHMMToTotalMinutes@h\tOne


Since the auxiliary macro expects to see

<tokens>:<argument>


TeX will look for a colon until getting to a \par token or the end of the file. The reason is that TeX does no macro expansion when collecting arguments to macros.

Thus you need to expand \tOne before TeX expands \timeHHMMToTotalMinutes@h and starts looking for its arguments.

Anyhow, there is another fix to do. If you define

\newcommand{\timeHHMMToTotalMinutes}[1]%
{\expandafter\timeHHMMToTotalMinutes@h#1}


then your call will become first

\expandafter\timeHHMMToTotalMinutes@h\tOne


and then

\timeHHMMToTotalMinutes@h 03:00


The collected arguments will be 03 for #1 and 0 for #2. The second 0 will remain in the input stream and you'll get

\the\numexpr#1*60+00


which provides the correct result, but by chance. Something like

\timeHHMMToTotalMinutes{\tOne}23


will surprise you. Better delimiting also the second argument and terminating \numexpr with \relax.

\documentclass{article}

\makeatletter

\newcommand{\timeHHMMToTotalMinutes}[1]{%
\expandafter\timeHHMMToTotalMinutes@h#1\@nil
}
\def\timeHHMMToTotalMinutes@h#1:#2\@nil{%
\the\numexpr#1*60+#2\relax
}

\makeatother

\begin{document}

\def\tOne{03:00}%
\timeHHMMToTotalMinutes{\tOne}23

\end{document}


This prints 18023. You code, with just \expandafter added would print 203.

• Do we have to handle the expansion for a "more than one" argument command in a different way? With the following code, I still get the same error: \newcommand{\timeStamp}[2]{%{#1-Date (YYYY.MM.DD)}{#2-Time (HH:MM)} \expandafter\timeStamp@t#1 #2\@nil%}% \def\timeStamp@t#1.#2.#3 #4:#5\@nil{% \the\numexpr#5+#4*60+(#3-1)*60*24+(#2-1)*60*24*31+(#1-2017)*60*24*31*365\relax% }%...\def\tOne{03:00}%\def\dOne{2017.08.01}%\timeStamp{\dOne}{\tOne}\\% – Joseph Marie Jun 18 '18 at 10:01
• @JosephMarie It's very difficult to read the code in comments; please ask a followup question. – egreg Jun 18 '18 at 10:03
• Indeed! Here is my new question: tex.stackexchange.com/questions/436868/… – Joseph Marie Jun 18 '18 at 10:25