Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I'm trying to make a command for converting an hour (ex: "14:17") to a decimal value (hour + min/60 = 14.28333333333) for use in a TikZ schedule :

\creneau[day=1, start=13.5, end=\hourtodec{14:17}]{Maths}

-> Monday (day=1), 13:30 to 14:17 : Maths

I know already how to extract the numbers from 14:17 with the xstring package (\StrBefore{14:17}{:} and \StrBehind{14:17}{:}), but I don't know how to calculate the minutes (calc package works only for integers).

share|improve this question
5  
tikz itself can do a lot of math, e.g. \pgfmathsetmacro{\decimalday}{\hours+\minutes/60}. Have a look at the manual, section 62 Evaluating Mathematical Expressions –  Tom Bombadil Sep 7 '11 at 17:27
    
@Tom, could you elaborate that as an answer? Looks like the most clean and simple answer so far. –  Juan A. Navarro Sep 14 '11 at 9:12

3 Answers 3

up vote 6 down vote accepted

Can be done by TeX itself:

\documentclass{article}
\usepackage[francais]{babel}
\makeatletter
\newcommand*\dechour[1]{\expandafter\dechour@i#1\@nil}
\def\dechour@i#1:#2\@nil{\strip@pt\dimexpr #1pt+#2pt/60\relax}
\makeatother
\begin{document}

\shorthandoff{:}
\dechour{12:31}

\dechour{12:30}

\def\mbf{10:20} and \dechour{\mbf}
\end{document}

enter image description here

share|improve this answer
    
Why it's don't work when i use \usepackage[francais]{babel} ? –  iDavid Sep 7 '11 at 18:45
1  
the character : is active in french and not a normal letter. Write \shorthandoff{:} before \dechour{12:31} –  Herbert Sep 7 '11 at 19:00
    
I use your solution, \shorthandoff{:} works fine, and now, I try \def\mbd{09:30} \def\mbf{10:20} but \dechour{\mbd} don't work : ! Argument of \dechour@i has an extra } –  iDavid Sep 7 '11 at 19:55
    
see my edited answer. If you want to use macros for the time, then you need an \expandafter. See code –  Herbert Sep 7 '11 at 20:07
    
Thanks, It's work perfectly, I can now fill in my agenda without having to calculate every hour:) –  iDavid Sep 7 '11 at 20:33
\usepackage{fp}

\newcommand\dechour[1]{\splithour#1!}
\def\splithour#1:#2!{\FPupn\result{60 #2 / #1 +}\FPround\result\result{5}\result}

\begin{document}
\dechour{12:31}

\dechour{12:30}
\end{document}

This prints

12.51667
12.50000

share|improve this answer
    
I have this error in my document : Runaway argument? 8:30! \end {document} ! File ended while scanning use of \splithour. <inserted text> \par <*> temp.tex –  iDavid Sep 7 '11 at 18:12
    
You must be missing a } somewhere, or perhaps a paragraph break in \dechour? –  Werner Sep 7 '11 at 18:21
    
Don't find : pastebin.com/DyuDM7zA –  iDavid Sep 7 '11 at 18:34
    
That snippet is not compilable. It misses \documentclass and definitions for \jours and \creneau. –  egreg Sep 7 '11 at 23:51

Although it is possible to parse such simple expressions and do such simple calculations in TeX, I prefer to use luatex for such tasks. Here is a solution in ConTeXt, where I am using a predefined lpeg pattern to match an integer.

\startluacode
  local C, P    = lpeg.C, lpeg.P
  local integer = lpeg.patterns.integer
  local match   = lpeg.match

  local function hourtodec(hour, min)
    return hour + min/60
  end

  local pattern = C(integer) * P(':') * C(integer) / hourtodec

  function commands.hourtodec(hour)
    return context(match(pattern, hour))
  end
\stopluacode

\def\hourtodec#1{\ctxcommand{hourtodec("#1")}}

\starttext
\hourtodec{14:17}
\stoptext

Almost the same solution works in LaTeX by replacing some of the convenience functions provided by ConTeXt by lua code

\documentclass{article}

\usepackage{luacode}

\begin{luacode}
  local C, P, R = lpeg.C, lpeg.P, lpeg.R
  local integer = R('09')^1
  local match   = lpeg.match

  local function hourtodec(hour, min)
    return hour + min/60
  end

  local pattern = C(integer) * P(':') * C(integer) / hourtodec

  commands = commands or {}

  function commands.hourtodec(hour)
    return tex.print(match(pattern, hour))
  end
\end{luacode}

\def\hourtodec#1{\directlua{commands.hourtodec("#1")}}

\begin{document}
\hourtodec{14:17}
\end{document}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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