4

I have the following equation:

enter image description here

Notice how the last two terms are not centered? If I use the code specified below, I would get that effect. Now, what I would like to have is the effect shown in the first term (centered the term "Bias"). You probably also see that since I kind of fail with paint, the first terms are not centered correctly.

Now I was wondering if it is possible to do that in Latex? Notice that it should be centered between the equal sign and the plus sign.

In code form:

\begin{equation}
\begin{aligned}
Err(x_{0}) &=\left(E\left[\hat{f}(x_{0})\right]-f(x_{0})\right)^{2}&+&E\left[\hat{f}(x_{0})-E\left[\hat{f}(x_{0})\right]\right]^{2}&+&\sigma^{2}_{\epsilon} \\
&=\text{Bias}^{2}&+&\text{Var}(\hat{f}(x_{0})&+&\text{Var}(\epsilon)
\end{aligned}
\end{equation}

Now unfortunately, the terms in the first equation and the second equations are not centered. I was wondering how I can center each term.

Thanks

  • 2
    $$...$$ is outdated. Please use \[...\] instead. Your first example is also in code form ;-) And provide a MWE, please – user31729 Feb 18 '15 at 15:52
  • 2
    Plus please learn the correct use of the \text command, hint: you are not using it right. Define \Bias, \Var in the preamble using \DeclareMathOperator\Bias{Bias}, and \DeclareMathOperator\Var{Var}. Var and Bias should be upright in all cases, \text is not! – daleif Feb 18 '15 at 15:59
  • if you want each line centered separately, use gathered instead of aligned. – barbara beeton Feb 18 '15 at 16:20
  • You shouldn't use \left and \right to begin with. – egreg Feb 18 '15 at 21:20
4

I wouldn't center the terms, but it's just my opinion.

You can obtain correct spacing around the = and + symbols with alignedat or also with array if you prefer centering.

\documentclass{article}
\usepackage{amsmath,array}

\DeclareMathOperator{\Err}{Err}
\DeclareMathOperator{\Bias}{Bias}
\DeclareMathOperator{\Var}{Var}
\DeclareMathOperator{\E}{E}

\newcolumntype{O}{>{{}}c<{{}}}

\begin{document}

\begin{equation}
\begin{alignedat}{3}
\Err(x_{0}) &= 
  \bigl(\E[\hat{f}(x_{0})]-f(x_{0})\bigr)^{2} 
  &&+ \E\bigl[\hat{f}(x_{0})-\E[\hat{f}(x_{0})]\bigr]^{2}
  &&+ \sigma^{2}_{\epsilon}
 \\
&= \Bias^{2} &&+ \Var(\hat{f}(x_{0})) &&+ \Var(\epsilon)
\end{alignedat}
\end{equation}

\begin{equation}
\setlength{\arraycolsep}{0pt}
\begin{array}{r O c O c O c O c}
\Err(x_{0}) &=& 
  \bigl(\E[\hat{f}(x_{0})]-f(x_{0})\bigr)^{2} 
  &+& \E\bigl[\hat{f}(x_{0})-\E[\hat{f}(x_{0})]\bigr]^{2}
  &+& \sigma^{2}_{\epsilon}
 \\[1ex]
&=& \Bias^{2} &+& \Var(\hat{f}(x_{0})) &+& \Var(\epsilon)
\end{array}
\end{equation}

\end{document}

Note the definitions of the four math operators; the \newcolumntype declaration is for the second rendering, it represents the column where the = and + signs are.

Don't use \left and \right, that produce awfully oversized fences. Just \bigl and \bigr are necessary in two places.

enter image description here

5

In general you should not use align.. for that kind of construction. If the equations are not numbered a solution is to use array

\documentclass{article}
\usepackage{amsmath}

\DeclareMathOperator{\Bias}{Bias}
\DeclareMathOperator{\Var}{Var}

\begin{document}

\begin{equation*}
\setlength\arraycolsep{1.5pt}
  \begin{array}{rcccccc}
    Err(x_{0}) & = & \left(E\left[\hat{f}(x_{0})\right]-f(x_{0})\right)^{2} 
               & + & E\left[\hat{f}(x_{0})-E\left[\hat{f}(x_{0})\right]\right]^{2}
               & + & \sigma^{2}_{\epsilon}\\
               & = & \Bias^2
               & + & \Var(\hat{f}(x_{0})
               & + & \Var(\epsilon)
  \end{array}
\end{equation*}

\end{document}

producing

enter image description here

See this answer for the rationale for setting the array inter column space to 1.5pt.

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.