在 tikz 程式碼中使用 tex 計數器

在 tikz 程式碼中使用 tex 計數器

在嘗試寫一些命令來簡化使用 tikz 產生時間軸時,我偶然發現了這個錯誤:

微量元素:

\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都是\EndTime太大的整數,維度無法容納(在 中pt)。

解決方法:

在解析數學表達式之前計算\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}

相關內容