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}

1 Answer 1

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
6
  • 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 1, 2022 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. Jun 1, 2022 at 18:46
  • shame on me but I did not know how to integrate egreg's answer in compilable code :/
    – JeT
    Jun 1, 2022 at 18:47
  • 1
    See answer update :D Jun 1, 2022 at 18:53
  • 1
    A tricky way is to use \csname tikzexternalenable\endcsname. Jun 1, 2022 at 19:06

You must log in to answer this question.

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