1

Based on this answer, I try to detect if a TikZ library is already loaded or not.

For a reason I ignore, it cannot detect external while it's called in the preambule.

enter image description here

Adapted MWE

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{tikz}
\usetikzlibrary{calc}
\usetikzlibrary{external}

\makeatletter \newcommand*@tikzextension{code.tex} \def\iftikzlibraryloaded#1{% @ifl@aded@tikzextension{tikzlibrary#1} } \makeatother

\iftikzlibraryloaded{external} {\def\OUTPUT{external loaded}} {\def\OUTPUT{external not loaded}}

\iftikzlibraryloaded{calc} {\def\OUTPUTT{calc loaded}} {\def\OUTPUTT{calc not loaded}}

\begin{document}

\OUTPUT

\OUTPUTT

\end{document}

JeT
  • 3,020

1 Answers1

2

The difference is that tikzlibrarycalc.tex.code, the source file loaded by \usetikzlibrary{calc}, begins with line \ProvidesFileRCS{tikzlibrarycalc.code.tex} but tikzlibraryexternal.tex.code doesn't contain any use of \ProvidesFileRCS.

\ProvidesFileRCS{<file>} will eventually call \ProvidesFile{<file>}, which makes the test \@ifl@aded{<ext>}{<file>} works.

If all you want is to detect if external library is loaded, then you can detect if any of commands exclusively provided by it is defined, for example

\ifdefined\tikzexternalize
  \expandafter\@firstoftwo
\else
  \expandafter\@secondoftwo
\fi
{<loaded>}{<otherwise>}

And if you want to further detect if the externalization is activated, \tikzifexternalizing{<true>}{<false>}, used after \tikzexternalize, is at your service.

Update

I've added an issue to pgf-tikz, see https://github.com/pgf-tikz/pgf/issues/1162. Also I find there're more tikz libraries that lack \ProvidesFileRCS, hence \@ifl@aded is not a reliable detecter for tikz libraries.

Update 2

An example employing @egreg's answer, the accepted answer to the question OP linked.

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{tikz}
\usetikzlibrary{calc}
\usetikzlibrary{external}

\makeatletter % \IfTikzLibraryLoaded{<lib>}{<true code>}{<false code>} \def\IfTikzLibraryLoaded#1{% \ifcsname tikz@library@#1@loaded\endcsname \expandafter@firstoftwo \else \expandafter@secondoftwo \fi } \makeatother

\begin{document}

calc: \IfTikzLibraryLoaded{calc}{loaded}{not loaded} \par external: \IfTikzLibraryLoaded{external}{loaded}{not loaded}

\end{document}

Output:

calc: loaded
external: loaded
muzimuzhi Z
  • 26,474
  • Thank you for this answer I would NEVER have figured out (really...). May I ask you to include your answer in a compilable code ? :) – JeT Jun 01 '22 at 18:39
  • @egreg's answer, the accepted answer to the question you linked, which tests if \tikz@library@<lib>@loaded is defined, (still) works. – muzimuzhi Z Jun 01 '22 at 18:46
  • shame on me but I did not know how to integrate egreg's answer in compilable code :/ – JeT Jun 01 '22 at 18:47
  • See answer update :D – muzimuzhi Z Jun 01 '22 at 18:53
  • Just to give some background, I sometimes need to \tikzexternalenable or disable some graphes in my code. I wanted to have an automatic way to include them without errors when I include the same piece of code without externalize. – JeT Jun 01 '22 at 18:58
  • 1
    A tricky way is to use \csname tikzexternalenable\endcsname. – muzimuzhi Z Jun 01 '22 at 19:06