1

I'm trying to use a command that calculates a number of days in the context of defining the minimum width for a tikz node:

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{positioning}
\usepackage{datenumber}

\begin{document}

\begin{tikzpicture}%
    \newcounter{startdate}
    \newcounter{enddate}

    \newcommand{\dayssincetimelinestart}[3]{%
          \setmydatenumber{startdate}{1900}{01}{01}%
          \setmydatenumber{enddate}{#1}{#2}{#3}%
          \addtocounter{enddate}{-\thestartdate}%
          \theenddate%
    }

    \node[minimum width=\dayssincetimelinestart{2011}{01}{01}] at (0,0){\dayssincetimelinestart{2011}{01}{01}};

\end{tikzpicture}

\end{document}

The calculation is working because the output shows:

enter image description here

However, the attempt to use the calculation for minimum width is giving two errors like this:

! Package PGF Math Error: Unknown operator `=' or `=1' (in '\@nom =1900\relax \
@den =100\relax \divide \@nom by \@den \multiply \@nom by \@den \@den =1900\adv
ance \@den by -\@nom \@nom =1900\relax \@den =4\relax \divide \@nom by \@den \m
ultiply \@nom by \@den \@den =1900\advance \@den by -\@nom \@tempcnta =28 \gdef
 {}\@tempcntb = 31\relax \global \c@@dayscnt 0\relax \relax \@tempcnta =1800\de
f \iterate {\@nom =\@tempcnta \relax \@den =100\relax \divide \@nom by \@den \m
ultiply \@nom by \@den \@den =\@tempcnta \advance \@den by -\@nom \@nom =\@temp
cnta \relax \@den =4\relax \divide \@nom by \@den \multiply \@nom by \@den \@de
n =\@tempcnta \advance \@den by -\@nom \global \advance \c@@dayscnt 365\relax \
advance \@tempcnta by 1 \relax \iterate }\iterate \let \iterate \relax \global 
\advance \c@@dayscnt 0\relax \@nom =1900\relax \@den =100\relax \divide \@nom b
y \@den \multiply \@nom by \@den \@den =1900\advance \@den by -\@nom \@nom =190
0\relax \@den =4\relax \divide \@nom by \@den \multiply \@nom by \@den \@den =1
900\advance \@den by -\@nom \global \advance \c@@dayscnt 01\relax \global \c@st
artdate 77067\relax \@nom =2011\relax \@den =100\relax \divide \@nom by \@den \
multiply \@nom by \@den \@den =2011\advance \@den by -\@nom \@nom =2011\relax \
@den =4\relax \divide \@nom by \@den \multiply \@nom by \@den \@den =2011\advan
ce \@den by -\@nom \@tempcnta =28 \gdef {}\@tempcntb = 31\relax \global \c@@day
scnt 0\relax \relax \@tempcnta =1800\def \iterate {\@nom =\@tempcnta \relax \@d
en =100\relax \divide \@nom by \@den \multiply \@nom by \@den \@den =\@tempcnta
 \advance \@den by -\@nom \@nom =\@tempcnta \relax \@den =4\relax \divide \@nom
 by \@den \multiply \@nom by \@den \@den =\@tempcnta \advance \@den by -\@nom \
global \advance \c@@dayscnt 365\relax \advance \@tempcnta by 1 \relax \iterate 
}\iterate \let \iterate \relax \global \advance \c@@dayscnt 0\relax \@nom =2011
\relax \@den =100\relax \divide \@nom by \@den \multiply \@nom by \@den \@den =
2011\advance \@den by -\@nom \@nom =2011\relax \@den =4\relax \divide \@nom by 
\@den \multiply \@nom by \@den \@den =2011\advance \@den by -\@nom \global \adv
ance \c@@dayscnt 01\relax \global \c@enddate 77067\relax \global \advance \c@en
ddate -36525\relax 40542').

How can I use the command in an expression assigned to the minimum width?

1 Answer 1

1

You need to do the non-expandable commands before being in the \node options. At least \addtocounter is one of these, because its definition from latex.ltx is:

\def\addtocounter#1#2{%
  \@ifundefined{c@#1}%
    {\@nocounterr{#1}}%
    {\global\advance\csname c@#1\endcsname #2\relax}}

and \advance is a TeX assignment (working here on a \count register). Therefore, in order to solve the problem, you can do as follows:

\documentclass{article}
\usepackage{tikz}
\usepackage{datenumber}

\newcounter{startdate}
\newcounter{nbdays}

\newcommand*{\dayssincetimelinestart}[3]{%
  \setmydatenumber{startdate}{1900}{01}{01}%
  \setmydatenumber{nbdays}{#1}{#2}{#3}%
  \addtocounter{nbdays}{-\thestartdate}%
  %\thenbdays % commented out: just compute, don't print
}

\begin{document}

\begin{tikzpicture}
  % Compute a new value for the 'nbdays' counter (this does non-expandable things)
  \dayssincetimelinestart{2011}{01}{01}
  % Then use it (\thenbdays is fully expandable -> no problem)
  \node[draw, minimum width=60*\thenbdays sp] at (0,0) { \thenbdays };
\end{tikzpicture}

\end{document}

Screenshot

I used a scaling factor of 60 and the unit sp, because the result 40542 or your calculation can't be used as a TeX length measure in points (\maxdimen is 16383.99998pt). And if I assume you want the length expressed in sp unit, then it would represent 0.618621pt (40542 divided by 65536) and wouldn't be visible at all, since the node contents is larger than that. 60 is somewhat of an arbitrary factor, but gives macroscopic (visible) and reasonable output here. :-)

If this is not what you want, please say which unit you want to use to make a length out of the computed number.

2
  • Awesome! A bit clunky to achieve some of these things in LaTeX, but glad it's achievable at this point. I actually multiple the number of days by a length to achieve final result and it's all working. Thanks!
    – me--
    Jun 11, 2019 at 10:02
  • Good to hear. :-)
    – frougon
    Jun 11, 2019 at 10:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .