# Tikz boxes do not appear of right size

I have a beamer document with an eqnarray. I would like the box to appear nice and clean. Specifically, I would like that the boxes around the parts of the equations appear of the right size. The actual code (below) is giving me this output:

as you can see boxes are placed right but they are too short (the blue and the green one).

\begin{frame}

Details on computations leading to the following expression of $R_i$ are reported in the paper. Here it is worth noticing that the generalization error can be split into 6 components, namely:

\tikzstyle{na} = [baseline=-1.3ex]

\item Coriolis acceleration
\tikz[na] \node[coordinate] (n1) {};
\end{itemize}
%\begin{eqnarray}
%        \tikz[baseline]{
%            \node[fill=blue!20,anchor=base] (t1)
%            {$2\vec{\omega}_{ib}\times\frac{{}^bd}{dt}\vec{r}$};
%}
%\end{eqnarray}

{\footnotesize

\begin{eqnarray}\label{eq:generalization_error_FE_var}
&&{R_i\left(\{\underline{x}_{n,t}\}_{n=1,\ldots,N}^{t=1,\ldots,T}\right)} \nonumber \\
&& \tikz[baseline]{
\node[fill=blue!20,anchor=base,height=3em, width=130mm] (t1)
{$\frac{\sigma^2}{{T}^2} \underline{1}_{{T}}' {X_i} \left(\sum_{n=1}^N {\ddot{X}_n'\Phi^{+} \ddot{X}_n}\right)^{-1} {X_i'} \underline{1}_{{T}}$}}
+\tikz[baseline]{
\node[fill=red!20,anchor=base] (t1) {$\frac{\sigma^2}{{T}^2}\underline{1}_{{T}}' {\Psi} \underline{1}_{{T}}$}}
\nonumber \\
&&- \tikz[baseline]{
\node[fill=green!20,anchor=base] (t1) {$\frac{2 {\sigma^2}}{{T}^2} \underline{1}_{{T}}' {X_i} \left(\sum_{n=1}^N {\ddot{X}_n'\Phi^{+} \ddot{X}_n} \right)^{-1} {\ddot{X}_i'\Phi^{+} Q_T} {\Psi} \underline{1}_{{T}}$}}  \nonumber \\
&& + \sigma^2 \mathbb{E} \Bigg\{\left(\underline{x}_i^{test}\right)'\left(\sum_{n=1}^N {\ddot{X}_n'\Phi^{+} \ddot{X}_n} \right)^{-1} \underline{x}_i^{test} {\big| \{\underline{x}_{n,t}\}_{n=1,\ldots,N}^{t=1,\ldots,T}} \Bigg\} \nonumber \\
&&-\frac{2 \sigma^2}{{T}} \underline{1}_{{T}}' {X_i} \left(\sum_{n=1}^N {\ddot{X}_n'\Phi^{+} \ddot{X}_n}\right)^{-1} \mathbb{E} \left\{\underline{x}_i^{test} \right\} \nonumber \\
&& +\frac{2 {\sigma^2}}{{T}} \left({Q_T} {\Psi} \underline{1}_{{T}}\right)' {\Phi^{+} \ddot{X}_i} \left(\sum_{n=1}^N {\ddot{X}_n' \Phi^{+} \ddot{X}_n} \right)^{-1} \mathbb{E} \left\{\underline{x}_i^{test} \right\}\,,
\end{eqnarray}

}

\end{frame}



How can I fix it? I would also like arrows to appear and display: First part (on blue rectangle), second part (on red) and third part (on green rectangle)

• Please extend your code fragment to complete MWE (Minimal Working Example), a small document beginning with \documentclass and end with \end{docment}. BTW, TikZ doesn't know for height, and width, but for minimum height, and minimum width Aug 11, 2021 at 17:54

I consider only equation (it is so huge, that I doubt that you will be able to insert left out stuff this frame):

Is above what you after? I try to wee bit improve your equation layout, but in this I may introduce some math error. So check it if it is still correct.

\documentclass{beamer}
\usepackage{tikz}
\usepackage{mathtools}
\tikzset{every picture/.append style = {baseline},
box/.style = {fill=#1, minimum height=9ex, anchor=base},
}

\begin{document}
\begin{frame}
\footnotesize
\begin{align}\label{eq:generalization_error_FE_var}
\MoveEqLeft
R_i\left(\bigl\{\underline{x}_{n,t}\bigr\}_{n=1,\ldots,N}^{t=1,\ldots,T}\right) \notag \\
& = \tikz{\node[box=blue!20] (t1)
{$\displaystyle\frac{\sigma^2}{{T}^2} \underline{1}_{{T}}' {X_i} \left(\sum_{n=1}^N {\ddot{X}_n'\Phi^{+} \ddot{X}_n}\right)^{-1} {X_i'} \underline{1}_{{T}}$}}
+ \tikz{ \node[box=red!20] (t2)
{$\displaystyle\frac{\sigma^2}{{T}^2}\underline{1}_{{T}}' {\Psi} \underline{1}_{{T}}$}} \notag  \\
%
& - \tikz{\node[box=green!20] (t3)
{$\displaystyle\frac{2 {\sigma^2}}{{T}^2} \underline{1}_{{T}}' {X_i} \left(\sum_{n=1}^N {\ddot{X}_n'\Phi^{+} \ddot{X}_n} \right)^{-1} {\ddot{X}_i'\Phi^{+} Q_T} {\Psi} \underline{1}_{{T}}$}}  \notag \\
%
& + \sigma^2 \mathbb{E} \left\{\left(\underline{x}_i^\mathrm{test}\right)'\left(\sum_{n=1}^N {\ddot{X}_n'\Phi^{+} \ddot{X}_n} \right)^{-1}
\underline{x}_i^\mathrm{test} \middle| \bigl\{\underline{x}_{n,t}\bigr\}_{n=1,\ldots,N}^{t=1,\ldots,T} \right\} \notag \\
%
& - \frac{2 \sigma^2}{{T}} \underline{1}_{{T}}' {X_i} \left(\sum_{n=1}^N {\ddot{X}_n'\Phi^{+} \ddot{X}_n}\right)^{-1} \mathbb{E} \left\{\underline{x}_i^\mathrm{test} \right\} \notag \\
%
& + \frac{2 {\sigma^2}}{{T}} \left({Q_T} {\Psi} \underline{1}_{{T}}\right)' {\Phi^{+} \ddot{X}_i} \left(\sum_{n=1}^N {\ddot{X}_n' \Phi^{+} \ddot{X}_n} \right)^{-1} \mathbb{E} \left\{\underline{x}_i^\mathrm{test} \right\}\,,
\end{align}
\end{frame}
\end{document}


Note: Ih you lake to have boxed layout as are shown in your question, than you only need to change box style to something like this:

             box/.style = {draw, fill=#1, minimum height=9ex, anchor=base,


and load TikZ library shadows to document preamble.

If you use in other frame nodes styles with the same name as in this one, but have different options, which contradict used in above MWE, than the simple solution (when contents of other frame are unknown) is move \tikzset for this frame inside frame (to make it local) and add option fragile to frame:

\documentclass{beamer}
\usepackage{tikz}
\usepackage{mathtools}

\begin{document}
\begin{frame}[fragile] % <---
\tikzset{every picture/.append style = {baseline},
box/.style = {fill=#1, minimum height=9ex, anchor=base},
}
\footnotesize
\begin{align}\label{eq:generalization_error_FE_var}
\MoveEqLeft
R_i\left(\bigl\{\underline{x}_{n,t}\bigr\}_{n=1,\ldots,N}^{t=1,\ldots,T}\right) \notag \\
& = \tikz{\node[box=blue!20] (t1)
{$\displaystyle\frac{\sigma^2}{{T}^2} \underline{1}_{{T}}' {X_i} \left(\sum_{n=1}^N {\ddot{X}_n'\Phi^{+} \ddot{X}_n}\right)^{-1} {X_i'} \underline{1}_{{T}}$}}
+ \tikz{ \node[box=red!20] (t2)
{$\displaystyle\frac{\sigma^2}{{T}^2}\underline{1}_{{T}}' {\Psi} \underline{1}_{{T}}$}} \notag  \\
%
& - \tikz{\node[box=green!20] (t3)
{$\displaystyle\frac{2 {\sigma^2}}{{T}^2} \underline{1}_{{T}}' {X_i} \left(\sum_{n=1}^N {\ddot{X}_n'\Phi^{+} \ddot{X}_n} \right)^{-1} {\ddot{X}_i'\Phi^{+} Q_T} {\Psi} \underline{1}_{{T}}$}}  \notag \\
%
& + \sigma^2 \mathbb{E} \left\{\left(\underline{x}_i^\mathrm{test}\right)'\left(\sum_{n=1}^N {\ddot{X}_n'\Phi^{+} \ddot{X}_n} \right)^{-1}
\underline{x}_i^\mathrm{test} \middle| \bigl\{\underline{x}_{n,t}\bigr\}_{n=1,\ldots,N}^{t=1,\ldots,T} \right\} \notag \\
%
& - \frac{2 \sigma^2}{{T}} \underline{1}_{{T}}' {X_i} \left(\sum_{n=1}^N {\ddot{X}_n'\Phi^{+} \ddot{X}_n}\right)^{-1} \mathbb{E} \left\{\underline{x}_i^\mathrm{test} \right\} \notag \\
%
& + \frac{2 {\sigma^2}}{{T}} \left({Q_T} {\Psi} \underline{1}_{{T}}\right)' {\Phi^{+} \ddot{X}_i} \left(\sum_{n=1}^N {\ddot{X}_n' \Phi^{+} \ddot{X}_n} \right)^{-1} \mathbb{E} \left\{\underline{x}_i^\mathrm{test} \right\}\,,
\end{align}
\end{frame}
\end{document}
`

Result is the same as before.

• thank you for the reply. You are right that I did not include a MWE and thank you for having done so. Is it possible to further add arrows explaining what the various colored parts are=? (e.g. "first", "second", "third"? Aug 14, 2021 at 9:19
• I also realized that, since I produced a tree using tikz, there are conflicting options that may lead to the error. Specifically: %\tikzset{ %every node/.style={draw,text width=2cm,drop shadow}, %style1/.style= {rectangle, rounded corners=2pt, thin,align=center,fill=green!30}, %style2/.style= {rectangle, rounded corners=6pt, thin,align=center,fill=green!60}, %style3/.style= {rectangle,thin,align=left,fill=pink!60}, %svisible on/.style={alt=#1{}{opacity=0,every shadow/.style={opacity=0}}} %}. If I comment them no problem emerges. But I need them. Is there a workaround maybe? Aug 14, 2021 at 9:22
• @NutarelliFederico, than move this definition inside frame. as is done in edited answer (will appear soon). Aug 14, 2021 at 9:43
• Actually it does fail again....I also tried to make the frames where a tree is made fragile but it sems that the fragile option is ignored somehow Aug 14, 2021 at 12:23
• @NutarelliFederico, I don't know, what you doing. problem for sure is not caused by content of frame in my MWE. I extensively tested my solution, and it not interfere with contents of other frames. If you have new problem, please ask new question where provide MWE with this frame and frame where you have your tree. Now I can only guessing, what is going wrong in your document. Aug 14, 2021 at 13:12