I am currently cleaning up my project to manage it better as it expands in the future. I am wondering if there is a way to manage the following in a smarter and more efficient way.
Currently, this is my LaTeX structure:
main.tex
\include{chapter_1.tex}
\input{fig_1.tikz}
\input{fig_2.tikz}
\include{chapter_2.tex}
\input{fig_1.tikz}
\include{chapter_3.tikz}
Into main.tex
I include chapters like \include{chapter_1.tex}
. Inside each chapter, there might be figures generated from TikZ, which I include like \input{fig_1.tikz}
.
Now, since I would like to keep my project's directory clean, I wanted to have a folder structure, where I have a folder for images, within which images are separated according to the chapters they belong to, like the following:
main_folder/
|-- main.tex
|-- chapter_1.tex
|-- chapter_2.tex
|-- chapter_3.tex
|-- figures/
|-- chapter_1/
|-- generated_fig_0.pdf
|-- generated_fig_1.pdf
|-- tikz/
|-- fig_1.tikz
|-- fig_2.tikz
and so on. Each tikz would then be input like \input{figures/chapter_1/tikz/fig_1.tikz
Now, the problem arises, when I externalize my figure generation from TikZ to speed up my document compilation.
In my document header I have \tikzexternalize[prefix=figures/]
to sort all TikZ images into the figures/
folder. In each included chapter, at the start of the file there is { \tikzset{external/figure name/.add={chapter_1/}{}}
with a closing brace }
after the chapter's body to add a prefix for putting all generated content into figures/chapter_#/
and keep that limited to the scope of the chapter's body.
However, if I want each generated image to have a meaningful name, I need to add \tikzsetnextfilename{image_name}
before each inputted TikZ, such that I would always have blocks like this:
\tikszetnextfilename{foo}
\input{figures/chapter/tikz/foo.tikz}
Is there maybe a straightforward way that if I have a chapter foo.tex
, into which I want to input bar.tikz
, that some \input{bar.tikz}
will automatically look for bar.tikz
in figures/foo/tikz/
and create the resulting externalized PDF as figures/foo/bar.pdf
?
Would the use of LatexMk do anything to this (I want to use it, but I am not sure what role it could play in the above)?
EDIT: It turns out, that \tikzsetnextfilename
actually overwrites all naming definitions except for the initial prefix set with \tikzexternalprefix
. Hence, I can't give my figures filenames directly.
Therefore another question:
Is there a way to a) save the generated images into a subfolder figures/chapter_name/
, and b) give the images invidual names?
Best Answer
You can try something along this:
In
main.tex
:EDIT: A cleaner, more LaTeX-y definition of
\useexternalfile
would beFor the record,
\tikzexternal@filenameprefix
is the internal macro where\tikzsetexternalprefix
saves the "current prefix". Also, you can substitute#1-output
for your favourite expression (maybegenerated-figure-#1
?)END EDIT
In
chapter1.tex
:In
chapter1/tikz/myfigure.tikz
:(I tested this in my OSX system and it works; I use something slightly simpler). This differs from your question in that
myfigure-output.pdf
lives in folderchapter1/tikz/
and not inchapter1/
as you seem to want. Perhaps you (or others) can tweak it a bit more.