LabCanvas

LSTM cell in TikZ: how the gate diagram is drawn

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 ht 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.

Contents

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.
LSTM cell (detailed), typeset with TeX Live 2026 (pdfLaTeX) from the code below, exactly as it is in fraserlove/nntikz.
Copyright (c) 2024 Fraser Love · MIT licence (full text). The licence asks that this notice stay with the code: keep the first lines when you reuse it.
% 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 ct and ht on the right. The input xt 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 ht−1 label, so \draw [arrowc1, -] (x) -- (x |- ht-1) -| (tanhi); goes up from xt 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 ft 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 ht. 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

Variations

Our own code, written for this page and typeset with TeX Live 2026 like the figure above. Each one is a complete example: copy it, or open it in the TikZ editor and change it. This code is CC0 1.0, free to use without conditions.

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.
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}
\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}
Open in the TikZ editor
Full document
A complete file you can compile as it is.
\documentclass[border=4pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows.meta}
\begin{document}
\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}
\end{document}

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.
a crossing drawn three ways: a gap by coordinates, a white halo with preaction, and an arc hop
Preamble: \usepackage{tikz} \usetikzlibrary{arrows.meta}
\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}
Open in the TikZ editor
Full document
A complete file you can compile as it is.
\documentclass[border=4pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows.meta}
\begin{document}
\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}
\end{document}

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

  1. fraserlove/nntikz at commit 3dc29cf: tikz/lstm.tex (MIT licence)github.com
  2. PGF/TikZ manual, §13.3.1 Intersections of Perpendicular Lines (|- and -|)tikz.dev
  3. PGF/TikZ manual, §14.1 The Move-To Operationtikz.dev
  4. PGF/TikZ manual, §14.2.2 Horizontal and Vertical Linestikz.dev
  5. PGF/TikZ manual, §14.5 Rounding Cornerstikz.dev
  6. PGF/TikZ manual, §14.7 The Arc Operationtikz.dev
  7. PGF/TikZ manual, §15.10 Doing Multiple Actions on a Path (preaction)tikz.dev
  8. PGF/TikZ manual, §16.2 Where and When Arrow Tips Are Placedtikz.dev
  9. PGF/TikZ manual, §17.8 Placing Nodes on a Line or Curve Explicitly (pos, midway)tikz.dev
  10. PGF/TikZ manual, §17.11 Connecting Nodes: Using Nodes as Coordinatestikz.dev
  11. PGF/TikZ manual, §94.1.1 Commands (\pgfmathsetlength: a value without unit is taken in points)tikz.dev

The walkthrough was checked against these sources and the pictures are the code typeset with TeX Live 2026. The original figure is © Fraser Love, under the MIT licence; the variations on this page are ours, CC0 1.0.

TikZ guides for these techniques

More figures explained

Change this figure in your browserOpen the figure or any variation in the free TikZ editor: it is typeset with TeX Live as you type, with errors pointed at their line.

Open the figure in the TikZ editor