# LSTM cell in TikZ: how the gate diagram is drawn

> How the LSTM cell diagram is built in TikZ: gates at fixed coordinates, perpendicular coordinates, branching arrows, a line crossing, and two variations.

URL: https://www.labcanvas.com/tikz/examples/machine-learning/lstm-cell-detailed

Updated 2026-11-22. Original figure: LSTM cell (detailed), © Fraser Love, MIT licence (licence text: https://www.labcanvas.com/tikz-library/credits.html#nntikz), from https://github.com/fraserlove/nntikz/blob/3dc29cfda3bd18db2d1e73830f95b4d0985e050f/tikz/lstm.tex. Walkthrough and variations by LabCanvas; the variation code is CC0 1.0 (https://creativecommons.org/publicdomain/zero/1.0/). Pictures typeset with TeX Live 2026 and PGF 3.1.12. Category: [TikZ neural network and machine learning examples](https://www.labcanvas.com/tikz/examples/machine-learning).

## Quick answer

This figure draws one LSTM cell: the cell state c runs along the top through a multiplication and an addition, the hidden state h and the input x feed three σ gates and a tanh, and the output h t leaves at the bottom right and up to ŷ t. Every gate is a small node at a fixed coordinate inside one large rounded rectangle, and every connection is a path made of `-|` and `|-` segments with rounded corners, often starting from a perpendicular coordinate such as `(x |- ht-1)`. The branch to ŷ t crosses the cell-state line with a deliberate gap, made by stopping the path just short of it.

## The figure and its code

![Detailed LSTM cell: a large rounded rectangle with the cell state line from c t−1 to c t through a multiply and an add node at the top, three sigma gates and a tanh box fed by h t−1 and x t at the bottom, labels f t, i t, c tilde t and o t, a grey tanh and multiply node producing h t, and a branch up to the output y hat t.](https://www.labcanvas.com/tikz-snippets/fig-lstm-cell-detailed/original.webp)

Copyright (c) 2024 Fraser Love, MIT licence (https://www.labcanvas.com/tikz-library/credits.html#nntikz). The code below is exactly the library's file (pdfLaTeX); its first lines name the source and the licence, and the MIT licence asks that they stay with the code. Open it in the TikZ editor from the page: https://www.labcanvas.com/tikz/examples/machine-learning/lstm-cell-detailed

```latex
% LSTM cell (detailed)
% Source: https://github.com/fraserlove/nntikz/blob/3dc29cfda3bd18db2d1e73830f95b4d0985e050f/tikz/lstm.tex
% Copyright (c) 2024 Fraser Love. MIT licence: https://github.com/fraserlove/nntikz/blob/main/LICENSE
% The licence’s copyright and permission notice: https://www.labcanvas.com/tikz-library/credits.html
\documentclass{standalone}

\usepackage{tikz}

\usetikzlibrary{positioning,chains,shapes.geometric,fit,shapes,arrows.meta,calc}

\begin{document}

\begin{tikzpicture}[
    >=LaTeX, % Use default LaTeX arrows
    % Styles
    cell/.style={ % RNN cell
        rectangle,
        rounded corners=5mm,
        draw,
        very thick,
        minimum height=4cm,
        minimum width=6cm
    },
    cellc/.style={ % RNN cells in a chain
        cell,
        on chain,
        join
    },
    input/.style={ % Input or output node
        circle,
        minimum width=2.25em,
        draw,
        fill=gray!10,
        thick
    },
    hidden/.style={ % Hidden state
    },
    func/.style={ % Functions
        rectangle,
        draw,
        inner sep=2pt,
        minimum height=0.4cm
    },
    op/.style={ % Operators
        circle,
        draw,
        inner sep=-0.5pt,
        minimum height=0.4cm,
    },
    pointwise/.style={ % Pointwise operations and functions
        fill=gray!50,
    },
    arrow/.style={
        -latex,
        very thick
    },
    arrowc1/.style={ % Arrows with rounded corners
        arrow,
        rounded corners=.25cm
    },
    arrowc2/.style={ % Arrows with big rounded corners
        arrow,
        rounded corners=.5cm
    },
    backprop/.style={ % Backpropagation arrows
        arrow,
        dashed,
        gray
    }
]

    % LSTM cell
    \node [cell] at (0,0){};

    % Internal functions and operators
    \node [func] (sigf) at (-2,-0.75) {$\sigma$};
    \node [func] (sigi) at (-1.4,-0.75) {$\sigma$};
    \node [func] (tanhi) at (-0.5,-0.75) {$\tanh$};
    \node [func] (sigo) at (0.5,-0.75) {$\sigma$};
    \node [func, pointwise] (tanho) at (1.5,0.75) {$\tanh$};
    \node [op, pointwise] (mulf) at (-2,1.5) {$\times$};
    \node [op, pointwise] (add) at (-0.5,1.5) {+};
    \node [op, pointwise] (muli) at (-0.5,0) {$\times$};
    \node [op, pointwise] (mulo) at (1.5,0) {$\times$};

    % Inputs, outputs and hidden states
    \node[hidden] (ct-1) at (-4,1.5) {$\mathbf{c}_{t-1}$};
    \node[hidden] (ht-1) at (-4,-1.5) {$\mathbf{h}_{t-1}$};
    \node[input] (x) at (-2.5,-3) {$\mathbf{x}_{t}$};
    \node[hidden] (ct) at (4,1.5) {$\mathbf{c}_{t}$};
    \node[hidden] (ht) at (4,-1.5) {$\mathbf{h}_{t}$};
    \node[input] (y) at (2.5,3) {$\hat{\mathbf{y}}_{t}$};

    % Arrows from input and previous hidden states h_{t-1} and c_{t-1}
    \draw [arrowc1, -] (x) -- (x |- ht-1) -| (tanhi);
    \draw [arrowc2, -] (ht-1) -| (sigo);
    \draw [arrowc1, -] (ht-1 -| sigf)++(-0.5,0) -| (sigf);
    \draw [arrowc1, -] (ht-1 -| sigi)++(-0.5,0) -| (sigi);
    \draw [arrowc1, -] (ht-1 -| tanhi)++(-0.5,0) -| (tanhi);
    \draw [arrow] (ct-1) -- (mulf) -- (add) -- (ct);

    % Arrows for operation flow within LSTM gates
    \draw [arrow] (sigf) -- node[midway, left] {$\mathbf{f}_t$} (mulf);
    \draw [arrowc2] (sigi) |- node[midway, xshift=-1.5, yshift=1.5] {$\mathbf{i}_t$} (muli);
    \draw [arrow] (tanhi) -- node[midway, xshift=12, yshift=1.5] {$\tilde{\mathbf{c}}_t$} (muli);
    \draw [arrowc2] (sigo) |- node[midway, xshift=-1.5, yshift=1.5] {$\mathbf{o}_t$} (mulo);
    \draw [arrow] (muli) -- (add);
    \draw [arrowc1] (add -| tanho) ++(-0.5,0) -| (tanho);
    \draw [arrow] (tanho) -- (mulo);

    % Arrows to output and current hidden states h_t
    \draw [arrowc2] (mulo) |- (ht);
    \draw (ct -| y) ++(0,-0.1) coordinate (i1);
    \draw [arrowc2, -] (ht -| y) ++(-0.5,0) -| (i1);
    \draw [arrow] (i1) ++(0,0.2) -- (y);
\end{tikzpicture}

\end{document}
```

## Styles, two of them unused

*Lines 13–70 of the code.*

The picture options define the vocabulary. `cell` is the 6 × 4 cm rounded rectangle; `func` is the small box for σ and tanh; `op` is a circle with `inner sep=-0.5pt`, a negative padding that makes the circle hug its × or + tightly; `pointwise` only adds `fill=gray!50` and is combined with `func` or `op` for the pointwise operations. `arrowc1` and `arrowc2` are the `arrow` style with `rounded corners=.25cm` and `.5cm`, for small and large elbows.

Three styles do nothing here: `hidden` is empty (the state labels are plain text, but the style is a hook for restyling them all at once), and `cellc` (`on chain`, `join` from the chains library) and `backprop` are never used. They come from the collection’s shared set of styles for recurrent networks.

## The cell and its gates at fixed coordinates

*Lines 72–84 of the code.*

`\node [cell] at (0,0){};` comes first, so everything drawn later lies on top of it. The gates are then placed on a hand-made grid: the three σ gates and the input tanh on the row y = −0.75, at x = −2, −1.4, −0.5 and 0.5; the cell-state operations on y = 1.5 (`(mulf) at (-2,1.5)` and `(add) at (-0.5,1.5)`); the input multiplication and output multiplication on y = 0; and the output tanh at `(1.5,0.75)`. Reusing the same x values is what makes the vertical arrows straight: the forget gate at x = −2 is directly under its multiplication at x = −2.

## States and inputs around the cell

*Lines 86–92 of the code.*

The four state labels sit outside the 6 cm wide cell, at x = ±4: `(ct-1) at (-4,1.5)` on the cell-state row and `(ht-1) at (-4,-1.5)` on the hidden-state row, mirrored by c t and h t on the right. The input x t is a grey circle below the cell at (−2.5, −3) and the output ŷ t one above it at (2.5, 3), so that both lines cross the cell’s border.

## Feeding the gates: perpendicular coordinates and branches

*Lines 94–100 of the code.*

`(x |- ht-1)` is a perpendicular coordinate: the x of the input node and the y of the h t−1 label, so `\draw [arrowc1, -] (x) -- (x |- ht-1) -| (tanhi);` goes up from x t to the hidden-state row and then along it and up into the tanh. The `-` after the style name switches off the arrow tip that `arrowc1` would add, because these lines merge into the row rather than point at something.

The branches into the gates use a move-to: in `(ht-1 -| sigf)++(-0.5,0) -| (sigf)`, the second coordinate directly follows the first, so the path starts 0.5 cm left of the point under the gate without drawing anything there, then runs right and turns up into the gate with a rounded corner. The cell-state line is one path through two nodes, `(ct-1) -- (mulf) -- (add) -- (ct)`: each segment stops at the node borders, and the arrow tip appears only once, at the end of the path.

## Gate outputs and their labels

*Lines 102–109 of the code.*

Each gate output is an arrow with a label node on it. For the straight forget-gate arrow, `node[midway, left]` puts f t beside the middle of the line. The input and output gates use `|-` paths (up, then right into the multiplication); on such a path `midway` is the corner, and `xshift=-1.5, yshift=1.5` nudge the label off it. These shifts have no unit, so they are read as points; the c̃ t label is pushed 12pt to the right with `xshift=12`.

Line 108 branches the new cell state into the output tanh with the same move-to trick as before, starting from `(add -| tanho)`, the point on the cell-state row above the tanh.

## The outputs, and a gap where two lines cross

*Lines 111–115 of the code.*

`\draw [arrowc2] (mulo) |- (ht);` takes the output down and right to h t. The branch up to ŷ t has to cross the cell-state line without seeming to join it. Line 113 draws nothing: it only defines the coordinate `(i1)` 0.1 cm below the crossing point. Line 114 runs from the hidden-state row up to `(i1)`, and line 115 restarts 0.2 cm higher, `(i1) ++(0,0.2)`, and continues to ŷ t. The two short gaps on either side of the cell-state line read as “passes over it”. The second variation compares this with two other ways to draw a crossing.

## Techniques used

- [Perpendicular coordinates (a |- b)](https://www.labcanvas.com/tikz/coordinates): `(x |- ht-1)` and `(add -| tanho)` take the x of one point and the y of another.
- [Elbow lines with -| and |-](https://www.labcanvas.com/tikz/lines): every connection turns at right angles, rounded by `rounded corners` in the arrow styles.
- [Labels on edges](https://www.labcanvas.com/tikz/nodes): `node[midway, left]` on a line, and what `midway` means on a `|-` path.
- [Arrow tips on and off](https://www.labcanvas.com/tikz/arrows): a style that adds `-latex`, switched off with `-` where a line merges into another.
- [Neural network diagrams](https://www.labcanvas.com/tikz/neural-networks): recurrent cells, gates and how they are usually drawn.

## Variations

Our own code, CC0 1.0, each typeset with TeX Live 2026 in a standalone document with the preamble lines shown.

### The LSTM unrolled over three time steps

The detailed cell is one time step. To show the recurrence, draw each step as a compact box and pass the cell state and hidden state from box to box. One `\foreach` with pairs, `\k/\t in {0/t-1, 1/t, 2/t+1}`, places the cells, inputs and outputs and writes the time index into the labels; a second one draws the two state arrows between neighbours, shifted up and down from the box centres with `([yshift=4mm]c\k.east)`.

![Three rounded LSTM boxes in a row for time steps t−1, t and t+1, each with an input x below and an output y hat above; arrows labelled c and h connect neighbouring boxes, and dashed arrows enter the first box and leave the last.](https://www.labcanvas.com/tikz-snippets/fig-lstm-cell-detailed/unrolled.webp)

*an LSTM unrolled over time steps t−1, t and t+1, with the cell state above and the hidden state below*

Preamble: `\usepackage{tikz}`, `\usetikzlibrary{arrows.meta}`

```latex
\begin{tikzpicture}[
    >={Latex[length=2mm]}, thick,
    cell/.style={draw, rounded corners=3mm, fill=gray!8, minimum width=1.8cm, minimum height=1.4cm},
    io/.style={circle, draw, fill=gray!10, minimum size=9mm, inner sep=0pt},
  ]
  % one cell per time step
  \foreach \k/\t in {0/t-1, 1/t, 2/t+1} {
    \node[cell] (c\k) at (3*\k, 0) {LSTM};
    \node[io] (x\k) at (3*\k, -1.8) {$\mathbf{x}_{\t}$};
    \node[io] (y\k) at (3*\k, 1.8) {$\hat{\mathbf{y}}_{\t}$};
    \draw[->] (x\k) -- (c\k);
    \draw[->] (c\k) -- (y\k);
  }
  % the cell state (top) and the hidden state (bottom) pass from cell to cell
  \foreach \k/\n/\t in {0/1/t-1, 1/2/t} {
    \draw[->] ([yshift=4mm]c\k.east) -- node[above, font=\small] {$\mathbf{c}_{\t}$} ([yshift=4mm]c\n.west);
    \draw[->] ([yshift=-4mm]c\k.east) -- node[below, font=\small] {$\mathbf{h}_{\t}$} ([yshift=-4mm]c\n.west);
  }
  % the states coming in and going out
  \foreach \dy in {4mm, -4mm} {
    \draw[->, dashed] ([xshift=-1.1cm, yshift=\dy]c0.west) -- ([yshift=\dy]c0.west);
    \draw[->, dashed] ([yshift=\dy]c2.east) -- ([xshift=1.1cm, yshift=\dy]c2.east);
  }
\end{tikzpicture}
```

### Three ways to draw a line crossing

The figure marks the crossing with a gap computed by hand. Two alternatives need no extra coordinates: a `preaction` that first draws the crossing line in white and much wider, which cuts a clean gap into the line underneath, and a small semicircular hop made with `arc`. The halo works for any angle and curve; the hop is the classic circuit-diagram convention.

![Three copies of a horizontal arrow crossed by a vertical arrow: in the first the vertical line stops just before and restarts just after the horizontal one, in the second the horizontal line has a white gap under the vertical line, in the third the vertical line makes a small semicircular hop around the crossing; labelled gap, halo and hop.](https://www.labcanvas.com/tikz-snippets/fig-lstm-cell-detailed/crossings.webp)

*a crossing drawn three ways: a gap by coordinates, a white halo with preaction, and an arc hop*

Preamble: `\usepackage{tikz}`, `\usetikzlibrary{arrows.meta}`

```latex
\begin{tikzpicture}[very thick, >={Latex[length=2.5mm]}]
  % (a) a gap, as in the LSTM figure: stop just below the line, restart above it
  \draw[->] (0,0) -- (2,0);
  \draw (1,-1) -- (1,-0.12);
  \draw[->] (1,0.12) -- (1,1);
  \node at (1,-1.4) {gap};
  % (b) a white halo under the crossing line: a preaction draws it first
  \begin{scope}[xshift=3cm]
    \draw[->] (0,0) -- (2,0);
    \draw[->, preaction={draw, white, line width=7pt}] (1,-1) -- (1,1);
    \node at (1,-1.4) {halo};
  \end{scope}
  % (c) a small hop over the line with an arc
  \begin{scope}[xshift=6cm]
    \draw[->] (0,0) -- (2,0);
    \draw[->] (1,-1) -- (1,-0.15) arc[start angle=-90, end angle=90, radius=0.15] -- (1,1);
    \node at (1,-1.4) {hop};
  \end{scope}
\end{tikzpicture}
```

## Frequently asked questions

### How do I draw an LSTM cell in TikZ?

Draw a large rounded rectangle for the cell, place the gates (σ and tanh boxes, × and + circles) at fixed coordinates inside it, and connect them with -| and |- paths. Perpendicular coordinates such as (x |- h) let a line join the hidden-state row exactly. The code on this page is a complete example to start from.

### How do I make a line pass over another line in TikZ?

Either leave a small gap by ending the first part just before the crossing and restarting just after it, as this figure does, or draw the upper line with preaction={draw, white, line width=6pt} so that a white halo cuts the lower line, or make a small hop with an arc.

### Why is there a - after arrowc1 in some lines?

The arrowc1 style adds an arrow tip. A later - in the same options sets the arrow specification to “no tips”, so those lines merge into the hidden-state row without an arrowhead.

## References

- [fraserlove/nntikz at commit 3dc29cf: tikz/lstm.tex (MIT licence)](https://github.com/fraserlove/nntikz/blob/3dc29cfda3bd18db2d1e73830f95b4d0985e050f/tikz/lstm.tex)
- [PGF/TikZ manual, §13.3.1 Intersections of Perpendicular Lines (|- and -|)](https://tikz.dev/tikz-coordinates#sec-13.3.1)
- [PGF/TikZ manual, §14.1 The Move-To Operation](https://tikz.dev/tikz-paths#sec-14.1)
- [PGF/TikZ manual, §14.2.2 Horizontal and Vertical Lines](https://tikz.dev/tikz-paths#sec-14.2.2)
- [PGF/TikZ manual, §14.5 Rounding Corners](https://tikz.dev/tikz-paths#sec-14.5)
- [PGF/TikZ manual, §14.7 The Arc Operation](https://tikz.dev/tikz-paths#sec-14.7)
- [PGF/TikZ manual, §15.10 Doing Multiple Actions on a Path (preaction)](https://tikz.dev/tikz-actions#sec-15.10)
- [PGF/TikZ manual, §16.2 Where and When Arrow Tips Are Placed](https://tikz.dev/tikz-arrows#sec-16.2)
- [PGF/TikZ manual, §17.8 Placing Nodes on a Line or Curve Explicitly (pos, midway)](https://tikz.dev/tikz-shapes#sec-17.8)
- [PGF/TikZ manual, §17.11 Connecting Nodes: Using Nodes as Coordinates](https://tikz.dev/tikz-shapes#sec-17.11)
- [PGF/TikZ manual, §94.1.1 Commands (\pgfmathsetlength: a value without unit is taken in points)](https://tikz.dev/math-parsing#sec-94.1.1)

## TikZ guides for these techniques

- [TikZ neural network diagrams: perceptron, MLP, CNN, LSTM and transformer](https://www.labcanvas.com/tikz/neural-networks): Neural network diagrams in LaTeX with TikZ: perceptron, MLP from \foreach loops, labelled weights, CNN feature maps, RNN, LSTM, autoencoder, transformer.
- [TikZ coordinates, paths, \foreach, scopes and styles](https://www.labcanvas.com/tikz/coordinates): TikZ coordinates (polar, relative ++, named, calc, intersections), paths and controls, \foreach loops, scopes, \tikzset styles, decorations and grids.
- [TikZ lines: dashed, dotted, thickness and line width](https://www.labcanvas.com/tikz/lines): TikZ lines: dashed and dotted styles with their exact patterns, ultra thin to ultra thick widths, dash pattern, caps and joins, double lines, grids.
- [TikZ arrows: arrow tips, sizes, curves and labels](https://www.labcanvas.com/tikz/arrows): All TikZ arrow tips in one rendered table, arrows.meta sizing (length, width, scale, open, round), and curved, labelled, thick, block and squiggly arrows.
- [TikZ nodes: shapes, anchors, positioning and labels](https://www.labcanvas.com/tikz/nodes): How TikZ nodes work: shapes, size (inner sep, minimum size), all anchors, the positioning library (right=of, node distance), labels, pins and scaling.
