I use to save different tikz images, tikzexternalize, I have no problem if I let tikz name the pictures or if I needed a name for the next image.
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{external}
\tikzexternalize[prefix=figures-tikz/]
\begin{document}
%\tikzsetnextfilename{circle}
\begin{tikzpicture}
\draw (0,0) circle [radius=1];
\end{tikzpicture}
\end{document}
As I have many included file, I wish I could name the pictures with the file name included rather than the source file name.
I use the currfile package to retrieve the name of the imported file current and I use the following configuration:
\documentclass{article}
\usepackage{tikz}
\usepackage{currfile}
\usetikzlibrary{external}
\tikzexternalize[prefix=figures-tikz/]
\tikzsetfigurename{\currfilename-}
\begin{document}
\begin{tikzpicture}
\draw (0,0) circle [radius=1];
\end{tikzpicture}
\end{document}
this configuration does not work on my PC (windows 10, miketx2.9, texworks 0.61, xelatex or pdflatex)
Of course I configured texworks like the picture (I have a problem with shell-escape or write18)
Best Answer
For this to work, the code for the picture must actually be in an external file. Otherwise, the file name is identical with the name of the main file, which won't work.
You can create such files on-the-fly so the code is in your main
.tex
file, by usingfilecontents
environment before\documentclass
.However, I would find this annoying in work-flow and only use it for MWEs.
The next problem is that
\currentfilename
includes the.tex
extension which is confusing, at best.\currentfilebase
is better. But this needs to be expanded when the figure name is set.However, if we just do this in the preamble, then the name will be set to the name of the main
.tex
document and won't work. So we need to do it inside the external file with the picture's code. For convenience, we can define a wrapper macro.Now we could use
\setmyname
before each picture in an external file. But it would be convenient not to have to do so.If all of your TikZ pictures are in external files and never included inline, then you can add the setting to the
tikzpicture
environment usingetoolbox
.However, you must not do this if you define any TikZ pictures inline in the main file.
Complete code to draw a circle:
EDIT
This version removes the requirement that all externalised images be in external files. However, externalised images in external files will be named according to file name only if they are wrapped in a
document
environment. Furthermore, each external file must include a\documentclass
. These conditions are a result of the fact that the code limits the effect of the naming change by modifyingstandalone
's modifieddocument
environment.Since the code uses
standalone
, the external image files can also include a full preamble, if required, which is convenient for compiling them individually during development. However, a complete preamble is not required. For example, it doesn't matter if the preamble doesn't loadtikz
and just has\documentclass{article}
before\begin{document}
. (Obviously such a file won't compile on its own, but it is fine for inclusion in the main.tex
file.)This relies on the grouping effect illustrated on page 611 of the PGF/TikZ manual.
This produces images in
ffigurau/
as follows:figure-0
,figure-1
,myfig-0
andmyfig2-0
. That is, the two inline pictures get thefigure-
name and the two in external files are named accordingly.Here's a check that it works fine if an external file contains multiple images.
This produces images in
ffigurau/
as follows:figure-0
,figure-1
,myfig-0
,myfig2-0
,myfig2-1
andmyfig3-0
, as expected.EDIT EDIT
This version uses
expl3
, as well asstandalone
,etoolbox
andcurrfile
. It is subject to the same conditions as the version under EDIT i.e. all externalised images in external files must include\documentclass
and adocument
environment.However, unlike the previous version, this one allows you to override the name of the next externalised image in the standard way.
In order to do this, it modifies one TikZ user command (
\tikzsetnextfilename
) and one internal TikZ macro (\tikzexternal@getnextfilename@resetglobals
), as well asstandalone
's replacement fordocument
. This means there are more ways for updates to break the code. The code produces warnings on the console and in the.log
file alerting the user either to the fact that stuff has been modified or to the fact that it hasn't.This produces externalised images in
ffigurau
namedben
,bill
,figure-0
,flowerpotmen
,myfig-0
andmyfig2-0
. That is, the first inline image is namedbill
rather thanfigure-0
(so the second isfigure-0
rather thanfigure-1
) and the first of the two images from the second external file is namedflowerpotmen
rather thanmyfig2-0
(so the second ismyfig2-0
rather thanmyfig2-1
) and the final image from an external file is namedben
rather thanmyfig3-0
. I think this is what is expected here.EDIT EDIT EDIT
Use this only if the
standalone
package must be avoided. If you can't use thestandalone
package, then an alternative way to create a wrapper around the included code is to define a suitable wrapper around\input
.For example, the following defines
\extinput{}
which is very like\input{}
except that it sets included figures' names appropriately. In this case, you don't even needcurrfile
.The downside, of course, is that only files input using
\extinput
rather than\input
will get special treatment and so only they will be named according to their files' names. Also, the current version of the command is very simple and does not attempt to check the file name in any way. So this should not be used unless the file can be input using only its base name, as opposed to a relative or absolute path including an enclosing directory.