Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I am using the mhchem package in-line and the equations don't break lines and thus cause overfull boxes. Any way to force them to follow the rules for line breaks.

i.e. the following sentence:

Clays exist in groups with generalised formulae, these include: 
the smectite group \ce{(A1/3D2-3[T4O10]Z2.nH2O)} and the serpentine group 
\ce{(D2[Si2O5](OH4)\pm nH2O)} among others.

the formulae comes out lovely looking it just hangs over the margin. . .

share|improve this question
1  
You'll have to break this up yourself: mhchem does not do this itself. –  Joseph Wright Jul 10 '12 at 15:56
add comment

1 Answer

up vote 2 down vote accepted

The expressions get boxed, but if you shake the box a bit it will break around the math binary operators. (I don't know if this is entirely safe for all mhchem constructs but works here at least to break on the \pm.)

enter image description here

\documentclass{article}

\usepackage{mhchem}

\makeatletter


\makeatletter
\DeclareRobustCommand*{\xce@main}[1]{%
  \bgroup%
  \ifnum 1<\mhchem@option@version%
    \ifmmode\else\def\mhchem@option@alwaystextmode{1}\fi%
  \fi%
  \def\bond{}\def\sbond{}\def\dbond{}\def\tbond{}%
  \ifx\@empty#1\@empty\else%
    \ifmmode\else\mhchem@option@textFont\fi%
    \ensuremath{\break%
      \mhchem@ce@xx#1 \mhchem@END\mhchem@ENDEND%
    \break}%
  \fi%
  \egroup%
}%

\def\xce#1{{%
\let\ce@main\xce@main
\setbox0\vbox{%
\hsize\maxdimen
\ce{#1}%
\par
\setbox0\lastbox
\unskip
\unpenalty
\setbox0\lastbox
\global\setbox1\hbox{\unhbox0\unskip\unskip\unpenalty}%
\global\setbox1\hbox{\unhbox1 \global\setbox3\lastbox}%
}%
\unhbox3 }}
\makeatother


\begin{document}

Clays exist in groups with generalised formulae, these include: the smectite group \ce{(A1/3D2-3[T4O10]Z2.nH2O)} and the serpentine group \ce{(D2[Si2O5](OH4)\pm nH2O)} among others.

Clays exist in groups with generalised formulae, these include: the smectite group \xce{(A1/3D2-3[T4O10]Z2.nH2O)} and the serpentine group \xce{(D2[Si2O5](OH4)\pm nH2O)} among others.





\end{document}
share|improve this answer
    
Thanks, this works. However I did find (by chance a simples way) the chemformula package (which is loaded with chemmacros) stays away from math mode so the equations automatically break :-D –  Darren Whitaker Jul 17 '12 at 11:28
    
Oh well: I had fun making the answer anyway;-) –  David Carlisle Jul 17 '12 at 11:36
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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