10

Why are the brackets in the following code bigger than the tikzpicture?

MWE

\documentclass{article}

\usepackage[utf8]{inputenc}
\usepackage{tikz}

\begin{document}

\newcommand{\nada}[2] {\fill[lightgray] (#1,#2) circle (0.5);}
\newcommand{\tr}[2]   {\fill[blue] (#1,#2) circle (0.5);}
\newcommand{\ts}[2]   {\fill[red] (#1,#2) circle (0.5);}

\centering

\begin{tikzpicture}[x=4mm,y=4mm]
\foreach \x in {1,...,7} \tr{\x}{0};
\foreach \x in {7,...,9} \ts{\x}{0};
\fill[green] (10,0) circle (0.5);
\end{tikzpicture}

Traditional validation

\vspace{5mm}

\[E\left[
\begin{tikzpicture}[x=4mm,y=4mm]
\foreach \x in {1,...,4}  \tr{\x}{0};
\foreach \x in {5,...,6}  \ts{\x}{0};
\foreach \x in {7,...,10} \nada{\x}{0};
\foreach \x in {1,...,2}  \nada{\x}{1};
\foreach \x in {3,...,6}  \tr{\x}{1};
\foreach \x in {7,...,8}  \ts{\x}{1};
\foreach \x in {9,...,10} \nada{\x}{1};
\foreach \x in {1,...,4}  \nada{\x}{2};
\foreach \x in {5,...,8}  \tr{\x}{2};
\foreach \x in {9,...,10} \ts{\x}{2};
\end{tikzpicture}
\right]\]

Sliding window

\vspace{5mm}

\[E\left[
\begin{tikzpicture}[x=4mm,y=4mm]
\foreach \x in {1,...,7}  \tr{\x}{0};
\foreach \x in {8,...,10} \ts{\x}{0};
\foreach \x in {1,...,4}  \tr{\x}{1};
\foreach \x in {5,...,7}  \ts{\x}{1};
\foreach \x in {8,...,10} \tr{\x}{1};
\foreach \x in {1,...,4}  \ts{\x}{2};
\foreach \x in {5,...,10} \tr{\x}{2};
\end{tikzpicture}
\right]\]

$k$-fold ($k=3$)

\end{document}
1
  • To answer your question, display mode assumes everything is vertically centered (to the nearest baseline, anyway) and the tikzpicture isn't. Sep 20, 2015 at 17:39

3 Answers 3

3

You can also simply adjust the position of the pictures relative to the baseline without changing the surrounding maths environment at all. The yshift=.5ex is an adjustment for the fact that the circles otherwise sit a little below the baseline:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}

\begin{document}

\newcommand{\nada}[2] {\fill[lightgray] (#1,#2) circle (0.5);}
\newcommand{\tr}[2]   {\fill[blue] (#1,#2) circle (0.5);}
\newcommand{\ts}[2]   {\fill[red] (#1,#2) circle (0.5);}

\centering

\begin{tikzpicture}[x=4mm,y=4mm]
\foreach \x in {1,...,7} \tr{\x}{0};
\foreach \x in {7,...,9} \ts{\x}{0};
\fill[green] (10,0) circle (0.5);
\end{tikzpicture}

Traditional validation

\vspace{5mm}

\[E\left[
\begin{tikzpicture}[x=4mm,y=4mm,baseline={([yshift=-.5ex]current bounding box.center)}]
\foreach \x in {1,...,4}  \tr{\x}{0};
\foreach \x in {5,...,6}  \ts{\x}{0};
\foreach \x in {7,...,10} \nada{\x}{0};
\foreach \x in {1,...,2}  \nada{\x}{1};
\foreach \x in {3,...,6}  \tr{\x}{1};
\foreach \x in {7,...,8}  \ts{\x}{1};
\foreach \x in {9,...,10} \nada{\x}{1};
\foreach \x in {1,...,4}  \nada{\x}{2};
\foreach \x in {5,...,8}  \tr{\x}{2};
\foreach \x in {9,...,10} \ts{\x}{2};
\end{tikzpicture}
\right]\]

Sliding window

\vspace{5mm}

\[E\left[
\begin{tikzpicture}[x=4mm,y=4mm,baseline={([yshift=-.5ex]current bounding box.center)}]
\foreach \x in {1,...,7}  \tr{\x}{0};
\foreach \x in {8,...,10} \ts{\x}{0};
\foreach \x in {1,...,4}  \tr{\x}{1};
\foreach \x in {5,...,7}  \ts{\x}{1};
\foreach \x in {8,...,10} \tr{\x}{1};
\foreach \x in {1,...,4}  \ts{\x}{2};
\foreach \x in {5,...,10} \tr{\x}{2};
\end{tikzpicture}
\right]\]

$k$-fold ($k=3$)

\end{document}

adjustments

5

You can first set the baseline of the picture to be in the middle; then with the gathered environment (a higher level version of \vcenter), you can fix the centering.

\documentclass{article}

\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{tikz}

\begin{document}

\newcommand{\nada}[2] {\fill[lightgray] (#1,#2) circle (0.5);}
\newcommand{\tr}[2]   {\fill[blue] (#1,#2) circle (0.5);}
\newcommand{\ts}[2]   {\fill[red] (#1,#2) circle (0.5);}

\centering

\begin{tikzpicture}[x=4mm,y=4mm]
\foreach \x in {1,...,7} \tr{\x}{0};
\foreach \x in {7,...,9} \ts{\x}{0};
\fill[green] (10,0) circle (0.5);
\end{tikzpicture}

Traditional validation

\vspace{5mm}

\[E\left[
\!\begin{gathered}
\begin{tikzpicture}[x=4mm,y=4mm,baseline=(current bounding box.center)]
\foreach \x in {1,...,4}  \tr{\x}{0};
\foreach \x in {5,...,6}  \ts{\x}{0};
\foreach \x in {7,...,10} \nada{\x}{0};
\foreach \x in {1,...,2}  \nada{\x}{1};
\foreach \x in {3,...,6}  \tr{\x}{1};
\foreach \x in {7,...,8}  \ts{\x}{1};
\foreach \x in {9,...,10} \nada{\x}{1};
\foreach \x in {1,...,4}  \nada{\x}{2};
\foreach \x in {5,...,8}  \tr{\x}{2};
\foreach \x in {9,...,10} \ts{\x}{2};
\end{tikzpicture}
\end{gathered}
\right]\]

Sliding window

\vspace{5mm}

\[E\left[
\!\begin{gathered}
\begin{tikzpicture}[x=4mm,y=4mm,baseline=(current bounding box.center)]
\foreach \x in {1,...,7}  \tr{\x}{0};
\foreach \x in {8,...,10} \ts{\x}{0};
\foreach \x in {1,...,4}  \tr{\x}{1};
\foreach \x in {5,...,7}  \ts{\x}{1};
\foreach \x in {8,...,10} \tr{\x}{1};
\foreach \x in {1,...,4}  \ts{\x}{2};
\foreach \x in {5,...,10} \tr{\x}{2};
\end{tikzpicture}
\end{gathered}
\right]\]

$k$-fold ($k=3$)

\end{document}

enter image description here

The \! before \begin{gathered} is to fix a “feature”.

When you do \left[...\right], the fences are drawn symmetric with respect to the “formula axis”, which lives slightly above the baseline. Since, by default, the baseline of the TikZ picture is at the (0,0) coordinate, you get the effect you see.

So, a first centering using as the baseline the center of the box almost does the job, and using gathered finishes it off.

However, you can get precise positioning without any gathered by shifting a bit the baseline, using the fact that the height of the formula axis above the baseline is

\fontdimen22\textfont2

Rather cryptic, isn't it? ;-) Thus

\[E\left[
\begin{tikzpicture}[x=4mm,y=4mm,
  baseline={([yshift=-\the\fontdimen22\textfont2]current bounding box.center)}]
\foreach \x in {1,...,4}  \tr{\x}{0};
\foreach \x in {5,...,6}  \ts{\x}{0};
\foreach \x in {7,...,10} \nada{\x}{0};
\foreach \x in {1,...,2}  \nada{\x}{1};
\foreach \x in {3,...,6}  \tr{\x}{1};
\foreach \x in {7,...,8}  \ts{\x}{1};
\foreach \x in {9,...,10} \nada{\x}{1};
\foreach \x in {1,...,4}  \nada{\x}{2};
\foreach \x in {5,...,8}  \tr{\x}{2};
\foreach \x in {9,...,10} \ts{\x}{2};
\end{tikzpicture}
\right]\]

would produce

enter image description here

3
  • 1
    All three answers offer fixes for the problem but none so far explains why the original undesired behaviour? Could you please add some remarks about the cause of the problem? Sep 19, 2015 at 1:07
  • @GonzaloMedina Added explanation
    – egreg
    Sep 19, 2015 at 6:31
  • I very much appreciate the thoughtful answer. But I am inclined to choose @cfr's because it is slightly simpler (no amsmath package) and yours adds unnecessary vertical spacing for some reason (you can see that if you remove my \vspace{5mm}). But again, thank you for the detailed answer! Sep 19, 2015 at 13:46
3

You can use \vcenter{..} to vertically center material in math mode. It needs to be inside, for instance an \hbox. So the whole syntax would be \vcenter{\hbox{⟨contents⟩}}:

\documentclass{scrartcl}

\usepackage[utf8]{inputenc}
\usepackage{tikz}

\begin{document}

\newcommand{\nada}[2] {\fill[lightgray] (#1,#2) circle (0.5);}
\newcommand{\tr}[2]   {\fill[blue] (#1,#2) circle (0.5);}
\newcommand{\ts}[2]   {\fill[red] (#1,#2) circle (0.5);}

\centering

\begin{tikzpicture}[x=4mm,y=4mm]
\foreach \x in {1,...,7} \tr{\x}{0};
\foreach \x in {7,...,9} \ts{\x}{0};
\fill[green] (10,0) circle (0.5);
\end{tikzpicture}

Traditional validation

\vspace{5mm}

\[E\left[\vcenter{\hbox{%
\begin{tikzpicture}[x=4mm,y=4mm]
\foreach \x in {1,...,4}  \tr{\x}{0};
\foreach \x in {5,...,6}  \ts{\x}{0};
\foreach \x in {7,...,10} \nada{\x}{0};
\foreach \x in {1,...,2}  \nada{\x}{1};
\foreach \x in {3,...,6}  \tr{\x}{1};
\foreach \x in {7,...,8}  \ts{\x}{1};
\foreach \x in {9,...,10} \nada{\x}{1};
\foreach \x in {1,...,4}  \nada{\x}{2};
\foreach \x in {5,...,8}  \tr{\x}{2};
\foreach \x in {9,...,10} \ts{\x}{2};
\end{tikzpicture}%
}}\right]\]

Sliding window

\vspace{5mm}

\[E\left[\vcenter{\hbox{%
\begin{tikzpicture}[x=4mm,y=4mm]
\foreach \x in {1,...,7}  \tr{\x}{0};
\foreach \x in {8,...,10} \ts{\x}{0};
\foreach \x in {1,...,4}  \tr{\x}{1};
\foreach \x in {5,...,7}  \ts{\x}{1};
\foreach \x in {8,...,10} \tr{\x}{1};
\foreach \x in {1,...,4}  \ts{\x}{2};
\foreach \x in {5,...,10} \tr{\x}{2};
\end{tikzpicture}%
}}\right]\]

$k$-fold ($k=3$)

\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .