[Tex/LaTex] Electron Configuration Diagram

chemistrytikz-pgf

I have seen some other postings about related electron configurations, but nothing that matched what I was looking for. I am pretty comfortable with a lot of LaTeX, except when it comes to stuff like using TikZ which I'm sure is what I'm needing here.

I'm wanting to create the following image:

electron configuration diagram

I do not always need to create both the "2s" and "2p" sections at the same time. Ideally wrapping it in a function since I will be needing to create many of these images.

\electron{2s}{1}{{1,-1}}

This would be the {label}{number of lines}{{array of electrons}} – where in the array (which I'm not sure how easy that is to implement), I can provide the arrows needed. 1 is an up arrow, 0 is no arrow, -1 is a down arrow (just as an example).

The 2p would be drawn as:

\electron{2p}{3}{{1,0},{1,0},{1,0}}

Then I could string them together if wanted, defining the space between them:

\electron{}{}{}$\qquad$\electron{}{}{}

Or something like that

Since I don't know the actual TikZ to use, I thought I'd at least provide pseudocode/markup to show what I would think would happen:

\newcommand{\electron}[3]{
    \newcounter{lines}
    \forloop{lines}{0}{\value{lines} < #2}{
        draw line
        if array[\value{lines}][0] = -1; draw down line %first entry
        if array[\value{lines}][0] = 1; draw up line %first entry
        if array[\value{lines}][1] = -1; draw down line %second entry
        if array[\value{lines}][1] = 1; draw up line %second entry
    }
    %place text of #1 cenetered under created lines
 }

Best Answer

I don't think you need TikZ for this. Here is an implementation using just tabulars and underlines:

\documentclass{standalone}
\usepackage{amssymb} % for harpoons
\newcommand{\electron}[2]{{%
  \def\+{\underline{\upharpoonleft}}%
  \def\-{\underline{\downharpoonright}}%
  \def\0{\underline{\phantom{\downharpoonright}}}%
  \setlength\tabcolsep{0pt}% remove extra horizontal space from tabular
  \begin{tabular}{c}$#2$\\[2pt]#1\end{tabular}%
}}
\begin{document}
  \electron{2s}{\+\-}\quad \electron{2p}{\+\0\ \+\0\ \0\0}
\end{document}

electrons

If you want the command to work in math mode instead of text mode just remove the dollar signs from the definition of \electron, replace tabular with array, and replace \tabcolsep with \arraycolsep.

You could probably even arrange to use just +, - and 0 in place of \+, \-, and \0, but that's more complicated.