答案1
您可以使用例如
\documentclass{article}
\usepackage{tikz}
\tikzset{labeled square/.cd,a/.initial=pi*1em,b/.initial=2pt,}
\newcommand{\LabeledSquare}[5][]{\begin{tikzpicture}[baseline={(W.base)},
thick,nodes={inner sep=0pt,text height=0.8em,text depth=0.25ex},#1]
\draw (0,0) -- node[pos=0.5,left=\pgfkeysvalueof{/tikz/labeled square/b}](W) {$#2$} ++ (0,-\pgfkeysvalueof{/tikz/labeled square/a})
-- node[pos=0.5,below=\pgfkeysvalueof{/tikz/labeled square/b}] {$#3$} ++ (\pgfkeysvalueof{/tikz/labeled square/a},0)
-- node[pos=0.5,right=\pgfkeysvalueof{/tikz/labeled square/b}] {$#4$} ++ (0,\pgfkeysvalueof{/tikz/labeled square/a})
-- node[pos=0.5,above=\pgfkeysvalueof{/tikz/labeled square/b}] {$#5$} cycle;
\end{tikzpicture}}
\begin{document}
\[
\LabeledSquare{T_3}{T_2}{T_1}{T_0}
=
\LabeledSquare{0}{0}{0}{T_0}
+
\LabeledSquare{0}{0}{T_1}{0}
+
\LabeledSquare{0}{T_2}{0}{0}
+
\LabeledSquare{T_3}{0}{0}{0}
\]
\tikzset{labeled square/a=4em}
\[
\LabeledSquare{T_3}{T_2}{T_1}{T_0}
=
\LabeledSquare[red]{0}{0}{0}{T_0}
+
\LabeledSquare[orange]{0}{0}{T_1}{0}
+
\LabeledSquare[blue]{0}{T_2}{0}{0}
+
\LabeledSquare[cyan]{T_3}{0}{0}{0}
\]
\end{document}
为了确保形状与方程完美融合,我们使用baseline
左边缘标签的来设置的基线tikzpicture
。为了避免对边标签偏离,我们为text height
和设置了一些(经验)值text depth
。如您所见,您可以使用\tikzset
或使用可选参数(或两者)更改所有这些参数。