如何正确使用带有 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}

相关内容