5

My LaTeX document contains the following lines:

\documentclass{article}

\usepackage{etoolbox}

\newcommand{\defineSpecies}[2]{\csdef{spec@#1}{#2}}
\newrobustcmd{\spec}[1]{%
  \ifcsname spec@#1\endcsname
    \csuse{spec@#1}
  \else
    \GenericError{}{Undefined species `#1'}{}{}
  \fi
}
\defineSpecies{h216o}{H$_2^{~16}$O}

\begin{document}
\spec{h216o}. \spec{h216o} molecule
\end{document}

When compiling this document, an extra space (denoted with a red box) is put before the dot in the PDF file:

enter image description here

Could you please help me to redefine \spec{} that (a) it does not provide the red space before the dot, but (b) it includes the blue space before the world molecule.

Please focus on repairing this command instead of suggesting packages, like mchem.

3 Answers 3

10

Add % at the end of the lines.

\documentclass{article}

\usepackage{etoolbox}

\newcommand{\defineSpecies}[2]{\csdef{spec@#1}{#2}}
\newrobustcmd{\spec}[1]{%
    \ifcsname spec@#1\endcsname
    \csuse{spec@#1}% <<<<
    \else
    \GenericError{}{Undefined species `#1'}{}{}% <<<<
    \fi
}
\defineSpecies{h216o}{H$_2^{~16}$O}

\begin{document}
    \spec{h216o}. \spec{h216o} molecule
\end{document}

aaa

2
  • Thank you very much! It helped a lot! I'll accept this answer in 3 minutes (as soon as tex.stachexchange allows). ;)
    – TobiR
    Oct 11, 2021 at 18:39
  • 1
    you missed one in the error case Oct 11, 2021 at 19:11
5

Apart from the missing % to mask endlines, you might exploit the chemformula package:

\documentclass{article}

\usepackage{etoolbox}
\usepackage{chemformula}

\newcommand{\defineSpecies}[2]{\csdef{spec@#1}{\ch{#2}}}
\newrobustcmd{\spec}[1]{%
  \ifcsname spec@#1\endcsname
    \csuse{spec@#1}%
  \else
    \GenericError{}{Undefined species `#1'}{}{}%
  \fi
}
\defineSpecies{h216o}{H2\,{^{16}}O}
\defineSpecies{caco3}{CaCO3}

\begin{document}

\spec{h216o}. \spec{caco3} molecule

\end{document}

enter image description here

A possible improvement is to use a “centralized” species definition.

\documentclass{article}

\usepackage{chemformula}

\ExplSyntaxOn

\prop_new:N \g_tobir_species_prop
\cs_generate_variant:Nn \chemformula_ch:nn { ne }

\NewDocumentCommand{\defineSpecies}{m}
 {
  \prop_gset_from_keyval:Nn \g_tobir_species_prop { #1 }
 }
\NewDocumentCommand{\spec}{m}
 {
  \prop_if_in:NnTF \g_tobir_species_prop { #1 }
   {
    \chemformula_ch:ne {} { \prop_item:Nn \g_tobir_species_prop { #1 } }
   }
   {
    \GenericError{}{Undefined~species~`#1'}{}{}
   }
 }

\ExplSyntaxOff

\defineSpecies{
  h216o = H2\,{^{16}}O,
  caco3 = CaCO3,
}

\begin{document}

\spec{h216o}. \spec{caco3} molecule

\end{document}
2

There might be good reasons for you to use etoolbox, but to write chemical equations, you do not need it. Like highly recommended for the body of questions/answers/comments on sibling chemistry.se, you may simply use mhchem (entry CTAN) to yield, e.g.

enter image description here

with an easier to read MWE like

\documentclass{article}
% \usepackage{etoolbox}  % neither requirement for, no inhibitor of a compilation
\usepackage[version=4]{mhchem}

\begin{document}
Our water is labelled as $\ce{H2{}^{16}O}$. $\ce{CaCO3}$ then is added.
\end{document}

The explicit call for version 1, 2, or 3 yield output with perhaps slightly different kerning; for the problem presented in your question, this however is irrelevant. To use {} past H2 prevents 2 to be misunderstood as a mass number of oxygen instead of an atom multiplier of hydrogen; the manual explicitly mentions this assistance by the user to the usepackage.

In addition to the project's own documentations, chemistry.se showcases some peculiarities in a dedicated FAQ, too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.