0

I am unable to properly capture dimensions with savebox while tikzexternalize is on.

Here is the 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}

When compiled like this, the main *.log will show correct values:
> \c_ellipse_width=10.0pt.
> \c_something=16.0pt.

While the tests/test_0.log will change the width and show:
> \c_ellipse_width=405.83112pt.
> \c_something=16.0pt.

Needless to say, the picture tests/test_0.png will draw incorrect values.

When parts of code regarding tikzexternalize are commented-out, the main *.log shows:
> \c_ellipse_width=10.0pt.
> \c_something=16.0pt.

The *.pdf content has correct values drawn in this scenario.

How can I correctly measure dimensions of tikzpicture while tikzexternalize is on?

0

As it turns out, the problem is related to tikz optimizations.
To avoid this issue, just disable optimizations with \tikzset{external/optimize = false}.

Solution:

\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}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.