我有下表:
如您所见,所有元素都与顶部对齐。我希望所有单元格元素都与中心对齐。我最终希望结果看起来像这样。
我的 MWE 是:
\begin{table}[]
\centering
\begin{tabular}{|>{\centering\arraybackslash}p{1.25cm}|>{\centering\arraybackslash}p{2.5cm}|c|c|ccc|c|}\hline
$X^{-1}$S & ${A}$ & {} & $y$&{}&{}&{}&$r_{1}$\\[5ex]\hdashline
$A^T$ & $-Y^{-1}Z$ & {} & $x$&{}&{}&{}&$r_{2}$\\[10ex] \hline
\end{tabular}
\end{table}
我还希望 \hline 和 \hdashline 不要超过几列。
答案1
矩阵也可以在 TikZ 中编写!那里的边框和虚线(如果你坚持的话)很容易绘制:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix, positioning}
\begin{document}
\[
\begin{tikzpicture}[
node distance=0pt,
every node/.style = {minimum height=9mm},
column sep=3mm
]
\matrix (m1) [draw, matrix of math nodes]
{
X^{-1}S & A \\
A^T & -Y^{-1}Z \\
};
\node (m2) [right=of m1] {$\cdot$};
\matrix (m3) [right=of m2,draw, matrix of math nodes]
{
y \\
x \\
};
\node (m4) [right=of m3] {$=$};
\matrix (m5) [right=of m4,draw, matrix of math nodes]
{
r_{1} \\
r_{2} \\
};
\draw[dashed] (m1.west) -- (m1.east) (m1.north) -- (m1.south)
(m3.west) -- (m3.east)
(m5.west) -- (m5.east);
\end{tikzpicture}
\]
\end{document}
附录: 如果您喜欢非对称矩阵布局或用方括号括起来(矩阵的通常符号):
对于这两种情况,上述 MWE 需要进行微小的修改(in code indicated by %<--
):
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix, positioning}
\begin{document}
\[
\begin{tikzpicture}[
node distance=0pt,
every node/.style = {minimum height=9mm},
column sep=3mm,
nodes in empty cells% <-- added
]
\matrix (m1) [draw, matrix of math nodes]
{
X^{-1}S & A \\
& \\% <-- added
A^T & -Y^{-1}Z \\
& \\% <-- added
};
\node (m2) [right=of m1] {$\cdot$};
\matrix (m3) [right=of m2,draw, matrix of math nodes]
{
y \\
\\% <-- added
x \\
\\% <-- added
};
\node (m4) [right=of m3] {$=$};
\matrix (m5) [right=of m4,draw, matrix of math nodes]
{
r_{1} \\
\\% <-- added
r_{2} \\
\\% <-- added
};
\draw[dashed] (m1-1-1.south west) -- (m1-1-1.south -| m1.east)
(m1.north) -- (m1.south)
%
(m3-1-1.south west) -- (m3-1-1.south east -| m3.east)
(m5-1-1.south west) -- (m5-1-1.south east -| m5.east);
\end{tikzpicture}
\]
\[
\begin{tikzpicture}[
node distance=1mm,% <-- changed
every node/.style = {minimum height=11mm},% <-- enlarged
column sep=3mm,
]
\matrix (m1) [matrix of math nodes,%
{left delimiter=[},{right delimiter=]}]% <-- added
{
X^{-1}S & A \\
A^T & -Y^{-1}Z \\
};
\node (m2) [right=of m1] {$\cdot$};
\matrix (m3) [right=of m2,
matrix of math nodes,%
{left delimiter=[},{right delimiter=]}]% <-- added
{
y \\
x \\
};
\node (m4) [right=of m3] {$=$};
\matrix (m5) [right=of m4,
matrix of math nodes,%
{left delimiter=[},{right delimiter=]}]% <-- added
{
r_{1} \\
r_{2} \\
};
\draw[dashed] (m1.west) -- (m1.east) (m1.north) -- (m1.south)
(m3.west) -- (m3.east)
(m5.west) -- (m5.east);
\end{tikzpicture}
\]
\end{document}
答案2
我不会使用tabular
看起来完全处于数学模式的材料环境;相反,我会使用三个独立的并排array
环境。我也不会提供(虚线或者水平线和垂直线(实线) 。这些线只不过是视觉“拐杖”,在处理 2x2 矩阵和 2 元素列向量时似乎几乎不需要。
\arraycolsep
类似下面的内容可能就是您的读者真正需要的(请随意修改和的设置\arraystretch
):
\documentclass{article}
\usepackage{array,amsmath}
\newcolumntype{C}{>{\displaystyle}c}
\begin{document}
\begin{table}[ht!]
\[
\setlength\arraycolsep{10pt} % default: 5pt
\renewcommand\arraystretch{2.5} % default: 1
\begin{array}{|CC|} \hline
X^{-1}S & A \\
A^T & -Y^{-1}Z \\[1ex] \hline
\end{array}
\quad
\begin{array}{|C|}\hline
y \\ x \\[1ex] \hline
\end{array}
\quad{=}\quad
\begin{array}{|C|}\hline
r_{1} \\ r_{2} \\[1ex] \hline
\end{array}
\]
\end{table}
\end{document}
答案3
使用表格环境时,我使用了array
booktabs
multirow
和arydshln
包,并使用了\multirow
\cline
和\cdashline
。我确信这不是最优雅的方式,但它确实很有故事性。
代码:
\documentclass{article}
\usepackage{array}
\usepackage{booktabs}
\usepackage{multirow}
\usepackage{arydshln}
\begin{document}
\begin{table}
\centering
\begin{tabular}{|>{\centering\arraybackslash}p{1cm}:>{\centering\arraybackslash}c|c|c|>{\centering\arraybackslash}p{1.2cm}|c|}
\cline{1-2}
\cline{4-4}
\cline{6-6}
&&&&&\\[1pt]
$X^{-1}$Z & ${A}$ & &$y$&\multirow{6}{*}{=} & $r_{2}$\\[1ex]
\cdashline{1-2}
\cdashline{4-4}
\cdashline{6-6}
&&&&&\\[3ex]
$A^T$ & $-Y^{-1}Z$ & & $x$& & $r_{2}$\\[7ex]
\cline{1-2}
\cline{4-4}
\cline{6-6}
\end{tabular}
\end{table}
\end{document}
得出:
注意:在您的 MWE 中,您在左上角单元格中使用了 S,但在您的“理想”图片中,它使用了 Z。我使用了 Z,但很容易更改。另外,我没有将单元格中的项目(垂直)完全居中,因为它们在“理想”表格图片中没有完全居中。