Tx · LaTeX & TikZ

How to Use a Custom TikZ Shape as a Reusable Building Block

Published By the 9to5Science editorial team

At a glance

  1. 1. Choose the right tool

    Use a pic for a reusable drawing; use a node shape when you need standard node anchors and shape behavior.

  2. 2. Define the component

    Place its drawing commands in a named pic definition, using local coordinates.

  3. 3. Reuse and customize

    Insert the pic wherever needed, and pass options such as scale or color.

  4. 4. Connect components

    Use nodes or explicit coordinates when the drawing must connect to other TikZ objects.

A custom TikZ drawing can be reused instead of copying its \draw commands each time. For a visual component—an icon, symbol, or small group of paths—the simplest building block is usually a pic. Define the pic once, then place it multiple times with \pic.

A pic is not a node: it is a small drawing. That makes it easy to reuse, but it does not automatically provide the usual node anchors or text layout. If you need those features, use a node-based approach instead.

Define a reusable pic

Here is a complete example. It defines a simple house icon and places it twice. Save it as a .tex file and compile it with a LaTeX engine that supports TikZ.

\documentclass{standalone}
\usepackage{tikz}

\tikzset{
  pics/house/.style={
    code={
      \draw[fill=blue!15] (0,0) rectangle (1,0.7);
      \draw[fill=red!20] (0,0.7) -- (0.5,1.2) -- (1,0.7) -- cycle;
      \draw (0.25,0) rectangle (0.45,0.4);
      \draw (0.65,0.35) rectangle (0.85,0.55);
    }
  }
}

\begin{document}
\begin{tikzpicture}
  \pic at (0,0) {house};
  \pic[scale=0.7] at (2,0) {house};
\end{tikzpicture}
\end{document}

The pics/house/.style entry defines a pic named house. Its code contains ordinary TikZ drawing commands, and the coordinates are local to the pic. The origin is at its placement point, so the first house extends from (0,0)(0,0) to (1,1.2)(1,1.2) in its local coordinate system.

Each \pic command inserts an instance of that definition. The second instance is scaled to 70% of its original size. You can also pass drawing options to a pic, for example \pic[draw=green] ..., but those options affect the drawing only when its code uses the relevant styles. In this example, the paths specify fill colors but otherwise inherit the drawing style.

Add labels or vary the appearance

A pic can include text in its code, just like any other TikZ drawing. For example, adding \node at (0.5,0.35) {A}; inside the code places a label in the house body. That label is fixed for every instance in this basic definition.

If you want to customize a label or color per instance, define a parameterized pic or set up styles that the pic’s paths use. Keep the drawing commands tied to styles rather than hard-coding every color. Then an instance can override those styles without duplicating the geometry. Parameterized pics use TikZ’s pics/.../.style args mechanism; check the PGF/TikZ manual for its argument syntax when defining one.

You can also wrap a pic in a small macro if your document needs a simpler command interface. A pic definition is generally easier to extend with TikZ options and is a good default for components that remain inside a tikzpicture.

When to use a node shape instead

Use a pic when you want to reuse a drawing as a unit. Use a node when the component needs behavior such as:

  • anchors like north, south, or east for connecting arrows;
  • automatic text placement and inner or outer separation;
  • node sizing and alignment based on its text;
  • references to the component by its node name.

For a simple labeled box, a styled node is often enough: \node[draw, minimum width=2cm] (part) {Part};. For a more elaborate node geometry, PGF also supports declaring custom node shapes with \pgfdeclareshape. That is a lower-level interface: you must specify the shape’s saved dimensions, anchors, and drawing behavior. Prefer a pic unless you specifically need node semantics.

Common mistakes

  • Defining a pic but not placing it. The pics/house style registers a definition; it does not draw anything until you use \pic {house}; inside a tikzpicture.
  • Expecting a pic to behave like a named node. A pic is not automatically a node with convenient shape anchors. Add coordinates or nodes for connection points when needed, or choose a node-based design.
  • Using absolute coordinates inside the component. Coordinates such as (0,0) and (1,0.7) are local to the pic. This is what lets the whole drawing move and scale as a unit.
  • Assuming every option changes the drawing. Options such as fill=... only take effect if the pic’s paths use or inherit that style. Set styles on the paths or define style hooks for customization.
  • Forgetting the TikZ package. The example requires \usepackage{tikz} in the preamble; without it, the tikzpicture environment and commands are unavailable.

References

  1. PGF/TikZ Manual

Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.