9

\mbox doesn't work inside align*:

\begin{document}
\usepackage{mhchem}

    \begin{align*} 
    \ce{K_w} &= \ce{[H3O+][OH^-]} \\
    -\log \ce{K_w} &= -\log ( \ce{[H3O+][OH^-]} ) \\
    -\log \ce{K_w} &= - ( \log \ce{[H3O+]} + \log \ce{[OH^-]} )  & \text{(Using log law for RHS.)}\\
    -\log \ce{K_w} &= -  \log \ce{[H3O+]} - \log \ce{[OH^-]} \\
    -\log \ce{K_w} &= -  \log \ce{[H3O+]} + - \log \ce{[OH^-]} \\
    \mbox{ \ce{pK_w} &= \ce{pH} + \ce{pOH} }\\
    \end{align*}

\end{document}
  • \mbox should not span across the alignment operator & in the align (or align*) environment. So you need \mbox{...} &= \mbox{...}. Why do you want to do this anyway? – Werner Oct 2 '11 at 6:17
  • Unfortunately, it doesn't work. Even if it did, is there a less awkward solution than splitting up my box command? I want to do it to emphasize a particular result which should be remembered. I thought of using empheq but that seems pretty heavy-handed; I just want that one line boxed in the least obstrusive way possible as far as the code I have to add. – ptrcao Oct 2 '11 at 6:38
  • Unlike the operator p equilibrium constants such as Kw are simple variables and should simply be typeset as such, i.e., in math mode and in italics $K_w$. For reference see page 58 of IUPAC Green Book (third edition). – clemens May 24 '13 at 20:35
16

Perhaps you are trying to place a box around the last equation. One way to do that is to use Aboxed from the mathtools package:

\documentclass{article}
\usepackage{mathtools}
\usepackage{mhchem}
\begin{document}
    \begin{align*} 
    \ce{K_w} &= \ce{[H3O+][OH^-]} \\
    -\log \ce{K_w} &= -\log ( \ce{[H3O+][OH^-]} ) \\
    -\log \ce{K_w} &= - ( \log \ce{[H3O+]} + \log \ce{[OH^-]} )  & \text{(Using log law for RHS.)}\\
    -\log \ce{K_w} &= -  \log \ce{[H3O+]} - \log \ce{[OH^-]} \\
    -\log \ce{K_w} &= -  \log \ce{[H3O+]} + - \log \ce{[OH^-]} \\
    \Aboxed{ \ce{pK_w} &= \ce{pH} + \ce{pOH} }
    \end{align*}
\end{document}

enter image description here

While it is true that the descriptions of mbox tell you that it creates a box just wide enough to hold the text, this is not a box that gets drawn. It is a virtual "box" in the TeX sense that does not get split across lines.

  • Just the simple elegant solution I was after, thanks Peter! – ptrcao Oct 2 '11 at 6:53
4

If you want the highlighting of the equation to be a bit more subtle, you can modify the \Aboxed command provided by the mathtools package. For instance, in the example below, the equation of interest is emphasized with a 15% black background (alternatives is also possible, of course):

\documentclass{article}
\usepackage{mathtools}% http://ctan.org/pkg/mathtools
\usepackage{xcolor}% http://ctan.org/pkg/xcolor
\usepackage[version=3]{mhchem}% http://ctan.org/pkg/mhchem
\usepackage{etoolbox}% http://ctan.org/pkg/etoolbox
\makeatletter
\patchcmd{\@Aboxed}{\boxed{#1#2}}{\colorbox{black!15}{$#1#2$}}{}{}%
\makeatother
\begin{document}

    \begin{align*} 
    \ce{K_w} &= \ce{[H3O+][OH^-]} \\
    -\log \ce{K_w} &= -\log ( \ce{[H3O+][OH^-]} ) \\
    -\log \ce{K_w} &= - ( \log \ce{[H3O+]} + \log \ce{[OH^-]} )  & \text{(Using log law for RHS.)}\\
    -\log \ce{K_w} &= -  \log \ce{[H3O+]} - \log \ce{[OH^-]} \\
    -\log \ce{K_w} &= -  \log \ce{[H3O+]} + - \log \ce{[OH^-]} \\
    \Aboxed{ \ce{pK_w} &= \ce{pH} + \ce{pOH} }\\
    \end{align*}

\end{document}

Modified \Aboxed

Modification of \Aboxed is performed using \patchcmd{<command>}{<search>}{<replace>}{<success>}{<failure>} (provided by the etoolbox package). Specifically, the \boxed{#1#2} command (that boxes the left- #1 and right-hand side #2 of the equation in align) is replaced with \colorbox{<color>}{$#1#2$} allowing for a coloured box. Colour choices is supported by means of xcolor.

  • 2
    It should be \colorbox{black!15}{$#1#2$} or the alignment will not be as desired. – egreg Oct 2 '11 at 9:25
  • @egreg: It was too early in the morning too see that; thanks! :) – Werner Oct 2 '11 at 14:19
  • Great solution! What are the two arguments in e. g. \boxed{#1#2}? Is the & separating the two arguments? I have never seen this before. – Dr. Manuel Kuehner Jun 11 '17 at 9:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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