我想将方程式放在 kmap 旁边,但似乎 kmap 的位置较高。我怎样才能将文本和 kmap 对齐,使其从同一高度开始?
\documentclass[12pt]{article}
\usepackage{tikz}
\usetikzlibrary{matrix,calc}
\begin{document}
\tikzset{
table nodes/.style={
rectangle,
draw=black,
align=center,
minimum height=7mm,
text depth=0.5ex,
text height=2ex,
inner xsep=0pt,
outer sep=0pt
},
table/.style={
matrix of nodes,
row sep=-\pgflinewidth,
column sep=-\pgflinewidth,
nodes={
table nodes
},
execute at empty cell={\node[draw=]{0};}
}
}
$a$:
\begin{tikzpicture}
\matrix (kmap) [every node/.style={anchor=base},table,text width=10mm,name=kmap]
{
\node (a11){1}; & \node (a12){}; & \node (a13){1}; & \node (a14){1}; \\
\node (a21){1}; & \node (a22){1}; & \node (a23){}; & \node (a24){1}; \\
\node (a31){1}; & \node (a32){1}; & \node (a33){1}; & \node (a34){1}; \\
\node (a41){}; & \node (a42){1}; & \node (a43){}; & \node (a44){1}; \\
};
\draw (a12.north west) -- ++(0,.3) node (lx3) {};
\draw (a13.north east) -- ++(0,.3) node (rx3) {};
\draw (lx3.north) to node[midway,above]{$x_0$} (rx3.north);
\draw (a21.north west) -- ++(-0.3,0) node (lx4) {};
\draw (a31.south west) -- ++(-0.3,0) node (rx4) {};
\draw (lx4.west) to node[midway,left]{$x_1$} (rx4.west);
\draw (a14.north east) -- ++(0.3,0) node (lx5) {};
\draw (a24.south east) -- ++(0.3,0) node (rx5) {};
\draw (lx5.east) to node[midway,right]{$x_3$} (rx5.east);
\draw (a41.south west) -- ++(0,-.3) node (lx2) {};
\draw (a42.south east) -- ++(0,-.3) node (rx2) {};
\draw (lx2.south) to node[midway,below]{$x_2$} (rx2.south);
\end{tikzpicture}
\begin{tabular}{cccc}
$a$ & $=$ && $x_3 \overline{x_0}$\\
$$ && $+$ & $x_2 x_1$\\
$$ && $+$ & $\overline{x_2}\; \overline{x_1}$\\
$$ && $+$ & $x_3 \overline{x_1}$\\
$$ && $+$ & $x_3 x_2 x_0$\\
$$ && $+$ & $x_3 \overline{x_2}\; \overline{x_1}$\\
\end{tabular}
\end{document}
添加了第二张图片以澄清
答案1
默认情况下,TiKZ 图片的底点与周围文本的基线对齐,但您可以使用baseline
选项更改此行为。例如,如果您想通过中心点对齐它们,可以使用:
\begin{tikzpicture}[baseline=(current bounding box.center), ...
只需更改代码中的此行即可获得以下结果:
完整代码:
\documentclass[12pt]{article}
\usepackage{tikz}
\usetikzlibrary{matrix,calc}
\begin{document}
\tikzset{
table nodes/.style={
rectangle,
draw=black,
align=center,
minimum height=7mm,
text depth=0.5ex,
text height=2ex,
inner xsep=0pt,
outer sep=0pt
},
table/.style={
matrix of nodes,
row sep=-\pgflinewidth,
column sep=-\pgflinewidth,
nodes={
table nodes
},
execute at empty cell={\node[draw=]{0};}
}
}
$a$:
\begin{tikzpicture}[baseline=(current bounding box.center)]
\matrix (kmap) [every node/.style={anchor=base},table,text width=10mm,name=kmap]
{
\node (a11){1}; & \node (a12){}; & \node (a13){1}; & \node (a14){1}; \\
\node (a21){1}; & \node (a22){1}; & \node (a23){}; & \node (a24){1}; \\
\node (a31){1}; & \node (a32){1}; & \node (a33){1}; & \node (a34){1}; \\
\node (a41){}; & \node (a42){1}; & \node (a43){}; & \node (a44){1}; \\
};
\draw (a12.north west) -- ++(0,.3) node (lx3) {};
\draw (a13.north east) -- ++(0,.3) node (rx3) {};
\draw (lx3.north) to node[midway,above]{$x_0$} (rx3.north);
\draw (a21.north west) -- ++(-0.3,0) node (lx4) {};
\draw (a31.south west) -- ++(-0.3,0) node (rx4) {};
\draw (lx4.west) to node[midway,left]{$x_1$} (rx4.west);
\draw (a14.north east) -- ++(0.3,0) node (lx5) {};
\draw (a24.south east) -- ++(0.3,0) node (rx5) {};
\draw (lx5.east) to node[midway,right]{$x_3$} (rx5.east);
\draw (a41.south west) -- ++(0,-.3) node (lx2) {};
\draw (a42.south east) -- ++(0,-.3) node (rx2) {};
\draw (lx2.south) to node[midway,below]{$x_2$} (rx2.south);
\end{tikzpicture}
\begin{tabular}{cccc}
$a$ & $=$ && $x_3 \overline{x_0}$\\
$$ && $+$ & $x_2 x_1$\\
$$ && $+$ & $\overline{x_2}\; \overline{x_1}$\\
$$ && $+$ & $x_3 \overline{x_1}$\\
$$ && $+$ & $x_3 x_2 x_0$\\
$$ && $+$ & $x_3 \overline{x_2}\; \overline{x_1}$\\
\end{tabular}
\end{document}
答案2
例如(在单元格中对齐临时注释,但取消注释):
\documentclass[12pt]{article}
\usepackage{tikz}
\usetikzlibrary{matrix,calc}
\begin{document}
\tikzset{
table nodes/.style={
rectangle,
draw=black,
% align=center,
minimum height=7mm,
text depth=0.5ex,
text height=2ex,
inner xsep=0pt,
outer sep=0pt
},
table/.style={
matrix of nodes,
row sep=-\pgflinewidth,
column sep=-\pgflinewidth,
nodes={
table nodes
},
execute at empty cell={\node[draw=]{0};}
}
}
$a$:
\begin{tabular}{cl} % PS
\begin{tikzpicture}
\matrix (kmap) [every node/.style={anchor=base},table,text width=10mm,name=kmap]
{
\node (a11){1}; & \node (a12){}; & \node (a13){1}; & \node (a14){1}; \\
\node (a21){1}; & \node (a22){1}; & \node (a23){}; & \node (a24){1}; \\
\node (a31){1}; & \node (a32){1}; & \node (a33){1}; & \node (a34){1}; \\
\node (a41){}; & \node (a42){1}; & \node (a43){}; & \node (a44){1}; \\
};
\draw (a12.north west) -- ++(0,.3) node (lx3) {};
\draw (a13.north east) -- ++(0,.3) node (rx3) {};
\draw (lx3.north) to node[midway,above]{$x_0$} (rx3.north);
\draw (a21.north west) -- ++(-0.3,0) node (lx4) {};
\draw (a31.south west) -- ++(-0.3,0) node (rx4) {};
\draw (lx4.west) to node[midway,left]{$x_1$} (rx4.west);
\draw (a14.north east) -- ++(0.3,0) node (lx5) {};
\draw (a24.south east) -- ++(0.3,0) node (rx5) {};
\draw (lx5.east) to node[midway,right]{$x_3$} (rx5.east);
\draw (a41.south west) -- ++(0,-.3) node (lx2) {};
\draw (a42.south east) -- ++(0,-.3) node (rx2) {};
\draw (lx2.south) to node[midway,below]{$x_2$} (rx2.south);
\end{tikzpicture}
& % PS
\begin{tabular}[b]{cccc}
$a$ & $=$ && $x_3 \overline{x_0}$\\
$$ && $+$ & $x_2 x_1$\\
$$ && $+$ & $\overline{x_2}\; \overline{x_1}$\\
$$ && $+$ & $x_3 \overline{x_1}$\\
$$ && $+$ & $x_3 x_2 x_0$\\
$$ && $+$ & $x_3 \overline{x_2}\; \overline{x_1}$\\
\end{tabular}
\end{tabular} % PS
\end{document}
为了更好地对齐,您可以在表格的最后一行后添加空格,例如通过添加可选参数\\
。