2

I recently externalized some of my often used TikZ commands into a library. This is as simple as writing the commands to a dedicated file and naming it tikzlibraryname.code.tex. Then it can be included using \usetikzlibrary{name}.

But this only works if it is placed in the same directory as the file that includes it or resides at some globally known include path. Trying a relative path like \usetikzlibrary{../name} results in an error

! I can't find file `tikzlibrary../name.code.tex'.

\input{../tikzlibraryname.code.tex} would be a workaround. Using the proper command \usetikzlibrary should be preferred though.

Can a tikz library be loaded from a relative path?

  • 2
    Add the custom file and it's path to your texmf – percusse Dec 7 '13 at 12:13
  • The library is part of a project also for others to access. I want to avoid directing everyone to move the file into their local texmf directory. – XZS Dec 7 '13 at 12:17
  • 1
    \usetikzlibrary simply adds tikzlibrary in front of the arguments separated by ,. Those files are supposed to be found by TeX. We could patch \usetikzlibrary to test for a . or a / at the start of the filename and parse the string so that it does ../tikzlibraryname.code.tex but that fix needs to be able to be found by TeX or placed directly in the document. If the user changes the placement of the tikzlibraryname.code.tex outside of a path that can be found by TeX they might just be better off to use \input? (So the answer would be "No.") – Qrrbrbirlbel Dec 7 '13 at 16:00
  • 1
    @XZS: Hardcoding paths is wrong. – Martin Schröder Dec 7 '13 at 23:31
  • 1
    Why not package your "often used TikZ commands" in a .sty file instead? That would be easier. – jubobs Dec 8 '13 at 18:59
1

The following is the definition of \usetikzlibrary

\def\usetikzlibrary{\pgfutil@ifnextchar[{\use@tikzlibrary}{\use@@tikzlibrary}}%}
\def\use@tikzlibrary[#1]{\use@@tikzlibrary{#1}}
\def\use@@tikzlibrary#1{%
  \edef\pgf@list{#1}%
  \pgfutil@for\pgf@temp:=\pgf@list\do{%
    \expandafter\pgfkeys@spdef\expandafter\pgf@temp\expandafter{\pgf@temp}%
    \ifx\pgf@temp\pgfutil@empty
    \else
      \expandafter\ifx\csname tikz@library@\pgf@temp @loaded\endcsname\relax%
      \expandafter\global\expandafter\let\csname tikz@library@\pgf@temp @loaded\endcsname=\pgfutil@empty%
      \expandafter\edef\csname tikz@library@#1@atcode\endcsname{\the\catcode`\@}
      \expandafter\edef\csname tikz@library@#1@barcode\endcsname{\the\catcode`\|}
      \expandafter\edef\csname tikz@library@#1@dollarcode\endcsname{\the\catcode`\$}
      \catcode`\@=11
      \catcode`\|=12
      \catcode`\$=3
      \pgfutil@InputIfFileExists{tikzlibrary\pgf@temp.code.tex}{}{
        \pgfutil@IfFileExists{pgflibrary\pgf@temp.code.tex}{%
          \expandafter\usepgflibrary\expandafter{\pgf@temp}%
        }{%
          \tikzerror{I did not find the tikz library
            '\pgf@temp'. I looked for files named
            tikzlibrary\pgf@temp.code.tex and
            pgflibrary\pgf@temp.code.tex, but neither
            could be found in the current texmf trees.}
        }}%
      \catcode`\@=\csname tikz@library@#1@atcode\endcsname
      \catcode`\|=\csname tikz@library@#1@barcode\endcsname
      \catcode`\$=\csname tikz@library@#1@dollarcode\endcsname
      \fi%
    \fi
  }%
}

Notice that \usetikzlibrary[fpu] is just an alternative to \usetikzlibrary{fpu}. It does not eat options.

From the definition you can see that \usetikzlibrary takes cares of the following

  • Check if the library is loaded. Some library can only be loaded once.
  • Remember the catcode of sensitive characters @, |, and $. This allows you to use \tikzlibrary almost anywhere. In face, you can use it inside a tikzpicture.
  • Test if the file exists. If so, input it.

The definition of the last check is

\def\pgfutil@InputIfFileExists#1#2#3{\pgfutil@IfFileExists{#1}
    {\input #1\relax#2}
    {#3}}%

you can modify its definition by

\def\pgfutil@InputIfFileExists#1#2#3{\pgfutil@IfFileExists{\tikz@library@directory #1}
    {\input \tikz@library@directory #1\relax#2}
    {#3}}%

with customized \tikz@library@directory.

  • \usetikzlibrary[fpu] is for context if I remember correctly – percusse Apr 22 '17 at 19:29
  • @percusse Almost forgot ConTeXt. Yes, it definitely is. As the manual suggests. – Symbol 1 Apr 22 '17 at 19:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.