12

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

3
  • Well, the fact they exist stands testament to the possibility. Usually, the answer to 'is it possible' is yes, so it's best to get on to the real question :) May 28, 2015 at 3:46
  • @SeanAllred I add the possible statement because I saw that circuitikz is provided as a package not library. So I thought maybe a package would be the correct choice.
    – jak123
    May 28, 2015 at 4:10
  • 1
    Ah, I see :) A reasonable guess. If I recall correctly, the difference between LaTeX packages and TikZ libraries is nominal – literally. TikZ libraries follow a naming convention. May 28, 2015 at 4:36

2 Answers 2

19

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.

6
  • Any idea what's the advantage of defining a tikz library over a regular package?
    – tobiasBora
    Oct 9, 2021 at 13:51
  • @tobiasBora Yes. Regular packages, while offering amazing functionality etc, still only have limited offerings that don't always correspond with the needs I have for the sorts of documents I create. When you create the same sort of code over and over again, you begin to think, "it would be nice just to be able to pre-load this as a library." So, that's what I learned to do. My custom libraries are probably of no use to anyone else, they're tailored to my needs. But having such libraries, saves me time and effort to do repetative tasks, and makes my code more readable.
    – A.Ellett
    Oct 9, 2021 at 15:15
  • @tobiasBora I should caution something though about private libraries. If you're an academic writing papers and interested in publishing them, private libraries could potentially be a handicap. I no longer have an interest in publishing math research papers. I write documents for my students. I enjoy the creativity that goes into creating those documents. Both LaTeX and TikZ allow me to easily create the sorts of documents I wish to create hence my continued interest in creating documents with these tools.
    – A.Ellett
    Oct 9, 2021 at 15:45
  • Thanks for the answer. Of course I love libraries, but I meant why not just create a .sty file like most packages, instead of using a tikz library.
    – tobiasBora
    Oct 9, 2021 at 21:44
  • @tobiasBora Ah! Because I can then use the \usetikzlibrary{myPersonalLibrary}. Of course, I'm only using such a library for TikZ-related things.
    – A.Ellett
    Oct 9, 2021 at 22:04
4

The program DIA can export your drawing as a tex file. I point this out because it already has a Chemical engineering library. My answer doesn't help users learn how to write shape libraries, but it can help a user get quality latex images a chemical engineering document/drawing... or at least give some basic tex code to build a library off of.

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.