Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Is it possible to load a TikZ library and make it local to a tikzpicture?

I'm writing a long document, and feel like the different TikZ libraries required for different diagrams are "littering" the namespace.

share|improve this question
3  
You can group it but I don't know if you gain much : \documentclass[tikz]{standalone}\begin{document}\begingroup\usetikzlibrary{calc‌​}\tikz \draw(0,0) -- ($(1,1)+(2,2)$);\endgroup\tikz \draw(0,0) -- ($(1,1)+(2,2)$);\end{document} –  percusse Apr 28 '13 at 9:32
add comment

2 Answers

Macro \usetikzlibrary is defined at the end of file tikz.code.tex. The macro remembers the state of a loaded module <foo> in the global macro \tikz@library@<foo>@loaded. If a module is loaded locally, all its local definitions and assignments are lost after the group, but tikz still thinks, the module is loaded and ignores further load requests (example taken from percusse's comment):

\documentclass[tikz]{standalone}
\begin{document}
\begingroup
  \usetikzlibrary{calc}
  \tikz\draw(0,0) -- ($(1,1)+(2,2)$);
\endgroup
\begingroup
  \usetikzlibrary{calc}
  \tikz\draw(0,0) -- ($(1,1)+(2,2)$);
\endgroup
\end{document}       

The result is an error in the second \tikz statement in line 9:

! Package tikz Error: You need to say \usetikzlibrary{calc} for coordinate calculation.

Macro \usetikzlibrary can be patched to make the remembering of the load state local:

\documentclass[tikz]{standalone}

\usepackage{etoolbox}
\makeatletter
\AtBeginDocument{%
  \patchcmd{\use@@tikzlibrary}{\global}{}{}{}%
}
\makeatother

\begin{document}
\begingroup
  \usetikzlibrary{calc}
  \tikz\draw(0,0) -- ($(1,1)+(2,2)$);
\endgroup
\begingroup
  \usetikzlibrary{calc}
  \tikz\draw(0,0) -- ($(1,1)+(2,2)$);
\endgroup
\end{document}

This works for module calc, because its definitions (tikzlibrarycalc.code.tex) are local. But the global load marker shows that libraries are intended to be loaded at top level. There it does not matter, if a macro is defined locally or globally. Thus there might be modules with relevant global assignments that persist after the group and disturb further tikz code.

share|improve this answer
add comment

A way to solve this problem would be to store the TikZ figures as standalone documents:

% file figures/myfigure.tikz
\documentclass{standalone}
\usepackage{tikz}    
\usetikzlibrary{shapes,arrows}
\begin{document}
  \begin{tikzpicture}
    % picture goes here
  \end{tikzpicture}
\end{document}

In the main file, include the generated pdf with \includegraphics{figures/myfigure.pdf}.

In order to let latexmk know that it should recompile the PDF files when they are outdated, add this to a .latexmkrc:

add_cus_dep( 'tikz', 'pdf', 0, 'tikz2pdf' );
sub tikz2pdf {
    $_[0] =~ m@^(.*/)([^/]+)$@;
    my ( $dir, $file ) = ($1,$2);
    system("pdflatex -output-directory $dir $_[0].tikz");
}

The latexmk part will only work if you don't include any other PDF files. If you do, you can probably solve it by using another extension for these.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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