1

This question already has an answer here:

I've written a command that takes a number of minutes and formats it like so: Xh Xmin.

The logic of the command works fine, but if I use it in combination with other command or itself, it produces unwanted spacing in front of the text.

The Command

\newcommand{\timeFormat}[1]
{
\def\hours{}
\def\minutes{}
\IfInteger{#1}
{
    \ifnum\intcalcDiv{#1}{60} > 0
        \appto\hours{\intcalcDiv{#1}{60}}
    \fi
    \ifnum\intcalcMod{#1}{60} > 0
        \appto\minutes{\intcalcMod{#1}{60}}
    \fi
    \ifthenelse{\equal{\hours}{}}
    {\ifthenelse{\equal{\minutes}{}}
        {}
        {\minutes min}}
    {\ifthenelse{\equal{\minutes}{}}
        {\hours h}
        {\hours h \minutes min}}
}{}
}

Dependencies

\usepackage{intcalc}
\usepackage{xstring}

Usage

\timeFormat{100}\timeFormat{1}\timeFormat{60}

Result

Ouptut

So far, I haven't figured out how to get rid of the spacing and I've run out of ideas.
How can i fix this?

marked as duplicate by Steven B. Segletes, Zarko, user36296, Stefan Pinnow, Jesse Aug 31 '17 at 12:25

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 1
    Every end-of-line will add a space, except for the ones after constants in the \ifnum tests and the ones following the first two arguments of \ifthenelse. – egreg Aug 31 '17 at 11:09
  • Get rid of those spaces at end-of-lines by adding a % character to the end of the offending lines of your macro (or each line for assurance). See tex.stackexchange.com/questions/7453/… – Steven B. Segletes Aug 31 '17 at 11:13
1

Almost every end-of-line in your code will add a space.

\newcommand{\timeFormat}[1]
{%
  \def\hours{}%
  \def\minutes{}%
  \IfInteger{#1}
   {%
    \ifnum\intcalcDiv{#1}{60} > 0
        \appto\hours{\intcalcDiv{#1}{60}}%
    \fi
    \ifnum\intcalcMod{#1}{60} > 0
        \appto\minutes{\intcalcMod{#1}{60}}%
    \fi
    \ifthenelse{\equal{\hours}{}}
      {\ifthenelse{\equal{\minutes}{}}
        {}
        {\minutes min}}%
      {\ifthenelse{\equal{\minutes}{}}
        {\hours h}
        {\hours h \minutes min}}%
   }{}%
}

Spaces are ignored after a required constant and when looking for an argument.

With xparse and expl3 there's little worry for spaces.

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\timeFormat}{m}
 {
  \clutch_time_format:n { #1 }
 }
\int_new:N \l_clutch_time_hour_int
\int_new:N \l_clutch_time_min_int

\cs_new_protected:Nn \clutch_time_format:n
 {
  \int_set:Nn \l_clutch_time_hour_int
   {
    \int_div_truncate:nn { #1 } { 60 }
   }
  \int_set:Nn \l_clutch_time_min_int
   {
    \int_mod:nn { #1 } { 60 }
   }
  \int_compare:nT { \l_clutch_time_hour_int > 0 }
   {
    \int_to_arabic:n { \l_clutch_time_hour_int }\,h
    \int_compare:nT { \l_clutch_time_min_int > 0 }
     {
      \nobreakspace
     }
   }
  \int_compare:nT { \l_clutch_time_min_int > 0 }
   {
    \int_to_arabic:n { \l_clutch_time_min_int }\,min
   }
 }
\ExplSyntaxOff

\begin{document}

---\timeFormat{100}---

---\timeFormat{1}---

---\timeFormat{60}---

\end{document}

enter image description here

Not the answer you're looking for? Browse other questions tagged or ask your own question.