leo-colisson / robust-externalize

A LaTeX library to cache pictures (including tikz, python code, and more) in a robust, customizable, and pure way.
7 stars 2 forks source link

robust-externalize fails to handle code that cares about newlines (such as inline tables well in tikz) #43

Open JasonGross opened 2 months ago

JasonGross commented 2 months ago

Parallel to https://github.com/sasozivanovic/memoize/issues/19 This code only works if I comment out \cacheTikz

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepackage{robust-externalize}
\cacheTikz
\robExtConfigure{
  add to preset={tikz}{
    add to preamble={
      \usepackage{pgfplots}
      \pgfplotsset{compat=newest}
    }
  },
}
\begin{document}
\begin{tikzpicture}
\begin{axis}
\addplot [semithick, black]
table {%
0 0
0.0752933174371719 0.0355731099843979
};
\end{axis}
\end{tikzpicture}
\end{document}

Otherwise it errors with

(/usr/local/texlive/2024/texmf-dist/tex/generic/pgfplots/pgfplots.markers.code.

(robExt)                tex)
(robExt)                ! Package pgfplots Error: Could not read table file
(robExt)                '"0 0 0.0752933174371719 0.
(robExt)                0355731099843979 "' in 'search path=.'. In case you
(robExt)                intended to provide inline
(robExt)                data: maybe TeX screwed up your end-of-lines? Try
(robExt)                `row sep=crcr' and terminate
(robExt)                your lines with `\\' (refer to the pgfplotstable
(robExt)                manual for details).
(robExt)                !  ==> Fatal error occurred, no output PDF file
(robExt)                produced!
(robExt)                Transcript written on
(robExt)                robExt-346C97BA17A0E18DFE55ADF736F7902C.log.
(robExt)
(robExt)                ^^^^^^
(robExt)                See full logs below (you might need to press ENTER
(robExt)                to go to the next error) or in
(robExt)
robustExternalize/robExt-346C97BA17A0E18DFE55ADF736F7902C-compilation.log.

Type <return> to continue.
 ...

l.23 \end
         {tikzpicture}
?
tobiasBora commented 2 months ago

The problem here is that pgf expects the lines to be preserved. The problem is that LaTeX gives us 2 options to parse the inputs:

The solution here is to use CacheMeCode instead of CacheMe (used internally by tikz), which uses the verbatim mode. This can be done by replacing

\begin{tikzpicture}
…
\end{tikzpicture}

with

\begin{CacheMeCode}{tikzpicture}
\end{CacheMeCode}

like in:

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepackage{robust-externalize}
\cacheTikz
\robExtConfigure{
  add to preset={tikz}{
    add to preamble={
      \usepackage{pgfplots}
      \pgfplotsset{compat=newest}
    }
  },
}

\begin{document}
\begin{CacheMeCode}{tikzpicture}
\begin{axis}
\addplot [semithick, black]
table {%
0 0
0.0752933174371719 0.0355731099843979
};
\end{axis}  
\end{CacheMeCode}
\end{document}

You can also choose a different name like:

\NewDocumentEnvironment{tikzpictureVerb}{}{\CacheMeCode{tikzpicture}}{\endCacheMeCode}

or replace the existing tikzpicture environment with the verbatim version (warning: not sure if it can have some side effects):

\RenewDocumentEnvironment{tikzpicture}{}{\CacheMeCode{tikzpicture}}{\endCacheMeCode}

MWE:

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepackage{robust-externalize}
\cacheTikz
\robExtConfigure{
  add to preset={tikz}{
    add to preamble={
      \usepackage{pgfplots}
      \pgfplotsset{compat=newest}
    }
  },
}

%% Create a new env
\NewDocumentEnvironment{tikzpictureVerb}{}{\CacheMeCode{tikzpicture}}{\endCacheMeCode}
%% Replace existing env
\RenewDocumentEnvironment{tikzpicture}{}{\CacheMeCode{tikzpicture}}{\endCacheMeCode}

\begin{document}
\begin{tikzpicture}
\begin{axis}
\addplot [semithick, black]
table {%
0 0
0.0752933174371719 0.0355731099843979
};
\end{axis}
\end{tikzpicture}
\end{document}

If you find this solution good enough for your need, we can close this.

tobiasBora commented 2 months ago

In any case, I'll let this issue open until I document this behavior, and I'd like to try to fix https://tex.stackexchange.com/questions/718003/alias-of-verbatim-environment-with-optional-arguments