5

I'm new here so bear with me if I don't catch on to something immediately.

I'm trying to write some script for some copyediting I'm doing and it automatically converts the code into the format once you save the edit (LaTeX and MathJax). However, I'm using a seperate cogeditor to prepare the code and it's fighting me on \frac. It's confusing me, because it literally runs fine later in the code, but the first time it's used it fights me. What's going on?

C_{3} H_{8(g)} + 5O_{2(g)}  \longrightarrow  3CO{2(g)} + 4H_{2}O_{(l)}

\vspace{10pt}

 \Delta H_{rxn} =  \Sigma n \Delta H_{f \hspace{5pt} products} -  \Sigma  m \Delta H_{f\hspace{5pt} reactants} 

\vspace{10pt}

test line

 \frac{kJ}{mol} 

test line


\vspace{10pt}

(-2220.1\frac{kJ}{mol}) \hspace{3pt}= \hspace{3pt}[(3 mol \hspace{3pt} \times\hspace{3pt} -393.5\frac{kJ}{mol}) + (4 mol \hspace{3pt} \times \hspace{3pt} -285.8\frac{kJ}{mol})] - [(1 mol  \hspace{3pt}\times \hspace{3pt} ∆Hf C3H8) + (5 mol\hspace{3pt}  \times \hspace{3pt} 0\frac{kJ}{mol})]

\vspace{10pt}

-2220.1\frac{kJ}{mol} \hspace{3pt}= \hspace{3pt} (-2321.7\frac{kJ}{mol})\hspace{3pt} -\hspace{3pt} ( \Delta H_{f\hspace{3pt} C_{3}H_{8}})

\vspace{10pt}

 \Delta H_{f\hspace{3pt} C_{3}H_{8}} \hspace{3pt}= \hspace{3pt} -103.6\frac{kJ}{mol}

enter image description here

22
  • I don't think, this is how \frac is meant to be used. you should use siunitx-package for the units.
    – naphaneal
    Apr 22, 2016 at 15:51
  • I was using \frac because sciweavers.org/free-online-latex-equation-editor this automatically used it. Apr 22, 2016 at 15:51
  • @DaddyLeibniz: This is a proof that such online editors are not really well-designed :-(
    – user31729
    Apr 22, 2016 at 15:57
  • @ChristianHupfer Exactly. I just hopped into an editing project not long ago and if I want continued work (which I WANT and NEED the it and the money from it), I gotta figure out how to incorporate LaTeX and incorporate it fast. Does anybody know of any editors they recommend? Apr 22, 2016 at 15:59
  • 1
    @DaddyLeibniz So I guess the answer is "put your code inside math mode", but please take half an hour and read some basic latex intro. It will save you a tremendous amount of debugging-time in the future! Apr 22, 2016 at 16:37

1 Answer 1

9

The following example uses specialized packages for the different tasks:

  • mhchem for easier input and typesetting of chemical formulas.
  • siunitx for setting units, numbers and numbers with units. There are many configuration options.
  • amsmath for more advanced features in the area of mathematical typesetting.

Example:

\documentclass{article}
\usepackage{amsmath}
\usepackage[version=4]{mhchem}
\usepackage{siunitx}

\sisetup{per-mode=fraction}

\DeclareSIUnit{\kJm}{\kilo\joule\per\mol}

\begin{document}
\ce{C3H8_{(g)} + 5O2_{(g)} -> 3CO2_{(g)} + 4H_2O_{(l)}}

\vspace{10pt}

\[
  \Delta H_{\text{rxn}} =  \sum_n \Delta H_{\!f\,\text{products}}
  - \sum_m \Delta H_{\!f\,\text{reactants}}
\]

\vspace{10pt}

test line

per-mode=fraction: \si{\kilo\joule\per\mol}

per-mode=symbol: \si[per-mode=symbol]{\kilo\joule\per\mol}

per-mode=reciprocal: \si[per-mode=reciprocal]{\kilo\joule\per\mol}

\vspace{10pt}

\begin{align*}
  \SI{-2220.1}{\kJm}
  ={}& \left[ \SI{3}{\mol} \times \Bigl(\SI{-393.5}{\kJm}\Bigr)
         + \SI{4}{\mol} \times \Bigl(\SI{-285.8}{\kJm}\Bigr)
       \right]
  \\
  &  - \left[ \SI{1}{\mol} \times \Delta H_{\!f\,\ce{C3H8}}
        + \SI{5}{\mol} \times \SI{0}{\kJm}
       \right]
\end{align*}
\end{document}

Result

5
  • That's incredible! I like this so much. Once I can vote you up, I will. Thanks! I just downloaded a TeX editor, so once I figure that out, I'll throw this in and fool with it a little more! Apr 22, 2016 at 17:02
  • Maybe the \Sigma should be \sum signs Apr 22, 2016 at 17:07
  • Perhaps the final broken equation would look better with the multlined environment, don't you think?
    – Bernard
    Apr 22, 2016 at 18:02
  • @Bernard There are many ways. I wanted the summands aligned after the equals sign (an alternative approach: \begin{aligned}[t]&...\\&...\end{aligned}). Apr 22, 2016 at 18:09
  • It's probably a matter of personal choice. I tend to emphasize the fact the second line is the end of a formula.
    – Bernard
    Apr 22, 2016 at 18:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .