为什么下面代码中的括号比tikzpicture更大?
\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
您还可以简单地调整图片相对于基线的位置,而无需改变周围的数学环境。这yshift=.5ex
是针对圆圈略低于基线这一事实的调整:
\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}
答案2
您可以先将图片的基线设置为位于中间;然后使用gathered
环境(的更高级别版本\vcenter
)来修复居中。
\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}
之前\!
是\begin{gathered}
为了修复一个“功能”。
当你这样做时\left[...\right]
,栅栏会相对于“公式轴”对称绘制,该轴略高于基线。由于默认情况下,TikZ 图片的基线位于 (0,0) 坐标,因此你会得到所见的效果。
因此,第一次使用盒子中心作为基线居中几乎可以完成工作,然后使用gathered
就完成了。
但是,你可以gathered
通过稍微移动基线来实现精确定位,利用公式轴在基线上方的高度
\fontdimen22\textfont2
相当神秘,不是吗?;-) 因此
\[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]\]
会产生
答案3
您可以使用\vcenter{..}
在数学模式下垂直居中材料。它需要位于内部,例如\hbox
。因此整个语法将是\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}