← All posts
tikz, circuitikz, electronics, haskell, hakyll, rendering

Schematics that compile — circuit diagrams in pure TikZ

A while back I rebuilt this blog’s TikZ pipeline so diagrams compile from source at build time — lualatex renders a fenced tikzpicture block, dvisvgm turns it into inline SVG, and the figure in the Markdown is the figure on the page. That worked for field plots and pgfplots charts, but one obvious thing was still missing: circuit diagrams. This post adds them, and shows them off.

The one-line backend change

Drawing schematics by hand in raw TikZ — battery symbols, resistor zig-zags, op-amp triangles — is miserable. circuitikz is the package that does it properly: components become path operations, so a resistor between two points is just to[R=$R$]1. (TikZ also ships a native circuit library, documented in the manual2.) Teaching the pipeline to use it took two small edits to the Blog.TikZ module:

-- 1. load the package in the LaTeX preamble each diagram is wrapped in
"\\usepackage[american]{circuitikz}"

-- 2. let a block open its own circuitikz environment (used verbatim,
--    instead of being wrapped in a bare tikzpicture)
let opensOwnPicture =
      any (`isInfixOf` tikzCode) ["\\begin{tikzpicture}", "\\begin{circuitikz}"]

That’s it. Because circuitikz builds on TikZ and the renderer already shells out to lualatex + dvisvgm, no new tool was needed — only the package (already present in CI’s texlive-latex-extra). The four diagrams below are each compiled on every build from the circuitikz source shown beneath them.

1 · An RC low-pass filter

The canonical first filter: a resistor in series, a capacitor to ground, output across the capacitor. At low frequencies the capacitor is a high impedance and the output follows the input; at high frequencies it shorts the signal to ground3.

Its transfer function and \(-3\,\text{dB}\) corner frequency are

\[H(j\omega) = \frac{1}{1 + j\omega RC}, \qquad f_c = \frac{1}{2\pi RC}.\]

2 · A series RLC circuit

Add an inductor and the circuit gains a resonance: inductive and capacitive reactances cancel at one frequency, leaving only \(R\). It is the textbook second-order system3.

It resonates at \(\omega_0\) with quality factor \(Q\):

\[\omega_0 = \frac{1}{\sqrt{LC}}, \qquad Q = \frac{1}{R}\sqrt{\frac{L}{C}}.\]

3 · An inverting op-amp amplifier

The active example. The op-amp drives its inverting input to match the grounded non-inverting input — the virtual ground — so the current through \(R_\text{in}\) must flow on through \(R_f\), fixing the gain by a ratio of two resistors4.

\[\frac{V_\text{out}}{V_\text{in}} = -\frac{R_f}{R_\text{in}}.\]

4 · A full-wave bridge rectifier

The showcase. Four diodes in a diamond steer either polarity of the AC input the same way through the load: whichever way \(v_\text{ac}\) swings, two diodes conduct and the load always sees current top-to-bottom, so the output never reverses4.

After smoothing, the average output of an ideal full-wave rectifier is

\[V_\text{dc} = \frac{2\,V_\text{peak}}{\pi}.\]

Why build-time matters

None of these are images. Each is text — a dozen lines of circuitikz — that the build compiles into vector SVG. That means they version-control as diffs, restyle with the page, scale without blurring, and can never drift out of sync with a caption, because there is no binary to forget to re-export. The same pipeline now covers field plots, chemical structures, 3D vector fields, and schematics; the only thing each new domain needs is the right LaTeX package in the preamble.

References

1.
The CircuiTikZ Project. CircuiTikZ: Draw Electrical Networks with TikZ. https://github.com/circuitikz/circuitikz.
2.
Tantau, T. The TikZ and PGF Manual: Circuit Libraries. https://tikz.dev/library-circuits.
3.
Nilsson, J. W.; Riedel, S. A. Electric Circuits, 11th ed.; Pearson, 2019.
4.
Horowitz, P.; Hill, W. The Art of Electronics, 3rd ed.; Cambridge University Press, 2015.
← All posts