0

I am writing a document which includes several figures done in tikz and I want to use the same color in all of them. Currently, I am defining all of them in each file as \definecolor{LightBlue}{HTML}{dae8fc} for example. I would like to have a library with all the colors defined for all .tikz files.

I have a tikzlibrarymycolors.code.tex file with:

\definecolor{lightBlue}{HTML}{dae8fc}
\definecolor{darkBlue}{HTML}{6c8ebf}
\definecolor{darkGreen}{HTML}{82b366}
\definecolor{ligthGreen}{HTML}{d5e8d4}

And for each figure I include \usetikzlibrary{mycolors}. However, I get the error Package tikz: I did not find the tikz library 'mycolors'. I looked for files named tikzlibrarymycolors.code.tex and pgflibrarymycolors.code.tex, but neither could be found in the current texmf trees.

I want to keep this library just locally to the current document. Do I need to build the texmf tree? I am using https://tug.org/texlive/quickinstall.html

Thanks for the help.

muzimuzhi Z
  • 26,474
aripod
  • 107
  • Where is your tikzlibrarymycolors.code.tex located? If it's in the same directory with your main tex file, compilation should work with no errors. – muzimuzhi Z Jun 29 '22 at 16:19
  • It is actually in a sub-directory. The main tex file is main.tex and the tikzlibrary with the .tikz files are in figures/ – aripod Jul 04 '22 at 13:08
  • May I suggest you define the colors in the preamble ? So that you'll be allowed to call it everywhere in your document. – Piroooh Jul 05 '22 at 05:54
  • The issue with defining in the preamble is that I am also using tikzedt. That's why I'd rather have them in a separate file that I could also use with the editor – aripod Jul 05 '22 at 07:12

1 Answers1

1

By convention user-provided class, package, and package library files can, or should be either in the current working directory or in one of TDS (TeX Directory Structure) tress. See https://tug.org/texlive/doc/texlive-en/texlive-en.html#x1-110002.3 for a full list.

Using \input or \usepackage and the alike, one can specify a relative path like \input{./sub/myfile.tex}. But that won't win for \tikzuselibrary because you only gives it middle part of the file name, hence the control of file path prefixes is lost.

Fortunately, LaTeX provides an \input@path which can store a list of directories to search for input files. The last step is to make tikz/pgf aware of \input@path. See related pgf issue #565, Use \graphicspath to search for images.

$ tree .
.
├── libs
│   └── tikzlibrarydemo.code.tex
└── main.tex

$ cat libs/tikzlibrarydemo.code.tex % do sth so we know demo lib is successfully loaded \tikzerror{lib demo}

\endinput

% main.tex
\documentclass{article}
\usepackage{tikz}

\makeatletter
\let\pgfutil@InputIfFileExists=\InputIfFileExists
% not necessary for current use case, but is actually more useful
% \let\pgfutil@IfFileExists=\IfFileExists

\def\input@path{{./libs/}}
\makeatother

\usetikzlibrary{demo}

\begin{document}
content
\end{document}

Update: Do the color definitions and possibly other tikz-relevant codes have to be loaded in the form of a tikz library? Because there's almost no differences between \tikzuselibrary{demo} and <optional \makeatletter>\input tikzlibrarydemo.code.tex<optional \makeatother>.

muzimuzhi Z
  • 26,474
  • Would it be easier/better to define all tikz-related in a "regular" .tex and include it in the preamble? – aripod Jul 06 '22 at 07:06
  • 1
    If relative path is involved, yes "regular" form is easier. Typically a pacakge/library is for some macros/settings that will be used by multiple main documents, and even different users. – muzimuzhi Z Jul 06 '22 at 08:07