# [Tex/LaTex] Do a chemical notation of an element

chemistry

So, at school (outside the US) our teacher has given us this type of chemical notation:

Where Ar is the relative atomic mass, Z atomic number, p protons, n neutrons.
Example:

UPDATE: I want to make a macro out of it, but there's a porblem with \atomicnumber, I think it returns a string instead of an integer or something:

\documentclass[a4paper]{article}
\usepackage{chemmacros}
\usepackage{bohr}
\usepackage{tikz}
\newcounter{neutron}
\newcommand{\mychemistry}[2]{\setcounter{neutron}{\numexpr#2-\atomicnumber{#1}\relax}\ch{^{#2}\atomicnumber{#1}#1}\tikz[baseline=(char.base)]{ \node[shape=circle,draw,inner sep=2pt] (char) {+\atomicnumber{#1}};}(\atomicnumber{#1} p,\theneutron n)}
\begin{document}
\mychemistry{O}{18}
\end{document}


Edit 2015-12-14 – adapt to use elements package

The idea is the same as in the original answer, see below. The bohr package doesn't provide \atomicnumber any more. It is now provided by the elements package. The latter also provides \saveatomicnumber<cs>{<element>} which makes the macro a little bit easier:

\saveatomicnumber\@tmp{#1}


and then

\the\numexpr#2-\@tmp\relax


for the number of neutrons.

\documentclass[a4paper]{article}
\usepackage{chemformula}
\usepackage{elements}
\usepackage{tikz}

\makeatletter
\newcommand*\mychemistry[2]{%
\saveatomicnumber\@tmp{#1}%
\ch{^{#2}_{\atomicnumber{#1}}#1}
\tikz[baseline]{
\node[anchor=base,shape=circle,draw,inner sep=2pt]
{+\atomicnumber{#1}};
}
(%
\atomicnumber{#1} p,
\the\numexpr#2-\@tmp\relax
n%
)%
}
\makeatother

\begin{document}
\mychemistry{O}{18}
\end{document}


Earlier answer using the bohr package

An answer to the edited question: \atomicnumber is not expandable and as a consequence not usable in \numexpr ... \relax. However, if you're certain that you only input the element symbol and never the element name you can use the internal command. For the element F this is \@bohr@atom@number@f, for Ne it's \@bohr@atom@number@ne and so on.

In order to use it with uppercase atomic symbols as input you need to \lowercase{...} the input. \lowercase again is not expandable but you can simply surround the \setcounter part with it. Actually, the counter isn't even needed since it is only used to determine the number of neutrons. It is possible to use \numexpr directly by preceding it with \the. In order to build the needed macro name we can use the primitives \csname...\endcsname or LaTeX's wrapper \@nameuse{...}, both of which are expandable. In the command this would then be something like \csname @bohr@atom@number@#1\endcsname or \@nameuse{@bohr@atom@number@#1}.

Putting this together you can get the number of neutrons with

\lowercase{\the\numexpr#2-\csname @bohr@atom@number@#1\endcsname\relax}


or

\lowercase{\the\numexpr#2-\@nameuse{@bohr@atom@number@#1}\relax}


Adapting you example, I added indentation and comment chars where needed but I also left a space between the different parts since it looked more readable to me this way. I also omitted to name the \node since alignment with the baseline can also be achieved with anchor=base as option to the node.

\documentclass[a4paper]{article}
\usepackage{chemformula}% the package that provides \ch and which is loaded by chemmacros'
\usepackage{bohr}
\usepackage{tikz}

\newcommand*\mychemistry[2]{%
\ch{^{#2}_{\atomicnumber{#1}}#1}
\tikz[baseline]{
\node[anchor=base,shape=circle,draw,inner sep=2pt]
{+\atomicnumber{#1}};
}
(%
\atomicnumber{#1} p,
\lowercase{\the\numexpr#2-\csname @bohr@atom@number@#1\endcsname\relax}
n%
)%
}
\begin{document}
\mychemistry{O}{18}
\end{document}


BTW: if you use chemmacros already you can also easily output protons as p+ and neutrons as n0:

\documentclass[a4paper]{article}
\usepackage{chemmacros}
\usepackage{bohr}
\usepackage{tikz}

\newcommand*\mychemistry[2]{%
\ch{^{#2}_{\atomicnumber{#1}}#1}
\tikz[baseline]{
\node[anchor=base,shape=circle,draw,inner sep=2pt]
{+\atomicnumber{#1}};
}
(%
\atomicnumber{#1} \prt, % <<<<<<<<<
\lowercase{\the\numexpr#2-\csname @bohr@atom@number@#1\endcsname\relax}
\ntr % <<<<<<<<<
)%
}
\begin{document}
\mychemistry{O}{18}
\end{document}


\prt actually just is the equivalent of \chcpd{p+} and \ntr is \chcpd{n^0} so chemmacros isn't really needed and chemformula` would suffice.