9

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

notation

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

enter image description here

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}
  • The only difficulty there is to put a prescript (\prescript from mathtools 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. – Manuel Jul 18 '14 at 10:59
  • 2
    Welcome to TeX.SX! I can't find any question in your post. This is not a "do it for me" homepage. Where are you having problems, what have you tried. Where is your example code? – LaRiFaRi Jul 18 '14 at 10:59
  • Welcome to TeX.SX! Have you tried looking at http://www.ctan.org/topic/chemistry ? – Andrew Swann Jul 18 '14 at 11:01
  • Hi, unfortunately I haven't used LaTex that much, so this is why I didn't include code in my question. If you could at least show me an example I would appreciate it very much. I don't know if there are functions in LaTex, like to do it automatically, but I guess if you could also show me that it would be great. I will look into the site, thank you. – user2563892 Jul 18 '14 at 11:04
  • @user2563892 Anyways, an example from \documentclass to \end{document} of something, just so we can copy that code and start working from there rather than typing it ourselves, is always welcome. – Manuel Jul 18 '14 at 11:07
  • Well, I think there's no need for that, the only problem is putting a letter in a circle and making it text. And I have no experience what so ever. There's no need for a full document, just an example that I could use. – user2563892 Jul 18 '14 at 11:15
  • 2
    @user2563892 : if three comments ask you to provide a MWE (an example code), I think you should interpret it as there is a need for that, at least to show us your good will to solve your problem. We won't solve it for you, you know, just provide some hints and directions for further studies. – Clément Jul 18 '14 at 11:32
  • Thank you for your "help". \documentclass[a4paper]{article} \usepackage{mhchem} \usepackage{tikz} \newcommand*\circled[1]{\tikz[baseline=(char.base)]{ \node[shape=circle,draw,inner sep=2pt] (char) {#1};}} \begin{document} \ce{^{Ar}_{Z}El}\circled{+Z}(Z p, Ar-Z n) \end{document} – user2563892 Jul 18 '14 at 11:51
  • Too bad I can't yet answer my own question – user2563892 Jul 18 '14 at 11:51
  • 1
    For the nuclides see Chemistry package for typesetting nuclide. Also maybe interesting: the bohr provides \atomicnumber{<element>} (e.g. \atomicnumber{F} gives 9) – cgnieder Jul 18 '14 at 13:52

2 Answers2

8

Is that what you want? I define a \chemelt command, with a very simple syntax: enter the comma-separated list of the element symbol, the atomic number and the atomic mass (in that order). It requires loading pstricks (for the circle box), with pdfoption to compile with pdfLaTeX, xparse (for the simple syntax) and mathtools (for the prescript command).

    \documentclass[a4paper,12pt, pdf]{article}
    \usepackage[utf8]{inputenc}
    \usepackage[T1]{fontenc} %
    \usepackage{lmodern} 

    \usepackage{pst-node}
    \newcounter{neutron}
    \usepackage{mathtools}
    \usepackage{xparse}

    \DeclareDocumentCommand{\chemelt}{>{\SplitArgument{2}{,}}m}
    {\chemeltaux#1}
    \NewDocumentCommand{\chemeltaux}{mmm}{\setcounter{neutron}{\numexpr#3-#2\relax}\def\C{\pscirclebox{+8}} \ensuremath{\mathrm{\prescript{#3}{#2}#1}}\raisebox{-0.25\height}{\scriptsize\pscirclebox[framesep = 1pt, linewidth = 0.6pt]{+#2} (#2p,\theneutron n)}}

    \begin{document}

    \chemelt{O, 8,16}\\

    \chemelt{F, 9,19}

    \end{document} 

enter image description here

Bernard
  • 271,350
5

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}

enter image description here

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}

enter image description here

\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.

cgnieder
  • 66,645