LaTeX forum ⇒ 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

Postby yotama9 » Thu Dec 03, 2009 11:10 am

Hi guys.

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

  1. \begin {equation}
  2. E = mc^2
  3. \label {Einsteins'}
  4. \end {equation}


so I want something like:
  1. \begin {chem equation}
  2. H^+ + OH^- \arrow H_2 O
  3. \label {Einsteins'}
  4. \end {chem equation}


*I'm using the mhchem package.

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

Postby localghost » Thu Dec 03, 2009 11:22 am

I assume you have already read the mhchem manual.


Best regards
Thorsten
LaTeX Community Moderator
How to make a "Minimal Example"
Board Rules
Avoidable Mistakes


¹ System: openSUSE 42.2 (Linux 4.4.52), TeX Live 2016 (vanilla), TeXworks 0.6.1

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

Postby josephwright » Thu Dec 03, 2009 2:30 pm

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

Postby yotama9 » Thu Dec 03, 2009 4:12 pm

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: 9204
Joined: Fri Feb 02, 2007 12:06 pm

Postby localghost » Thu Dec 03, 2009 8:10 pm

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.
LaTeX Community Moderator
How to make a "Minimal Example"
Board Rules
Avoidable Mistakes


¹ System: openSUSE 42.2 (Linux 4.4.52), TeX Live 2016 (vanilla), TeXworks 0.6.1

yotama9
Posts: 61
Joined: Thu Sep 24, 2009 2:59 pm

Postby yotama9 » Fri Dec 04, 2009 7:40 pm

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

Postby yotama9 » Sun Dec 06, 2009 5:39 pm

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:
  1. \newcounter{reaction}
  2. \addtocounter{reaction}{1}
  3. \newcommand{\reaction}[1]{
  4. \begin{flushleft}
  5. \centering
  6. {#1}
  7. \end{flushleft}
  8. \begin{flushright}
  9. reaction(\arabic{chapter}.\arabic{reaction} \stepcounter{reaction})
  10. \end{flushright}
  11. }


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: 9204
Joined: Fri Feb 02, 2007 12:06 pm

Postby localghost » Sun Dec 06, 2009 6:23 pm

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.
  1. \documentclass[11pt,a4paper,english]{report}
  2. \usepackage[T1]{fontenc}
  3. \usepackage[utf8]{inputenc}
  4. \usepackage{babel}
  5. \usepackage{amsmath,amssymb}
  6.  
  7. \makeatletter
  8. \@definecounter{reaction}
  9. \def\reaction{$$\refstepcounter{reaction}}
  10. \def\endreaction{\eqno \hbox{\@reacnum}$$\@ignoretrue}
  11. \def\@reacnum{{\normalfont \normalcolor (\thereaction)}}
  12. \@addtoreset{reaction}{chapter}
  13. \renewcommand\thereaction{C\ifnum \c@chapter>\z@ \thechapter.\fi \@arabic\c@reaction}
  14. \makeatother
  15.  
  16. \begin{document}
  17. \chapter{One}
  18. An equation
  19. \begin{equation}
  20. E=mc^2
  21. \end{equation}
  22.  
  23. A reaction
  24. \begin{reaction}\label{rctn:water}
  25. H^+ + OH^- \leftrightharpoons H_2O
  26. \end{reaction}
  27. The reference to reaction \eqref{rctn:water}.
  28.  
  29. \medskip
  30. Another equation
  31. \begin{equation}
  32. E=mc^2
  33. \end{equation}
  34. \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.
LaTeX Community Moderator
How to make a "Minimal Example"
Board Rules
Avoidable Mistakes


¹ System: openSUSE 42.2 (Linux 4.4.52), TeX Live 2016 (vanilla), TeXworks 0.6.1

yotama9
Posts: 61
Joined: Thu Sep 24, 2009 2:59 pm

Postby yotama9 » Mon Dec 07, 2009 1:20 pm

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: 9204
Joined: Fri Feb 02, 2007 12:06 pm

Postby localghost » Mon Dec 07, 2009 8:11 pm

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.
  1. \documentclass[11pt,a4paper]{report}
  2. \usepackage[T1]{fontenc}
  3. \usepackage[utf8]{inputenc}
  4. \usepackage{lmodern}
  5. \usepackage[version=3]{mhchem}
  6.  
  7. \makeatletter
  8. \newcounter{reaction}
  9. %%% >> for article <<
  10. %\renewcommand\thereaction{C\,\arabic{reaction}}
  11. %%% << for article <<
  12. %%% >> for report and book >>
  13. \renewcommand\thereaction{C\,\thechapter.\arabic{reaction}}
  14. \@addtoreset{reaction}{chapter}
  15. %%% << for report and book <<
  16. \newcommand\reactiontag{\refstepcounter{reaction}\tag{\thereaction}}
  17. \newcommand\reaction@[2][]{\begin{equation}\ce{#2}%
  18. \ifx\@empty#1\@empty\else\label{#1}\fi%
  19. \reactiontag\end{equation}}
  20. \newcommand\reaction@nonumber[1]{\begin{equation*}\ce{#1}%
  21. \end{equation*}}
  22. \newcommand\reaction{\@ifstar{\reaction@nonumber}{\reaction@}}
  23. \makeatother
  24.  
  25. \begin{document}
  26. \begin{equation}
  27. a+b
  28. \end{equation}
  29. \reaction{CO2 + C}
  30. \reaction*{CO2 + C}
  31. \reaction[react:co]{CO2 + C}
  32. \begin{equation}
  33. a+b
  34. \end{equation}
  35. \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.
LaTeX Community Moderator
How to make a "Minimal Example"
Board Rules
Avoidable Mistakes


¹ System: openSUSE 42.2 (Linux 4.4.52), TeX Live 2016 (vanilla), TeXworks 0.6.1


Return to “General”

Who is online

Users browsing this forum: No registered users and 7 guests