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.
\prescript
frommathtools
package, for instance, would work), and to draw the circle around an element (tikz
package would do). There are lots of answers here in this site for doing so. However, may be you want an easy way to type that automatically… perhaps adding more info to the question would help. What input would you expect? A minimal compilable example would be great to invite us to help.bohr
provides\atomicnumber{<element>}
(e.g.\atomicnumber{F}
gives9
)