如何正確使用 savebox 和 tikz externalize?

如何正確使用 savebox 和 tikz externalize?

打開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}

相關內容