[Tex/LaTex] Creating TiKz Libraries

tikz-pgftikzlibrary

How does one go about creating a TiKz library (is it even possible)? Is this a formidable task for a beginner? As a chemical engineer, I would very much like to have a library to create process flow diagram, like the one below, in which you place nodes with certain shapes and connect them based on specific ports.

enter image description here

Best Answer

I think the simplest answer is "it depends". The purpose of this answer is to show you how easy it is to set up a library.

Creating a library itself is not hard. It's just a naming convention.

tikzlibrary<library name>.code.tex

I don't know anything about chemical engineering or the symbols you wish to use. So the following is just a very simple presentation of how you might consider approaching this.

Here's a very simple and rather boring example of a library which I call unitcircle. Save this to a file called tikzlibraryunitcircle.code.tex and either place it in the current directory for the document or wherever you store your custom code.

I've never created node shapes: if you want to create new shapes, you might want to check out creating node shapes. So, I'll leave that part up to you. But, creating pics is relatively easy. And, that's what I do here.

\def\aeunitcircleradius{1cm}
\tikzset{
  quadrant I/.pic={%%
    \draw[line width=1pt] (-\aeunitcircleradius/2,\aeunitcircleradius/2)  arc (90:0:\aeunitcircleradius);
    \path (0,0)                  coordinate (-center)
                -- ++ (0.5,-0.5) coordinate (-se)
                -- ++ (0,1.0)    coordinate (-ne)
                -- ++ (-1,0)     coordinate (-nw)
                -- ++ (0,-1)     coordinate (-sw);
    },
  quadrant II/.pic={%%
    \draw[line width=1pt] (-\aeunitcircleradius/2,-\aeunitcircleradius/2)  arc (180:90:\aeunitcircleradius);
    \path (0,0)                  coordinate (-center)
                -- ++ (0.5,-0.5) coordinate (-se)
                -- ++ (0,1.0)    coordinate (-ne)
                -- ++ (-1,0)     coordinate (-nw)
                -- ++ (0,-1)     coordinate (-sw);
    },
  quadrant III/.pic={%%
    \draw[line width=1pt] (-\aeunitcircleradius/2,\aeunitcircleradius/2)  arc (180:270:\aeunitcircleradius);
    \path (0,0)                  coordinate (-center)
                -- ++ (0.5,-0.5) coordinate (-se)
                -- ++ (0,1.0)    coordinate (-ne)
                -- ++ (-1,0)     coordinate (-nw)
                -- ++ (0,-1)     coordinate (-sw);
    },
  quadrant IV/.pic={%%
    \draw[line width=1pt] (-\aeunitcircleradius/2,-\aeunitcircleradius/2)  arc (-90:0:\aeunitcircleradius);
    \path (0,0)                  coordinate (-center)
                -- ++ (0.5,-0.5) coordinate (-se)
                -- ++ (0,1.0)    coordinate (-ne)
                -- ++ (-1,0)     coordinate (-nw)
                -- ++ (0,-1)     coordinate (-sw);
    },
}

Here's a standalone document that calls this new TikZ library:

\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\usetikzlibrary{unitcircle}
\begin{document}

\begin{tikzpicture}

  \path
              (0,0) pic (LL) {quadrant III}
        -- ++ (1,0) pic (LR) {quadrant IV}
        -- ++ (0,1) pic (UR) {quadrant I}
        -- cycle
        -- ++ (0,1) pic (UL) {quadrant II}
      ;
  \draw[blue] (LR-ne) -- (LR-sw);

  \draw[red] (LL-ne) rectangle (LL-sw);

  \draw (LL-center) -- (UR-center)
                    -- (UL-center)
                    -- (LR-center);
\end{tikzpicture}

\end{document}

Here's the result:

enter image description here

You might take some time to examine some of the TikZ library files on your system such as tikzlibraryarrows.code.tex. There are also libraries created by others such as hobby and tikzmark. It may be worth your time to open up those files and look at how things are done there.