1

I'm writing my PhD manuscript and I've got a lots of figures. What I want is:

  • use externalized tikz as the graphs are saved as .dat and there's a lots,
  • store the externalized files in an explicit directory,
  • be able to set the tikz figures size easily,
  • be able to switch to \includegraphics with generated pdf (because I put it in my lab sharelatex which does not support shell-escape).

My answers are:

  • use tikz/pgfplots externalization library,
  • set the file name using \tikzsetnextfilename,
  • set the image width using the tikzscalepackage,
  • make my own \includetikz which looks like the following (inside the preamble).
% Tikz & co
\usepackage{tikz,pgfplots}
\pgfplotsset{compat=1.13}
\usepackage{tikzscale}

% Tikz libraries
\usepgfplotslibrary{fillbetween}
\usetikzlibrary{decorations.markings}
\usetikzlibrary{patterns}
\usetikzlibrary{babel}

% Externalized tikz
\usepgfplotslibrary{external}
\tikzexternalize
\tikzexternaldisable

% Command to include and controll the tikz figure size
%
% Use it as \includetikz[width=0.2\textwidth]{/path/to/file}{myfile}
% with /path/to/file/myfile.tikz being a file.
%
\def\usepdf{}
\newcommand{\includetikz}[3][]{%
    % Get file extension
    \filename@parse{#2/#3}
    %
    % Check if tikz should be built or not.
    \ifx\usepdf\empty
        % Enable externalization.
        \tikzexternalenable
        % Set next file name.
        \tikzsetnextfilename{#2/build/\filename@base}
        % Include graphics
        \includegraphics[#1]{#2/\filename@base.tikz}
        % Disable externalization.
        \tikzexternaldisable
    \else
      \includegraphics[#1]{#2/build/\filename@base.pdf}
    \fi
}

\usepackage{style/tikz}

This command is used inside the file as

\def\path{img/chapitre2}
\includetikz[width=0.8\textwidth, height=0.4\textwidth]{\path}{eels_spectra.tex}

with eels_spectra.tikzas:

\begin{tikzpicture}
    \begin{axis}[]
        \addplot [] table [x=eV, y=loc3] {\path/data/eels_spectra.dat};
    \end{axis}
\end{tikzpicture}

Last, the .dat file is like

eV loc1 loc2 loc3
463.57605489759953 1168.0 1127.0 1146.0
463.8996794786981 1292.0 1038.0 1285.0
464.22330405979665 1289.0 979.0 1155.0
464.5469286408952 1339.0 1088.0 1229.0
...

I've noticed that there were no problem in case the text inside the .dat were put inside the \addplotcommand, but with \path/data/eels_spectra.dat, the pdflatex command gives me:

===== 'mode=convert with system call': Invoking 'pdflatex -shell-escape -halt-o
n-error -interaction=batchmode -jobname "img/chapitre2/build/eels_spectra" "\de
f\tikzexternalrealjob{main}\input{main}"' ========
This is pdfTeX, Version 3.14159265-2.6-1.40.20 (TeX Live 2019) (preloaded format=pdflatex)
 \write18 enabled.
entering extended mode
system returned with code 256

! Package tikz Error: Sorry, the system call 'pdflatex -shell-escape -halt-on-e
rror -interaction=batchmode -jobname "img/chapitre2/build/eels_spectra" "\def\t
ikzexternalrealjob{main}\input{main}"' did NOT result in a usable output file '
img/chapitre2/build/eels_spectra' (expected one of .pdf:.jpg:.jpeg:.png:). Plea
se verify that you have enabled system calls. For pdflatex, this is 'pdflatex -
shell-escape'. Sometimes it is also named 'write 18' or something like that. Or
 maybe the command simply failed? Error messages can be found in 'img/chapitre2
/build/eels_spectra.log'. If you continue now, I'll try to typeset the picture.


See the tikz package documentation for explanation.
Type  H <return>  for immediate help.
 ...                                              

l.47 \end{tikzpicture}

? 

and the job log writes:

Package pdftex.def Info: img/chapitre2/eels_spectra_haadf.png  used on input li
ne 86.
(pdftex.def)             Requested size: 193.47581pt x 107.86781pt.
(./img/chapitre2/eels_spectra.tikz
PGFPlots: reading {/home/etienne/ownCloud/Manuscrit/eels_spectra.dat}
) (./img/chapitre2/eels_spectra.tikz
PGFPlots: reading {/home/etienne/ownCloud/Manuscrit/eels_spectra.dat}
) (./img/chapitre2/eels_spectra.tikz
PGFPlots: reading {/home/etienne/ownCloud/Manuscrit/eels_spectra.dat}
) (./img/chapitre2/eels_spectra.tikz
\openout4 = `img/chapitre2/build/eels_spectra.dpth'.

PGFPlots: reading {/home/etienne/ownCloud/Manuscrit/eels_spectra.dat}
! Argument of \path has an extra }.
<inserted text> 
                \par 
l.35 ...enne/ownCloud/Manuscrit/eels_spectra.dat};

Here is how much of TeX's memory you used:
 54569 strings out of 492167
 1263128 string characters out of 6123477
 2177993 words of memory out of 5000000
 57937 multiletter control sequences out of 15000+600000
 590919 words of font info for 79 fonts, out of 8000000 for 9000
 1141 hyphenation exceptions out of 8191
 89i,20n,125p,10125b,2476s stack positions out of 5000i,500n,10000p,200000b,80000s

!  ==> Fatal error occurred, no output PDF file produced!

How can I import the .datfile in that case ? It's so heavy to import the .dat inside the .tikz !!!

EDIT

I've forgotten to mention that the .dat file were correctly imported if the \tikzsetnextfilename{#2/build/\filename@base}were removed in the definition of \includetikz. I.e. defining the job name makes the .dat import crash !

1 Answer 1

4

Well, Well, Well ... Nobody was inspired by my question.

Here is the answer I found.

It sounds like \tikzsetnextfilename is not made to specify a directory. One should either use the prefix. In other words:

  • Use the prefix to specify the directory where the job should be saved,
  • Use \tikzsetnextfilename to specify the job name.

What I made is to define the following command

\newcommand{\setpath}[1]{% Input like /path/to/file
    \graphicspath{{#1/}}% sets /path/to/file/
    \def\tikzpath{#1}% sets /path/to/file
}

Hence, at every begining of chapter, I use \setpath{/path/to/chapter/images}to define the current path to images (png as tikz).

The command includetikz` became:

\def\usepdf{}
\def\tikzpath{}
\newcommand{\includetikz}[2][]{%
    % Get file extension
    \filename@parse{\tikzpath/#2}
    %
    % Check if tikz should be built or not.
    \ifx\usepdf\empty
        % Enable externalization.
        \tikzexternalenable
        % Set file prefix
        \tikzsetexternalprefix{\tikzpath/build/}
        % Set next file name.
        \tikzsetnextfilename{\filename@base}
        % Include graphics
        \includegraphics[#1]{\tikzpath/\filename@base.tikz}
        % Disable externalization.
        \tikzexternaldisable
    \else
        \includegraphics[#1]{\tikzpath/build/\filename@base.pdf}
    \fi
}

and I now include tikz images inside each chapter with \includetikz[width=0.8\textwidth]{my_image.tikz}.

And managing the externalization with prefixes solves my .dat file importation problem.

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.