2

I'm trying to write a macros for converting day time to a format, which the pgfgrantt package can understand. Here it is:

\newcommand{\htime}[2][0]{
  \StrBefore{#2}{:}[\hours]
  \StrBehind{#2}{:}[\minutes]
  \pgfmathparse{\hours+\minutes/60+#1}
  \pgfmathresult
}

\newcommand{\gantthbar}[3]{
  \def\from{\htime[1]{#2}}
  \def\to{\htime{#3}}
  \ganttbar{#1}{\from}{\to}
}

% \gantthbar{test}{11:15}{12:00} -> \ganttbar{test}{12.25}{12.0}

Compilation failed with an error, I can't resolve:

! Use of \\ganttbar doesn't match its definition.
\kernel@ifnextchar ...rved@d =#1\def \reserved@a {
                                                  #2}\def \reserved@b {#3}\f...
l.37   \gantthbar{test}{11:15}{12:00}

?

This one compiles without errors:

\newcommand{\gantthbar}[3]{
  \def\from{\htime[1]{#2}}
  \def\to{\htime{#3}}
  \ganttbar{#1 \from \to}{12.25}{12.0}
}

Thanks for your help.

Very minimal working example I wrote after reading comments:

\documentclass[a4paper, 12pt]{report}

\usepackage{xstring}
\usepackage{pgfgantt}

\newcommand{\gantthbar}[3]{
  \StrBefore{#2}{:}[\hoursfrom]
  \StrBehind{#2}{:}[\minutesfrom]
  \StrBefore{#3}{:}[\hoursto]
  \StrBehind{#3}{:}[\minutesto]
  \pgfmathsetmacro\from{\hoursfrom + \minutesfrom / 60 + 1}
  \pgfmathsetmacro\to{\hoursto + \minutesto / 60}
  \ganttbar{#1}{\from}{\to}
}

\begin{document}
\begin{ganttchart}[vgrid]{24}
  \gantthbar{test}{11:15}{12:00}
\end{ganttchart}
\end{document}
kermzyxer
  • 73
  • 5

0 Answers0