How to Create Beautiful Atomic Structures with TikZ and LaTeX
In this article, I’m going to show you how to draw atomic nuclei and visualize protons and neutrons using the powerful TikZ package in LaTeX. Let’s get started!
It All Starts with Protons and Neutrons
We’ll begin by defining the protons and neutrons using the following commands:
pics/proton/.style={code={\shade[ball color=red] circle (3pt);}},
pics/neutron/.style={code={\shade[ball color=white] circle (3pt);}},
These commands allow us to draw small red and white balls whenever we call \pic (x,y) {proton};
. Next, we’ll add this code to generate the nucleus:
\pgfmathsetseed{#1+1}
\foreach \A/\R in {8/0.2, 5/0.13, 1/0}{
\pgfmathsetmacro{\S}{360/\A}
\foreach \B in {0,\S,...,360}{
\pgfmathrandomitem{\C}{nucleon}
\pic at ($(\B+2*\A+5*rnd:\R)$) {\C};
}
}
This defines another pic
command called nucleussmall
. In this macro, we declare a random list using \pgfmathdeclarerandomlist
which contains two protons and three neutrons. When called, this list will randomly generate protons and neutrons with probabilities of 2/5 and 3/5, respectively.
•Random Seed Initialization: \pgfmathsetseed{#1+1}
This sets the random number generator’s seed based on the parameter #1
to ensure different arrangements for each nucleus.
•Ring Creation: \foreach \A/\R in {8/0.2, 5/0.13, 1/0}
This creates three rings of nucleons with decreasing radii. \A
defines the number of nucleons per ring, and \R
defines the ring radius.
•Angle Calculation: \pgfmathsetmacro{\S}{360/\A}
Calculates the angular spacing between nucleons in each ring.
•Random Nucleon Placement:
\pic at ($(\B+2*\A+5*rnd:\R)$) {\C};
This places a nucleon at a position determined by the current angle, random offsets, and the ring’s radius using the TikZ calc
library’s coordinate calculation syntax ($...$)
.
That’s essentially the core logic. We use the same approach to define larger nuclei. Here’s the complete document:
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\tikzset{
pics/proton/.style={code={\shade[ball color=red] circle (3pt);}},
pics/neutron/.style={code={\shade[ball color=white] circle (3pt);}},
pics/nucleussmall/.style={code={%
\pgfmathdeclarerandomlist{nucleon}{{proton}{proton}{neutron}{neutron}{neutron}}
\pgfmathsetseed{#1+1}
\foreach \A/\R in {8/0.2, 5/0.13, 1/0}{
\pgfmathsetmacro{\S}{360/\A}
\foreach \B in {0,\S,...,360}{
\pgfmathrandomitem{\C}{nucleon}
\pic at ($(\B+2*\A+5*rnd:\R)$) {\C};
}
}
}},
pics/nucleusbig/.style={code={%
\pgfmathdeclarerandomlist{nucleon}{{proton}{proton}{neutron}{neutron}{neutron}}
\pgfmathsetseed{#1+1}
\foreach \A/\R in {24/0.4, 24/0.3, 24/0.2, 13/0.35, 11/0.27, 6/0.15, 1/0}{
\pgfmathsetmacro{\S}{360/\A}
\foreach \B in {0,\S,...,360}{
\pgfmathrandomitem{\C}{nucleon}
\pic at ($(\B+2*\A+5*rnd:\R)$) {\C};
}
}
}},
pics/nucleusbiggest/.style={code={%
\pgfmathdeclarerandomlist{nucleon}{{proton}{proton}{neutron}{neutron}{neutron}}
\pgfmathsetseed{#1+1}
\foreach \A/\R in {24/0.5, 24/0.4, 24/0.3, 24/0.2, 13/0.47, 15/0.44, 13/0.37, 11/0.27, 6/0.15, 1/0}{
\pgfmathsetmacro{\S}{360/\A}
\foreach \B in {0,\S,...,360}{
\pgfmathrandomitem{\C}{nucleon}
\pic at ($(\B+2*\A+5*rnd:\R)$) {\C};
}
}
}},
}
\pic at (0,0) {nucleussmall};
\pic at (2,0) {nucleusbig=1};
\pic at (4,0) {nucleusbiggest=1};
\end{tikzpicture}
\end{document}
