4

I am trying to use the pdfcalendar library, but I think I'm confused by the definitions.

My Question: What kind of LaTeX counter/macro/variable can I use with pgfcalendar functions? Do I have to learn the TeX syntax? If so, will later issues arise when these TeX counters interact with LaTeX commands expecting LaTeX-style variables?

Background The TikZ manual specifies this function:

\pgfcalendardatetojulian{<date>}{<counter>}

and later, where the manual warns that the second argument must be a TeX counter, there is this function:

\pgfcalendarjuliantoweekday{<Julian day>}{<week day counter>}

How do I define <counter> and <week day counter>?

Can I use \newcounter{mycount} or \newcommand{\mycount}?

I've tried all combinations I can think of, and only got partial success.

Some googling led me to differences between TeX and LateX counters, but concludes that apart from syntactical variance, either was reasonably acceptable. But it seems that TikZ/pgfcalendar is wanting something quite specific.

MWE:

\documentclass{article}
\usepackage{tikz,pgfcalendar}
\begin{document} 
\newcommand{\myisodate}{2018-06-02}
\newcommand{\myjuliandatecommand}{}
\newcounter{myjuliandatecounter}
\newcounter{myweekdaycounter}
\newcommand{\myweekdaycommand}{}
\pgfcalendardatetojulian{\myisodate}{\myjuliandatecommand} %works, but prints the value, something I want to avoid.
% \pgfcalendardatetojulian{\myisodate}{myjuliandatecounter} %doesn't parse correctly
% \pgfcalendarjuliantoweekday{\myjuliandatecommand}{myweekdaycounter} %doesn't work
% \pgfcalendarjuliantoweekday{\myjuliandatecommand}{\myweekdaycommand} %doesn't work
\end{document}
Tim
  • 1,539
  • 1
    pgfcalendar, or the whole [tag:pgf], is designed for both TeX and LaTeX. In this case you must use \newcount\mylovelycounter to declare a TeX-counter. – Symbol 1 May 24 '18 at 21:00
  • 1
    Basically, a LaTeX counter named name creates a TeX count named \c@name. – John Kormylo May 25 '18 at 21:49

2 Answers2

0

As Symbol 1 states in their comment, PGF is designed to be usable with TeX so everything is designed for TeX (counts) and thus LaTeX (counters) is technically not supported.

You'd have to use

\pgfcalendardatetojulian{\myisodate}{\c@myjuliandatecounter}
\themyjuliandatecounter

but that would need @ to be a letter (which isn't bad per se and depends on your usecase).

If you don't want to use \c@… you could use \csname c@…\endcsname but that leads us to expansion issues and the same is true for the top-level LaTeX version

\pgfcalendardatetojulian{\myisodate}{\value{myjuliandatecounter}}

The reason why \myjuliandatecommand fails is because PGFcalendar does at some point

\myjuliandatecommand 2458272\relax

and that's just nothing (literally, \myjuliandatecommand is empty) and the number itself just gets typeset.

It's even worse for \myweekdaycommand since that gets used more than once by \pgfcalendarjuliantoweekday


You could do something cheaty like

\def\setcounterJulian#1\relax{%
  \setcounter{myjuliandatecounter}{#1}}
%
\pgfcalendardatetojulian{\myisodate}{\setcounterJulian}
\themyjuliandatecounter

but the same trick won't work for \pgfcalendarjuliantoweekday.


The easiest solution would be to use a TeX count directly, you can even reuse it for \pgfcalendarjuliantoweekday:

\documentclass{article}
\usepackage{pgfcalendar}

\newcommand*\myisodate{2018-06-02} \newcount\PGFcalendarCount

\begin{document} \pgfcalendardatetojulian{\myisodate}{\PGFcalendarCount} Julian date: \the\PGFcalendarCount

\pgfcalendarjuliantoweekday{\PGFcalendarCount}{\PGFcalendarCount} Weekday number: \the\PGFcalendarCount \end{document}

Qrrbrbirlbel
  • 119,821
0

If you want to store the value in a LaTeX counter, define your own wrapper:

\makeatletter
\newcommand{\latexpgfcalendardatetojulian}[2]{%
  \pgfcalendardatetojulian{#1}{\count@}%
  \setcounter{#2}{\count@}%
}
\makeatother

exploiting the scratch TeX counter \count@. If you want to be completely safe, you can add a group

\makeatletter
\newcommand{\latexpgfcalendardatetojulian}[2]{%
  \begingroup
  \pgfcalendardatetojulian{#1}{\count@}%
  \setcounter{#2}{\count@}%
  \endgroup
}
\makeatother

because \setcounter acts globally.

Let's do the experiment, defining also another wrapper.

\documentclass{article}
\usepackage{tikz,pgfcalendar}

\makeatletter \newcommand{\latexpgfcalendardatetojulian}[2]{% \begingroup \pgfcalendardatetojulian{#1}{\count@}% \setcounter{#2}{\count@}% \endgroup } \newcommand{\latexpgfcalendarjuliantoweekday}[2]{% \begingroup \pgfcalendarjuliantoweekday{#1}{\count@}% \setcounter{#2}{\count@}% \endgroup } \makeatother

\newcounter{myjuliancounter} \newcounter{myweekdaycounter}

\begin{document}

\newcommand{\myisodate}{2018-06-02}

\latexpgfcalendardatetojulian{\myisodate}{myjuliancounter}

The stored value is \themyjuliancounter

\latexpgfcalendarjuliantoweekday{\value{myjuliancounter}}{myweekdaycounter}

The stored value is \themyweekdaycounter

\end{document}

enter image description here

With 2022-07-25 (a Monday) we get

enter image description here

egreg
  • 1,121,712