0

I have the following tree:

.
├── main.tex
├── output
└── tikz

I would like to compile main.tex using output for LaTeX output, but having the tikz PDFs externalised in the tikz directory, which would be created if missing. The output directory should be detected from the -output-directory CLI option.

So far, I am using the following code.

\documentclass{article}
\usepackage[abspath]{currfile}
\getabspath{\jobname.log} % gives \theabsdir
\IfFileExists{"\theabsdir ../tikz"}{}{\immediate\write18{mkdir "tikz"}}
\typeout{theabsdir: \theabsdir ../tikz}
\typeout{currfiledir: \currfiledir tikz}
 
\usepackage{tikz}
\usetikzlibrary{external}
\tikzexternalize
\tikzset{%
  external/system call={pdflatex \tikzexternalcheckshellescape --halt-on-error
    --interaction=batchmode --output-directory="\currfiledir tikz"
 --jobname "\image" "\texsource"},
  /pgf/images/include external/.code={\includegraphics{"\currfiledir tikz/#1"}}
}
      
\begin{document}
\tikz \fill (0,0) circle(5pt);.
\end{document}

And I compile it with:

pdflatex -shell-escape -recorder -output-directory=output main   

From main.log, I see that \theabsdir ../tikz and \currfiledir tikz are output/../tikz and tikz, as expected, also, the tikz directory is created, if missing. The PDF for the figure, main-figure0.pdf, is correctly created in it, along with the log and the dpth file, but main-figure0.md5 goes to the output dir. Possibly for this reason, I get

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

It bears mentioning that, if I hit Enter, as suggested, the document is correctly generated.

Note: I also unsuccessfully tried to add in the preamble:

\RequirePackage{graphicx}
\graphicspath{{tikz/}}
2
  • Have you try \tikzexternalize[prefix=tikz/]?
    – Tom
    May 31, 2022 at 20:32
  • @Tom, well, yes, several times. \tikzsetexternalprefix writes the MD5s in ./output/tikz/, not in ./tikz/ and breaks (even if the dir exits). I was creating the code to add this case to my question, but I forgot to remove the previous tikz folder, and... I found the solution: two dirs, one in, one out the output dir.
    – antonio
    May 31, 2022 at 22:51

1 Answer 1

2

By sheer luck, I found the answer.
My code was intended as a workaround to \tikzsetexternalprefix, which writes the MD5 files (used to determine whether to rebuild the figure) inside the output dir and still breaks. While creating a further example to document this (in reply to @Tom's comment), I forgot to remove the external tikz folder, and found that it worked.

So the solution is to create two tikz folders, one inside and the other outside the output dir. There is a minor inconvenience: when you trash the output dir, you lose the status and have to rebuild all the figures. So one should consider keeping it, if the project is not yet finished.

Here's the code:

\documentclass{article}
\usepackage[abspath]{currfile}
\getabspath{\jobname.log} % gives \theabsdir
\typeout{theabsdir: \theabsdir ../tikz} % just for debug
\IfFileExists{"\theabsdir ../tikz"}{}{\immediate\write18{mkdir "tikz"}}
\IfFileExists{"\theabsdir /tikz"}{}{\immediate\write18{mkdir "\theabsdir tikz"}}
     
\usepackage{tikz}
\usetikzlibrary{external}  
\tikzexternalize
\tikzsetexternalprefix{tikz/}
      
\begin{document}
\tikz \fill (0,0) circle(5pt);.
\end{document}

This time both the internal and the external tikz dirs are built if missing.

Of course, a better solution, with one folder, is very welcomed.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .