12

I want to write some demo code showing code with examples for tikz. However I want the resulting pdf to be automatically depended on the tikz libraries and therefore I must test which are loaded.

Is this functionality implemented in tikz already or can I test for the existence of special commands?

EDIT:

Example which shows that the code does not work outside the preamble. I however need code that works everywhere:

\documentclass[]{scrbook}

\usepackage{pgf}
\usepackage{tikz}

\usetikzlibrary{circuits}

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

\listfiles

\begin{document}    
\IfTikzLibraryLoaded{circuits}{is loaded}{not loaded}
\end{document}

2 Answers2

16

When tikz loads the library foo, it defines the macro \tikz@library@foo@loaded. Therefore the following code will do the job:

\makeatletter
\def\IfTikzLibraryLoaded#1{%
  \ifcsname tikz@library@#1@loaded\endcsname
    \expandafter\@firstoftwo
  \else
    \expandafter\@secondoftwo
  \fi
}
\makeatother
egreg
  • 1,121,712
  • I made a remix of this solution (thanks!) to make it a one-liner that can go inside the document rather than on the preamble. Here it goes: \def\CheckTikzLibraryLoaded#1{ \ifcsname tikz@library@#1@loaded\endcsname \else \PackageWarning{tikz}{You forgot to add usetikzlibrary{#1} in the preamble!} \fi } \CheckTikzLibraryLoaded{circuits} – ferdymercury Oct 20 '21 at 12:36
8

you can create a new command based on the definition of the command \@ifpackageloaded:

In the example below I provided the new conditional \iftikzlibraryloaded with the syntax:

\iftikzlibraryloaded{<name>}%
 {True Code}%
 {False Code}

Here the complete example:

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

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

\begin{document}
\OUTPUT
\end{document}
Marco Daniel
  • 95,681
  • You are not using \@ifpackageloaded, at least I see not where. What is \@ifl@aded actually doing and what is your meaning of \@tikzextension ? Is this code working also outside the preamble? – Matthias Pospiech Mar 18 '12 at 09:20
  • @MatthiasPospiech: \@ifpackageloaded is defined as \def\@ifpackageloaded{\@ifl@aded\@pkgextension}. Do you see what I mean?. A library of tikz has the following syntax tikzlibrary<Name of Library>.code.tex and the command \@ifl@aded requires two arguments. The first one is the extension. – Marco Daniel Mar 18 '12 at 09:24
  • Ok, but as far as I know \@ifpackageloaded only works in preamble. – Matthias Pospiech Mar 18 '12 at 09:53
  • @MatthiasPospiech: That's right. In the file latex.ltx you can find the relevant part \@onlypreamble\@ifpackageloaded. I don't set this. – Marco Daniel Mar 18 '12 at 09:55
  • Ok, so If I use only \@ifl@aded I do not have this proble, correct? – Matthias Pospiech Mar 18 '12 at 10:46
  • @matthiaspospiech it s correct – Marco Daniel Mar 18 '12 at 10:49
  • Well, I tried it and it gave me ! LaTeX Error: Can be used only in preamble. I added my test code to the question. – Matthias Pospiech Mar 18 '12 at 11:05
  • @MarcoDaniel latex.ltx has \@onlypreamble\@ifl@aded too. So this command can't be used after \begin{document}. But this is not the problem: files loaded by \usetikzlibrary are not listed by \listfiles, because they lack the necessary \ProvidesFile. Hence no method based on \@ifl@aded can work with them. – egreg Mar 18 '12 at 13:45
  • @egreg: I didn't see \@onlypreamble\@ifl@aded ;-( -- However \@ifl@aded works. Why do you think it can't handle that? – Marco Daniel Mar 18 '12 at 14:41
  • @MarcoDaniel In your case it works because you define \OUTPUT in the preamble. Moreover it doesn't work with the circuits library, for example (with calc it does because the \ProvidesFile is in it). – egreg Mar 18 '12 at 14:52
  • @egreg: That's really bad. I tested with the wrong library ;-) – Marco Daniel Mar 18 '12 at 14:54