3

Consider the following example:

\documentclass{article}

\usepackage{mathtools}
\usepackage[amsmath]{empheq}

\usepackage{xcolor}
\definecolor{shadecolor}{cmyk}{0,0,0.45,0}
\definecolor{light-blue}{cmyk}{0.25,0,0,0}
\newsavebox{\mysaveboxM}
\newsavebox{\mysaveboxT}

\newcommand*\Yellowbox[2][Formel]{%
  \sbox{\mysaveboxM}{#2}
  \sbox{\mysaveboxT}{\fcolorbox{black}{light-blue}{#1}}
  \sbox{\mysaveboxM}{%
    \parbox[b][\ht\mysaveboxM+0.4\ht\mysaveboxT+0.8\dp\mysaveboxT][b]{%
      \wd\mysaveboxM}{#2}
  }
  \sbox{\mysaveboxM}{%
    \fcolorbox{black}{shadecolor}{%
      \makebox[\linewidth]{\usebox{\mysaveboxM}}%
    }%
  }%
  \usebox{\mysaveboxM}%
  \makebox[0pt][r]{%
    \makebox[\wd\mysaveboxM][c]{%
      \raisebox{\ht\mysaveboxM-0.5\ht\mysaveboxT
                +0.5\dp\mysaveboxT-0.5\fboxrule}{\usebox{\mysaveboxT}}
    }
  }
}

\begin{document}

\begin{empheq}[box={\Yellowbox[Inverse $3 \times 3$ matrix]}]{equation*}
  \textsf{A}^{-1}
  = \frac{1}{a(ei-fh)+b(fg-di)+c(dh-eg)}
    \begin{pmatrix}
      ei-fh & ch-bi & bf-ce\\
      fg-di & ai-cg & cd-af\\
      dh-eg & bg-ah & ae-bd
    \end{pmatrix}
\end{empheq}

\end{document}

output

Why is the contents in the box not horizontally centered inside it?

P.S. I have taken the code from the empheq manual and modified it a bit.

Update

I have a feeling that something has changed in empheq because I have used the code above before without and problems.

  • It is centered in the space left by the title... – karlkoeller Aug 12 '13 at 20:41
  • @karlkoellerI don't follow. There is too little space as the front of the formula compared to after it. – Svend Tveskæg Aug 12 '13 at 20:44
  • Sorry, I was looking at the vertical position... – karlkoeller Aug 12 '13 at 20:46
  • You have several unprotected end-of-lines in the definition of \Yellowbox – egreg Aug 12 '13 at 21:06
5

You have several unprotected end-of-lines in the definition of \Yellowbox; you have also an overfull box, because you're not taking into account \fboxsep and \fboxrule.

\documentclass{article}

\usepackage{mathtools}
\usepackage[amsmath]{empheq}
\usepackage{calc}


\usepackage{xcolor}
\definecolor{shadecolor}{cmyk}{0,0,0.45,0}
\definecolor{light-blue}{cmyk}{0.25,0,0,0}
\newsavebox{\mysaveboxM}
\newsavebox{\mysaveboxT}

\newcommand*\Yellowbox[2][Formel]{%
  \sbox{\mysaveboxM}{#2}%
  \sbox{\mysaveboxT}{\fcolorbox{black}{light-blue}{#1}}%
  \sbox{\mysaveboxM}{%
    \parbox[b][\ht\mysaveboxM+0.4\ht\mysaveboxT+0.8\dp\mysaveboxT][b]{%
      \wd\mysaveboxM}{#2}%
  }
  \sbox{\mysaveboxM}{%
    \fcolorbox{black}{shadecolor}{%
      \makebox[\displaywidth-2\fboxsep-2\fboxrule]{\usebox{\mysaveboxM}}%
    }%
  }%
  \usebox{\mysaveboxM}%
  \makebox[0pt][r]{%
    \makebox[\wd\mysaveboxM][c]{%
      \raisebox{\ht\mysaveboxM-0.5\ht\mysaveboxT
                +0.5\dp\mysaveboxT-0.5\fboxrule}{\usebox{\mysaveboxT}}%
    }%
  }%
}

\begin{document}

\noindent X\hrulefill X\par
\begin{empheq}[box={\Yellowbox[Inverse $3 \times 3$ matrix]}]{equation*}
  \textsf{A}^{-1}
  = \frac{1}{a(ei-fh)+b(fg-di)+c(dh-eg)}
    \begin{pmatrix}
      ei-fh & ch-bi & bf-ce\\
      fg-di & ai-cg & cd-af\\
      dh-eg & bg-ah & ae-bd
    \end{pmatrix}
\end{empheq}

\end{document}

enter image description here

There's apparently more space on the right than on the left, because of the fact that the big parentheses (and pmatrix) leave more space. Probably \displaywidth, rather than \linewidth should be used.

  • Can you tell me the difference between \linewidth and \displaywidth-2\fboxsep-2\fboxrule? (THe first actually looks better to me but I'm probably mistaken as ever.) – Svend Tveskæg Aug 14 '13 at 2:30
  • @SvendTveskæg With \linewidth you get an overfull box, so it's hardly “better”; \displaywidth is the width TeX maintains for deciding how wide is a displayed equation, which can be or not equal to \linewidth. – egreg Aug 14 '13 at 9:15
  • Thank you. Assume I have \linewidth-27em (i.e., not just \linewidth), how do I change this to something involving \displaywidth? – Svend Tveskæg Aug 14 '13 at 12:35
  • I got it; never mind. – Svend Tveskæg Aug 14 '13 at 12:37

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.