Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
JasonGross opened this issue May 10, 2024 · 2 comments

Comments

@JasonGross
Copy link

Parallel to sasozivanovic/memoize#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
Copy link
Contributor

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:

  • in "macro" mode (default in robust-externalize): can be used anywhere, but the format is not precisely preserved (new lines are removed for instance, lines starting with % are removed…)
  • in "verbatim" mode: the formating is preserved, but this cannot be used inside most macros

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
Copy link
Contributor

tobiasBora commented May 15, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants