tikz externalize で savebox を適切に使用するにはどうすればよいですか?

tikz externalize で savebox を適切に使用するにはどうすればよいですか?

saveboxがオンになっていると、寸法を適切にキャプチャできませんtikzexternalize

MWE は次のとおりです。

\documentclass {article}

\RequirePackage {tikz}
\RequirePackage {expl3}

\usetikzlibrary {shapes}
\usetikzlibrary {external}

% Make every {tikzpicture} block as separate *.png image.
\tikzexternalize [prefix = tests/]
\tikzsetfigurename {test_}
\tikzset{export as png/.style={external/system call/.add = {}{; convert -density 300 -resize 800x600 "\image.pdf" "\image.png"}}}
\tikzset{export as png}

\ExplSyntaxOn

    \tikzset{external/export~next = false} % This is needed for savebox to work.

    \newsavebox \sandbox
    \savebox \sandbox
    {
        \begin {tikzpicture}
            \node [ellipse, minimum~size = 10pt] { };
        \end {tikzpicture}
    }

    \dim_const:Nn \c_ellipse_width {\the\wd\sandbox\space}
    \dim_const:Nn \c_something {16pt}

\ExplSyntaxOff



\begin {document}

    \ExplSyntaxOn

        \dim_log:N \c_ellipse_width
        \dim_log:N \c_something

        \begin {tikzpicture}

            \dim_log:N \c_ellipse_width
            \dim_log:N \c_something
            \node [ellipse, align = center] {\the\c_ellipse_width \\ \the\c_something};

        \end {tikzpicture}

    \ExplSyntaxOff

\end {document}

このようにコンパイルすると、メインの *.log に正しい値が表示されます。
> \c_ellipse_width=10.0pt.
> \c_something=16.0pt.

tests/test_0.log は幅が変わり、次のように表示されます。
> \c_ellipse_width=405.83112pt.
> \c_something=16.0pt.

言うまでもなく、画像にはtests/test_0.png不正確な値が描画されます。

関連するコードの一部がtikzexternalizeコメントアウトされている場合、メインの *.log には次のように表示されます。
> \c_ellipse_width=10.0pt.
> \c_something=16.0pt.

このシナリオでは、*.pdf コンテンツに正しい値が描画されます。

tikzpictureオンのときの寸法を正確に測定するにはどうすればいいですかtikzexternalize?

答え1

結局のところ、この問題は tikz の最適化に関連しています。
この問題を回避するには、 で最適化を無効にするだけです\tikzset{external/optimize = false}

解決:

\documentclass {article}

\RequirePackage {tikz}
\RequirePackage {expl3}

\usetikzlibrary {shapes}
\usetikzlibrary {external}

% Make every {tikzpicture} block as separate *.png image.
\tikzexternalize [prefix = tests/]
\tikzsetfigurename {test_}
\tikzset{export as png/.style={external/system call/.add = {}{; convert -density 300 -resize 800x600 "\image.pdf" "\image.png"}}}
\tikzset{export as png}
\tikzset{external/optimize = false} % <--------------------------------

\ExplSyntaxOn

    \tikzset{external/export~next = false} % This is needed for savebox to work.

    \newsavebox \sandbox
    \savebox \sandbox
    {
        \begin {tikzpicture}
            \node [ellipse, minimum~size = 10pt] { };
        \end {tikzpicture}
    }

    \dim_const:Nn \c_ellipse_width {\the\wd\sandbox\space}
    \dim_const:Nn \c_something {16pt}

\ExplSyntaxOff



\begin {document}

    \ExplSyntaxOn

        \dim_log:N \c_ellipse_width
        \dim_log:N \c_something

        \begin {tikzpicture}

            \dim_log:N \c_ellipse_width
            \dim_log:N \c_something
            \node [ellipse, align = center] {\the\c_ellipse_width \\ \the\c_something};

        \end {tikzpicture}

    \ExplSyntaxOff

\end {document}

関連情報