Chemistry notation using mhchem: Difference between revisions

From MoodleDocs
Line 6: Line 6:
===via MathJax===
===via MathJax===
The simplest way is by using MathJax to render TeX notation and include the mhchem extension. To do this, mhchem needs to be added to the MathJax configuration
The simplest way is by using MathJax to render TeX notation and include the mhchem extension. To do this, mhchem needs to be added to the MathJax configuration
In Administration > Site administration > Plugins > Filters > Mathjax > Local Mathjax installation, edit the Mathjax configuration to include mhchem.js in the TeX extensions:
TeX: {
  extensions: ["mhchem.js","color.js","AMSmath.js","AMSsymbols.js","noErrors.js","noUndefined.js"]
},
===via LaTeX===
===via LaTeX===
Alternatively, the mhchem bundle can be installed alongside LaTex on the Moodle server.
Alternatively, the mhchem bundle can be installed alongside LaTex on the Moodle server.

Revision as of 08:04, 31 August 2014

Note: This page is a work-in-progress. Feedback and suggested improvements are welcome. Please join the discussion on moodle.org or use the page comments.


The mhchem pack­age pro­vides com­mands for type­set­ting chem­i­cal molec­u­lar for­mu­lae and equa­tions.

Installation

The mhchem package can be integrated with Moodle in one of two ways

via MathJax

The simplest way is by using MathJax to render TeX notation and include the mhchem extension. To do this, mhchem needs to be added to the MathJax configuration

In Administration > Site administration > Plugins > Filters > Mathjax > Local Mathjax installation, edit the Mathjax configuration to include mhchem.js in the TeX extensions:

TeX: {
 extensions: ["mhchem.js","color.js","AMSmath.js","AMSsymbols.js","noErrors.js","noUndefined.js"]
},

via LaTeX

Alternatively, the mhchem bundle can be installed alongside LaTex on the Moodle server.

In Administration > Site administration > Plugins > Filters > TeX notation > LaTeX renderer Settings, add the following to the LaTeX preamble:

\usepackage{mhchem}

The following gives a filled arrowhead style

\usepackage[arrows=pgf-filled]{mhchem}

Basics

The default TeX delimiters are \[...\] or $$...$$ for displayed mathematics, and \(...\) for in-line mathematics. For mhchem the additional delimiter \ce{...} is required inside the math delimeters i.e. \[\ce{...}\] or $$\ce{...}$$ for displayed chemical formulae, and \(\ce{...}\) for in-line chemical formulae. In the rest of this document the TeX delimiters are not explicitly shown.

H2O

\ce{H2O}

Sb2O3

\ce{Sb2O3}

H+

\ce{H+}

CrO42–

\ce{CrO4^2-}

AgCl2

\ce{AgCl2-}

[AgCl2]

\ce{[AgCl2]-}

Y99+

\ce{Y^{99}+}

H2(aq)

\ce{H2_{(aq)}}

NO3

\ce{NO3-}

(NH4)2S

\ce{(NH4)2S}

Amounts

Place amounts directly in front of a formula. A small space will be inserted automatically.

2H2O

\ce{2H2O}

½H2O

\ce{1/2H2O}

Isotopes

22790Th+

\ce{^{227}_{90}Th+}

Fonts

A few words about fonts. LaTeX distinguishes between text fonts and math fonts. In general, the math fonts have much more glyphs: alphas, nabla operators and all that kind of suff. Ideally, the math font looks very similar to the text font and that is why many LaTeX users do not know about the two kinds of fonts. However, the distinction is important for the use of mhchem.

In the following examples, I will switch the text font to sans serif. This way, one can easily distinguish text font (sans serif) and math font (with serifs):textfont and math font. When setting a document in sans serif, one would, of course, use a matching math font. When you are in a math environment (e.g. opened and closed with a $), you could simply use \ce. Its content will be set in an upright font. (Remember: all variables—like V for volume—are set using an italic font, physical units and chemical elements are set using an upright font.)

VH2O

$V_{\ce{H2O}}$

When used in text mode, \ce adapts to the current text font. You could simply write a formula in one of your section titles it would be set with the correct, no matter where is appears (sectiontitle, header, contents, references, ...).

H2O, H2O

\ce{H2O}, $\ce{H2O}$

CeIV

\ce{Ce^{IV}}

There are some special cases. A negative charge (-) in text mode is replaced with a dash (–), because a text minus sign often is too short. All ‘operators’, e.g. ‘+’ and reaction arrows, are always taken from the math font.

Special Symbols

KCr(SO4)2 · 12H2O

\ce{KCr(SO4)2*12H2O}

KCr(SO4)2 · 12H2O

\ce{KCr(SO4)2.12H2O}

[Cd{SC(NH2)2}2] · [Cr(SCN)4(NH3)2]2

\cf{[Cd\{SC(NH2)2\}2].[Cr(SCN)4(NH3)2]2}

RNO2−·, RNO2−·

$\ce{RNO2^{-.}}$, \ce{RNO2^{-.}}

μ–Cl

\ce{$\mu\hyphen$Cl}

Bonds

Horizontal bonds can be set using the characters -, = and # inside a formula (single, double, triple bond) inside a formula (a - at the end of a formula yields a negative charge).

C6H5−CHO

\ce{C6H5-CHO}

X=Y≡Z

\ce{X=Y#Z}

A−B=C≡D

\ce{A\sbond B\dbond C\tbond D}

Different books visualise bonds in extremely different ways. I will implement the possiblity to adjust the appearance in the future. In the meanwhile, the minus sign from the math font is used to display the bonds (even in text mode). The bonds are vertically aligned on the math axis. For most math fonts, this is lower than half the height of a capital letter.

The \bond command allows you to typeset some types of special bonds.

A−B=C≡D

\ce{A\bond{-}B\bond{=}C\bond{#}D}

A∼B≃C

\ce{A\bond{~}B\bond{~-}C}

A≅B≅C≅D

\ce{A\bond{~=}B\bond{~--}C\bond{-~-}D}

A···B····C

\ce{A\bond{...}B\bond{....}C}

A→B←C

\ce{A\bond{->}B\bond{<-}C}

Please be aware, that the dashed bonds use the \scalebox macro internally, which may not be visualised correctly by some DVI viewers. If you switch to another math font, the sidebearing of the minus sign may vary, which would cause the dashed bonds to align badly (in \bond{~=}, for instance). In that case, you may want to adjust the layout by using the command \mhchemoptions{minus-sidebearing-left=0.06em, minus-sidebearing-right=0.11em} with the appropriate values.

Using Math

To use math commands inside \ce, one can enclose that commands with $.

Fe(CN)6/2

\ce{Fe(CN)_{$\frac{6}{2}$}}

xNa(NH4)HPO4→ (NaPO3)x+xNH3↑+xH2O

\ce{$x\,$ Na(NH4)HPO4 ->[\Delta](NaPO3)_{$x$} + $x\,$ NH3 ^ + $x\,$ H2O}

Formulae

Reaction Arrows

CO2+C → 2CO

\ce{CO2 + C -> 2CO}

CO2+C ← 2CO

\ce{CO2 + C <- 2CO}

CO2+C ⇌ 2CO

\ce{CO2 + C <=> 2CO}

H+ + OH ⇌ H2O

\ce{H+ + OH- <=>> H2O}

A ↔ A

\ce{$A$ <-> $A’$}

CO2 + C −α→ 2CO

\ce{CO2 + C ->[\alpha] 2CO}

CO2 + C −αβ→ 2CO

\ce{CO2 + C ->[\alpha][\beta] 2CO}

As with ^ and _, the content above and below reaction arrows is set in math font. When you want to put descriptive text there, use the \text command. Or, as a shortcut, you could type a ‘T’ between reaction arrow and opening bracket.

CO 2 + C above −−−→ 2CO \ce{CO2 + C ->[\text{above}] 2CO}

CO 2 + C above −−−→ below 2CO \ce{CO2 + C ->[\text{above}][\text{below}] 2CO}

CO 2 + C above −−−→ below 2CO \ce{CO2 + C ->T[above][below] 2CO}

Similarly, there is a shortcut for using \ce with reaction arrows:

A + H 2 O −−−→ B \ce{$A$ ->[\ce{+H2O}] $B$}

A + H 2 O −−−→ B \ce{$A$ ->C[+H2O] $B$}

Precipitate and Gas

Use v or (v) for precipitate (arrow down) and ^ or (^) for gas (arrow up), both separated by spaces.

SO42– + Ba2+ → BaSO4

\ce{SO4^2- + Ba^2+ -> BaSO4 v}

Watch Out! Please be aware that you sometimes have to enclose spaces in braces. In particular, you have to do so, when they appear between brackets that belong to an reaction arrow.

A ←enclose spaces!→ A

\ce{$A$ <->T[{enclose spaces!}] $A’$}

Further Examples

\ce{Zn^2+ <=>[\ce{+ 2OH-}][\ce{+ 2H+}] $\underset{\text{amphoteres Hydroxid}}{\ce{Zn(OH)2 v}}$ <=>C[+2OH-][{+ 2H+}] $\underset{\text{Hydroxozikat}}{\cf{[Zn(OH)4]^2-}}$ } 9 Zn 2+ + 2OH – −−−−−

) −−−−− + 2H + Zn(OH) 2 ↓ amphoteres Hydroxid + 2OH – −−−−−

) −−−−− + 2H + [Zn(OH) 4 ] 2– Hydroxozikat


$K = \frac{[\ce{Hg^2+}][\ce{Hg}]}{[\ce{Hg2^2+}]}$ K = [ Hg 2 + ][ Hg ] [ Hg 2 + 2 ] \ce{Hg^2+ ->[\ce{I-}] $\underset{\mathrm{red}}{\ce{HgI2}}$ ->C[I-] $\underset{\mathrm{red}}{\ce{[Hg^{II}I4]^2-}}$ } Hg 2+ I – −→ HgI 2 red I – −→ [Hg II I 4 ] 2– red