Generalchemical equation numbering

LaTeX specific issues not fitting into one of the other forums of this category.
yotama9
Posts: 61
Joined: Thu Sep 24, 2009 2:59 pm

chemical equation numbering

Post by yotama9 »

Hi guys.

Is there a way to automatically number chemical equations? I want to number them the same way as mathemtical formula i.e.:

Code: Select all

\begin {equation}
  E = mc^2
  \label {Einsteins'} 
\end {equation}
so I want something like:

Code: Select all

\begin {chem equation}
  H^+ + OH^- \arrow H_2 O
  \label {Einsteins'} 
\end {chem equation}
*I'm using the mhchem package.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

chemical equation numbering

Post by localghost »

I assume you have already read the mhchem manual.


Best regards
Thorsten
josephwright
Site Moderator
Posts: 814
Joined: Tue Jul 01, 2008 2:19 pm

chemical equation numbering

Post by josephwright »

If you want equations, use equations! If you want some kind of float, take a look at chemscheme. Also take a look at bpchem and chemcompounds.
Joseph Wright
yotama9
Posts: 61
Joined: Thu Sep 24, 2009 2:59 pm

Re: chemical equation numbering

Post by yotama9 »

I want to separate my equations from my schemes. I don't want the schemes to be floats. I want them where I put them. I only want that I would be able to number and reference chemical scehmes as easily as I would mathematics formula.
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

chemical equation numbering

Post by localghost »

Beside Joseph's advice I think you should take a look at the mhchem manual (»Tips and Tricks«, p. 10f). Just take this code and you got it.
yotama9
Posts: 61
Joined: Thu Sep 24, 2009 2:59 pm

chemical equation numbering

Post by yotama9 »

localghost wrote:Beside Joseph's advice I think you should take a look at the mhchem manual (»Tips and Tricks«, p. 10f). Just take this code and you got it.
You (not only localghost) are insisting on giving me the wrong solutions. I want to create a new counter for my chemical formulas. I cannot think on a better way to explain it.

On the documentations of mhchem the example add the chemical equations counting to the mathematical equations counter which is not good as I won't be able to reffer them.
yotama9
Posts: 61
Joined: Thu Sep 24, 2009 2:59 pm

chemical equation numbering

Post by yotama9 »

I found a solution.
It's not perfect but it does the trick.

I read the Tabula Rasa post and I did something similar:

I created the following command:

Code: Select all

\newcounter{reaction}
\addtocounter{reaction}{1}
\newcommand{\reaction}[1]{
	\begin{flushleft}
	\centering
	{#1}
	\end{flushleft}
	\begin{flushright}
	reaction(\arabic{chapter}.\arabic{reaction} \stepcounter{reaction})
	\end{flushright}
}
I got to problems with it now.
  • It is ugly solution, meaning the flushleft part
    I get one extra line between the reaction and the number.
Any thoghts?

Thanks.

p.s. If someone was offended by my previous post I'm deeply sorry. I do appreciate the help and effort.
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

chemical equation numbering

Post by localghost »

I'm still convinced that the example from the mhchem manual is the most proper solution. But I had an idea and collected some code snippets from the latex.ltx file and the report class file. I modified them and got the result below.

Code: Select all

\documentclass[11pt,a4paper,english]{report}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{babel}
\usepackage{amsmath,amssymb}

\makeatletter
\@definecounter{reaction}
\def\reaction{$$\refstepcounter{reaction}}
\def\endreaction{\eqno \hbox{\@reacnum}$$\@ignoretrue}
\def\@reacnum{{\normalfont \normalcolor (\thereaction)}}
\@addtoreset{reaction}{chapter}
\renewcommand\thereaction{C\ifnum \c@chapter>\z@ \thechapter.\fi \@arabic\c@reaction}
\makeatother

\begin{document}
  \chapter{One}
  An equation
  \begin{equation}
    E=mc^2
  \end{equation}

  A reaction
  \begin{reaction}\label{rctn:water}
    H^+ + OH^- \leftrightharpoons H_2O
  \end{reaction}
  The reference to reaction \eqref{rctn:water}.

  \medskip
  Another equation
  \begin{equation}
    E=mc^2
  \end{equation}
\end{document}
This creates a new reaction environment derived from the equation environment. Therefore it behaves exactly the same way. The reaction counter has been marked with a »C« to separate it optically. I'm not sure if this is what you want.

Note that at the moment this only works with the report class and doesn't represent a general solution but only a first basic approach. For other classes there might be some accommodations needed. I didn't take a look but this might be done much easier with the mathtools package from the mh bundle.
yotama9
Posts: 61
Joined: Thu Sep 24, 2009 2:59 pm

Re: chemical equation numbering

Post by yotama9 »

localghost, I read the mhchem documentation more then one time and I couldn't find an explanation on how number my chemical formulas. Can you please type in a short example?

What does bind your solution for the report document class only? As you can guess, I'm hardly a medium level latex user. I can't understand the code (I can guess the function though). I thought at first to use the equation environment but I didn't know how to separate the counting.

Thanks.
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

chemical equation numbering

Post by localghost »

yotama9 wrote:[…] I read the mhchem documentation more then one time and I couldn't find an explanation on how number my chemical formulas. Can you please type in a short example? […]
I thought I already directed you to the relevant place in the manual. In order not to lengthen this matter take a look at the code below.

Code: Select all

\documentclass[11pt,a4paper]{report}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{lmodern}
\usepackage[version=3]{mhchem}

\makeatletter
\newcounter{reaction}
%%% >> for article <<
%\renewcommand\thereaction{C\,\arabic{reaction}}
%%% << for article <<
%%% >> for report and book >>
\renewcommand\thereaction{C\,\thechapter.\arabic{reaction}}
\@addtoreset{reaction}{chapter}
%%% << for report and book <<
\newcommand\reactiontag{\refstepcounter{reaction}\tag{\thereaction}}
\newcommand\reaction@[2][]{\begin{equation}\ce{#2}%
\ifx\@empty#1\@empty\else\label{#1}\fi%
\reactiontag\end{equation}}
\newcommand\reaction@nonumber[1]{\begin{equation*}\ce{#1}%
\end{equation*}}
\newcommand\reaction{\@ifstar{\reaction@nonumber}{\reaction@}}
\makeatother

\begin{document}
  \begin{equation}
    a+b
  \end{equation}
  \reaction{CO2 + C}
  \reaction*{CO2 + C}
  \reaction[react:co]{CO2 + C}
  \begin{equation}
    a+b
  \end{equation}
\end{document}
This is exactly the code given on page 10 of the mhchem manual. But it is still not a full automatism. You have to choose the appropriate lines according to your document class (see comments).
yotama9 wrote:[…] What does bind your solution for the report document class only? As you can guess, I'm hardly a medium level latex user. I can't understand the code (I can guess the function though). I thought at first to use the equation environment but I didn't know how to separate the counting. […]
The reason is very simple. As mentioned in my last reply I collected some code from the source of the report class. The major part is from the LaTeX kernel. My example may work with the book class (and I'm pretty sure it will). But it doesn't work with the article class because this class doesn't support chapters. There would be the possibility for an if-else-branch but at the moment I haven't got the time to figure out an appropriate implementation.
Post Reply