tikz コードで tex カウンターを使用する

tikz コードで tex カウンターを使用する

tikz でタイムラインの生成を簡素化するコマンドをいくつか記述しようとしたときに、次のエラーが発生しました。

MWE:

\documentclass[12pt]{article}
\usepackage{tikz}
\usetikzlibrary{calc}

\usepackage{pgfcalendar}


\begin{document}

\newcount\StartTime
\newcount\EndTime
\begin{tikzpicture}
    \pgfcalendardatetojulian{2021-01-01}{\StartTime}
    \pgfcalendardatetojulian{2022-01-01}{\EndTime}
    \typeout{\the\StartTime}
    \draw let
        \n{length} = (\the\StartTime-\the\EndTime),
    in
        (0,0) -- (\n{length},0);
\end{tikzpicture}

\end{document}

実行すると次の出力が得られます:

2459216
/media/daten/coding/01_manuals/tex/testing/chrono/mwe.tex:17: Use of \tikz@cc@stop@let doesn't match its definition.
<recently read> \the

l.17            \n{length} = (\the
                       \StartTime-\the\EndTime),

カウンターの値を取得するのに問題があるようです。私は LaTeX の展開に関しては初心者なので、残念ながら自分ではこれを修正できません (インターネットで調べても役に立つ情報は得られませんでした)。

誰か、Tex カウンターを Tikz に導入するのを手伝ってくれませんか?

答え1

\n{<number register>}の構文は次の通りです

\n{<number register>} = {<pgfmath expression>}

\n{length} = {\the\StartTime-\the\EndTime}したがって、括弧ではなく中括弧にする必要があります。

ただし、とはどちらも次元が保持するには大きすぎる整数であるため ( 内) 、\n{length} = {\the\StartTime-\the\EndTime}では「次元が大きすぎます」というエラーが発生します。\StartTime\EndTimept

回避策:

数式を解析する前に\StartTime、 との差を計算します。これは、解析の前に(別のカウントを使用して) 実行することも、拡張可能な方法でその場で実行することもできます。\EndTimepgfmath\draw

\documentclass[12pt]{article}
\usepackage{tikz}
\usetikzlibrary{calc}

\usepackage{pgfcalendar}

\begin{document}

\newcount\StartTime
\newcount\EndTime
\begin{tikzpicture}
    \pgfcalendardatetojulian{2021-01-01}{\StartTime}
    \pgfcalendardatetojulian{2022-01-01}{\EndTime}
    \typeout{\the\StartTime}
    \draw let
        \n{length} = {\the\numexpr\StartTime-\EndTime pt},
    in
        % \n{length} is -365pt
        (0,0) -- (\n{length},0);
\end{tikzpicture}

\end{document}

関連情報