Single neuron (perceptron) in TikZ, explained
Quick answer
This figure draws one artificial neuron: inputs x0 to xn with weights w0 to wn 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.
Contents
The figure and its code

% 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 x0, x1 and x2 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 xn, 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 x2 to xn 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 x0 (0) and xn (−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 w1 and w2, 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 the old keys measure between centres; the positioning library measures between borders.
- Labels on arrows
node[midway, fill=white]after the target puts a masked label on the line. - Loops with \foreach inputs named and labelled from one loop variable.
- Fit nodes a
fitnode only shows when it is drawn or filled.
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.
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.

\usepackage{tikz} \usetikzlibrary{positioning, calc, arrows.meta}\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}
Full document
\documentclass[border=4pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning, calc, arrows.meta}
\begin{document}
\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}
\end{document}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.

\usepackage{tikz} \usetikzlibrary{positioning}\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}
Full document
\documentclass[border=4pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}
\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}
\end{document}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)github.com
- PGF/TikZ manual, §13.5.3 The Syntax of Partway Modifierstikz.dev
- PGF/TikZ manual, §17.5.3 Advanced Placement Options (positioning, node distance)tikz.dev
- PGF/TikZ manual, §17.8 Placing Nodes on a Line or Curve Explicitly (pos, midway)tikz.dev
- PGF/TikZ manual, §23.2 Specifying a Uniform Opacity (fill opacity, text opacity)tikz.dev
- PGF/TikZ manual, §54 Fitting Librarytikz.dev
- PGF/TikZ manual, §88 Repeating Things: The Foreach Statementtikz.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.
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