# Single neuron (perceptron) in TikZ, explained

> A single neuron (perceptron) in TikZ, explained: inputs from \foreach, weight labels on arrows, the old right of= syntax, and two variations to copy.

URL: https://www.labcanvas.com/tikz/examples/machine-learning/single-neuron

Updated 2026-11-22. Original figure: Single neuron, © Fraser Love, MIT licence (licence text: https://www.labcanvas.com/tikz-library/credits.html#nntikz), from https://github.com/fraserlove/nntikz/blob/3dc29cfda3bd18db2d1e73830f95b4d0985e050f/tikz/neuron.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 artificial neuron: inputs x 0 to x n with weights w 0 to w n on their arrows, the weighted sum z, and the output ŷ = σ(z). The inputs come from a `\foreach` loop, each weight is a white-filled label node sitting on its arrow, and z and ŷ are placed with the older `right of=` and `above of=` keys, which measure between node centres. The page explains why that differs from `right=of`, and the variations add a bias and a separate activation.

## The figure and its code

![A single neuron: five grey input circles x 0, x 1, x 2, dots and x n on the left, arrows labelled w 0, w 1, w 2 and w n converging on a circle z, the formula z = w transpose x above it, and an arrow labelled sigma of z to the output circle y hat.](https://www.labcanvas.com/tikz-snippets/fig-single-neuron/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/single-neuron

```latex
% Single neuron
% Source: https://github.com/fraserlove/nntikz/blob/3dc29cfda3bd18db2d1e73830f95b4d0985e050f/tikz/neuron.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
    node/.style={ % Input or output node
        circle,
        minimum width=2.25em,
        draw,
        fill=gray!10,
        thick
    },
    cell/.style={
        rectangle,
        rounded corners=2mm,
        minimum height=2.5em,
        minimum width=2.5em,
        draw,
        thick
    },
    arrow/.style={
        -latex,
        thick
    },
    backprop/.style={ % Backpropagation arrows
        arrow,
        dashed,
        gray
    }
]

    % The first two x nodes are spaced out more
    \foreach \x in {0,1,2}
        \node[node] (x\x) at (0,-\x*1.25) {$x_{\x}$};
    \node[node] (xm) at (0,-4*1.25) {$x_{n}$};

    % The dots are placed between x2 and xm
    %\node at (0,{-3.4}) {$\vdots$};
    \path (x2) -- (xm) node[pos=0.35, scale=2] {\vdots};

    % The xm node is placed further down to make space for the dots

    % Mid Node
    \node[node] (z) at (2.5,-2.5) {$z$};

    % Output Node
    \node[node, right of=z, node distance=2.5cm] (yhat) {$\hat{y}$};

      % Arrows from input nodes to mid node with labels above
        \draw[arrow] (x0) -- (z) node[midway, fill=white, inner sep=1.5pt] {$w_0$};
        \foreach \x in {1,...,2}
            \draw[arrow] (x\x) -- (z) node[midway, fill=white, inner sep=1.5pt] {$w_{\x}$};
        \draw[arrow] (xm) -- (z) node[midway, fill=white, inner sep=1.5pt] {$w_{n}$};

    % Arrow from mid node to output node with sigma(z) over it
    \draw[arrow] (z) -- (yhat) node[midway, fill=white, inner sep=1.5pt] {$\sigma(z)$};

    % Equations.
    \node[above of=z, node distance=1.2cm] {$z = \mathbf{w}^\top \mathbf{x}$};

    \node[rectangle, rounded corners=2mm, minimum height=2.5em, minimum width=2.5em, fit=(x0) (xm)] (xbox) {};
\end{tikzpicture}

\end{document}
```

## The shared styles

*Lines 13–40 of the code.*

The picture opens with the same four styles as this collection’s network figures: `node` for the grey circles (at least `minimum width=2.25em`, so labelled and empty neurons match), `cell`, `arrow` with the old `-latex` tip, and `backprop`. Only `node` and `arrow` are used here; the box at the end writes its options out by hand instead of using `cell`.

## Inputs and the dots

*Lines 42–51 of the code.*

`\foreach \x in {0,1,2}` places x 0, x 1 and x 2 at `(0,-\x*1.25)`, 1.25 cm apart, and names them `x0`, `x1`, `x2`; the label `$x_{\x}$` uses the same loop variable. The last input, named `xm` but labelled x n, goes at `(0,-4*1.25)`, skipping one slot for the dots, as the comment on line 51 says. The comment on line 42 no longer matches the code (the loop spaces the first inputs evenly), and line 48 keeps a commented-out alternative that placed the dots at a fixed y.

The dots in use are on an invisible path: `\path (x2) -- (xm) node[pos=0.35, scale=2] {\vdots};` builds the line from x 2 to x n without drawing it and puts a doubled `\vdots` 35% of the way along. The value is tuned by eye, because `\vdots` has empty space above its dots.

## z and ŷ with the old right of= syntax

*Lines 53–57 of the code.*

z sits at `(2.5,-2.5)`: y = −2.5 is halfway between x 0 (0) and x n (−5), so the arrows fan in symmetrically. The output is placed with `right of=z, node distance=2.5cm`. This is the older form, with “of” before the equals sign: it needs no library and puts the *centres* one node distance apart, here 2.5 cm. The positioning library’s `right=2.5cm of z` would instead leave 2.5 cm between the *borders*. For two circles of the same size the difference is only their diameter, but with boxes of different widths `right of=` lets them overlap, as the second variation shows. The old keys still work, but they are deprecated.

## Weight labels on the arrows

*Lines 59–66 of the code.*

Each weight is a node written after the arrow’s target: `\draw[arrow] (x0) -- (z) node[midway, fill=white, inner sep=1.5pt] {$w_0$};`. A node at the end of a path is placed on the segment just drawn, and `midway` puts it halfway along. `fill=white` paints over the line behind the label, so the weight reads cleanly on top of its arrow, and `inner sep=1.5pt` keeps that white patch small. A loop does w 1 and w 2, and the same technique writes σ(z) on the arrow from z to ŷ.

## The equation, and a box that is never drawn

*Lines 68–71 of the code.*

The equation is a node `above of=z, node distance=1.2cm`: its centre is 1.2 cm above z’s centre. The last line is a leftover from the fully connected network figure, where layers sit in dashed boxes: `\node[rectangle, rounded corners=2mm, …, fit=(x0) (xm)] (xbox) {};` computes a box around the inputs but has no `draw` option, so nothing appears. It only adds a few points of empty margin to the picture’s bounding box. Add `draw, dashed` to show it, or delete the line.

## Techniques used

- [right of= versus right=of](https://www.labcanvas.com/tikz/nodes): the old keys measure between centres; the positioning library measures between borders.
- [Labels on arrows](https://www.labcanvas.com/tikz/arrows): `node[midway, fill=white]` after the target puts a masked label on the line.
- [Loops with \foreach](https://www.labcanvas.com/tikz/neural-networks): inputs named and labelled from one loop variable.
- [Fit nodes](https://www.labcanvas.com/tikz/diagrams): a `fit` node only shows when it is drawn or filled.

## Variations

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

### A perceptron with a bias and a separate activation

The usual textbook form: the bias as an extra input fixed at 1 with weight b, a Σ node for the weighted sum and a box for the activation φ. Everything is placed with the positioning library: the inputs with `below=of`, and the Σ node `right=24mm of mid`, where `mid` is the calc midpoint between the first and last input, so the sum stays centred however many inputs there are.

![A perceptron: an orange input 1 and grey inputs x 1, x 2, dots and x n on the left, arrows labelled b, w 1, w 2 and w n into a blue circle Sigma, an arrow labelled z into a green box phi, an arrow to y hat, and the formula y hat = phi of the sum of w i x i plus b above.](https://www.labcanvas.com/tikz-snippets/fig-single-neuron/perceptron-bias.webp)

*a perceptron with a bias input, weights on the arrows, a sum node and an activation box*

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

```latex
\begin{tikzpicture}[
    >={Latex[length=2mm]}, thick, node distance=6mm,
    neuron/.style={circle, draw, fill=gray!10, minimum size=9mm, inner sep=0pt},
    w/.style={midway, fill=white, inner sep=1.5pt, font=\small},
  ]
  % inputs, with the bias as an input that is always 1
  \node[neuron, fill=orange!20] (b) {$1$};
  \node[neuron, below=of b] (x1) {$x_1$};
  \node[neuron, below=of x1] (x2) {$x_2$};
  \node[below=1mm of x2] (dots) {$\vdots$};
  \node[neuron, below=1mm of dots] (xn) {$x_n$};
  % the sum at mid-height, then the activation
  \coordinate (mid) at ($(b)!0.5!(xn)$);
  \node[neuron, fill=blue!12, minimum size=12mm, right=24mm of mid] (sum) {$\Sigma$};
  \node[draw, rounded corners=2pt, fill=green!15, minimum size=9mm, right=12mm of sum] (act) {$\varphi$};
  \node[right=10mm of act] (out) {$\hat{y}$};
  \draw[->] (b) -- (sum) node[w] {$b$};
  \foreach \nm/\k in {x1/1, x2/2, xn/n}
    \draw[->] (\nm) -- (sum) node[w] {$w_{\k}$};
  \draw[->] (sum) -- node[above, font=\small] {$z$} (act);
  \draw[->] (act) -- (out);
  \node[above=5mm of act, font=\small] {$\hat{y} = \varphi\bigl(\sum_i w_i x_i + b\bigr)$};
\end{tikzpicture}
```

### right of= and right=of side by side

The same three nodes placed both ways with `node distance=1.2cm`. With the old `right of=` the centres are 1.2 cm apart, so the wide middle box overlaps its neighbours; with `right=of` from the positioning library there are 1.2 cm between the borders. The boxes are slightly transparent (`fill opacity=0.75`) so that the overlap shows.

![Two rows of three grey boxes z, sigma of z = 1 over 1 plus e to the minus z, and y hat: in the top row, labelled right of=, the wide middle box overlaps both neighbours; in the bottom row, labelled right=of, the boxes are evenly spaced with gaps between them.](https://www.labcanvas.com/tikz-snippets/fig-single-neuron/right-of-vs-right-eq-of.webp)

*right of= (centres 1.2 cm apart, boxes overlap) above right=of (borders 1.2 cm apart)*

Preamble: `\usepackage{tikz}`, `\usetikzlibrary{positioning}`

```latex
\begin{tikzpicture}[
    node distance=1.2cm,
    box/.style={draw, thick, rounded corners=2pt, fill=gray!15, fill opacity=0.75,
                text opacity=1, minimum height=8mm},
  ]
  % right of= (the old syntax): the CENTRES are one node distance apart
  \node[box] (a1) {$z$};
  \node[box, right of=a1] (b1) {$\sigma(z) = 1/(1+e^{-z})$};
  \node[box, right of=b1] (c1) {$\hat{y}$};
  \node[anchor=east, font=\small] at ([xshift=-4mm]a1.west) {\texttt{right of=}};
  % right=of (positioning library): the BORDERS are one node distance apart
  \node[box, below=1.4cm of a1] (a2) {$z$};
  \node[box, right=of a2] (b2) {$\sigma(z) = 1/(1+e^{-z})$};
  \node[box, right=of b2] (c2) {$\hat{y}$};
  \node[anchor=east, font=\small] at ([xshift=-4mm]a2.west) {\texttt{right=of}};
\end{tikzpicture}
```

## Frequently asked questions

### How do I draw a perceptron in TikZ?

Place the input nodes in a column, a sum node to their right, and draw an arrow from each input to the sum with a weight label: \draw[->] (x1) -- (sum) node[midway, fill=white] {$w_1$};. Add an activation box and the output to the right. The perceptron variation on this page is a complete example.

### What is the difference between right of= and right=of in TikZ?

right of=a is the old syntax: no library is needed and the new node’s centre is one node distance to the right of a’s centre. right=of a needs \usetikzlibrary{positioning} and leaves one node distance between the two borders, which keeps gaps even when nodes have different sizes.

### How do I put a label on an arrow without the line running through it?

Give the label node fill=white (or the background colour): \draw[->] (a) -- (b) node[midway, fill=white, inner sep=1pt] {$w$};. The white fill covers the line behind the text.

## References

- [fraserlove/nntikz at commit 3dc29cf: tikz/neuron.tex (MIT licence)](https://github.com/fraserlove/nntikz/blob/3dc29cfda3bd18db2d1e73830f95b4d0985e050f/tikz/neuron.tex)
- [PGF/TikZ manual, §13.5.3 The Syntax of Partway Modifiers](https://tikz.dev/tikz-coordinates#sec-13.5.3)
- [PGF/TikZ manual, §17.5.3 Advanced Placement Options (positioning, node distance)](https://tikz.dev/tikz-shapes#sec-17.5.3)
- [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, §23.2 Specifying a Uniform Opacity (fill opacity, text opacity)](https://tikz.dev/tikz-transparency#sec-23.2)
- [PGF/TikZ manual, §54 Fitting Library](https://tikz.dev/library-fit)
- [PGF/TikZ manual, §88 Repeating Things: The Foreach Statement](https://tikz.dev/pgffor)

## 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 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.
- [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 diagrams: block, cycle, flowchart, tree and more](https://www.labcanvas.com/tikz/diagrams): How to draw diagrams in TikZ: block, cycle, flowchart, tree, graph, timeline, matrix, commutative and circuit diagrams, each with compiled code.
