我试图将表格内容垂直和水平居中。表格包含Bmatrix
和bmatrix
元素。只有第一列完全居中。第二列仅水平居中,尽管我使用设置了两列m{...}
:
\begin{table}[h]
\centering
\begin{tabular}{|>{\centering\arraybackslash}m{5cm}|>{\centering\arraybackslash}m{5cm}|}
\hline
\textbf{Set Representation} & \textbf{Binary Representation} \\ \hline
$U = \begin{Bmatrix} 1 & 2 & 3 & 4 \end{Bmatrix}$ & $U = \begin{bmatrix} 1 & 1 & 1 & 1 \end{bmatrix} = 15$ \\[.5cm] \hline
$S_1 = \begin{Bmatrix} 1 & 2 \end{Bmatrix}$ & $S_1 = \begin{bmatrix} 0 & 0 & 1 & 1 \end{bmatrix} = 3$ \\[.5cm] \hline
$S_2 = \begin{Bmatrix} 1 \end{Bmatrix}$ & $S_2 = \begin{bmatrix} 0 & 0 & 0 & 1 \end{bmatrix} = 1$ \\[.5cm] \hline
$S_3 = \begin{Bmatrix} 1 & 3 \end{Bmatrix}$ & $S_3 = \begin{bmatrix} 0 & 1 & 0 & 1 \end{bmatrix} = 5$ \\[.5cm] \hline
$S_4 = \begin{Bmatrix} 4 \end{Bmatrix}$ & $S_4 = \begin{bmatrix} 1 & 0 & 0 & 0 \end{bmatrix} = 8$ \\[.5cm] \hline
\end{tabular}%\quad}
\caption{\textbf{Conversion from set to binary representation}}\label{table:setToBin}
\end{table}
答案1
借助以下makecell
包:
\documentclass{article}
\usepackage{amsmath}
\usepackage{makecell}
\usepackage[skip=1ex, font=bf]{caption}
\begin{document}
\begin{table}[htb]
\[\setcellgapes{5pt}\makegapedcells % <--- for vertical space around cells contents
\begin{array}{|c|c|}
\hline
\textbf{Set Representation} & \textbf{Binary Representation} \\
\hline
U = \begin{Bmatrix} 1 & 2 & 3 & 4 \end{Bmatrix}
& U = \begin{bmatrix} 1 & 1 & 1 & 1 \end{bmatrix} = 15 \\
\hline
S_1 = \begin{Bmatrix} 1 & 2 \end{Bmatrix}
& S_1 = \begin{bmatrix} 0 & 0 & 1 & 1 \end{bmatrix} = 3 \\
\hline
S_2 = \begin{Bmatrix} 1 \end{Bmatrix}
& S_2 = \begin{bmatrix} 0 & 0 & 0 & 1 \end{bmatrix} = 1 \\
\hline
S_3 = \begin{Bmatrix} 1 & 3 \end{Bmatrix}
& S_3 = \begin{bmatrix} 0 & 1 & 0 & 1 \end{bmatrix} = 5 \\
\hline
S_4 = \begin{Bmatrix} 4 \end{Bmatrix}
& S_4 = \begin{bmatrix} 1 & 0 & 0 & 0 \end{bmatrix} = 8 \\
\hline
\end{array}%
\]
\caption{Conversion from set to binary representation}
\label{table:setToBin}
\end{table}
\end{document}
答案2
根据答案:这里您可以使用@StevenB.Segletes包\belowbaseline
的命令stackengine
:
\documentclass[12pt,a4paper]{article}
\usepackage{amsmath}
\usepackage{array}
\usepackage{stackengine}
\begin{document}
\begin{table}[h]
\centering
\begin{tabular}{|>{\centering\arraybackslash}m{5cm}|>{\centering\arraybackslash}m{5cm}|}
\hline
\textbf{Set Representation} & \textbf{Binary Representation}\\ \hline
$U = \begin{Bmatrix} 1 & 2 & 3 & 4 \end{Bmatrix}$ & \belowbaseline[-3pt]{$U = \begin{bmatrix} 1 & 1 & 1 & 1 \end{bmatrix} = 15$ } \\[.5cm] \hline
$S_1 = \begin{Bmatrix} 1 & 2 \end{Bmatrix}$ & \belowbaseline[-3pt]{$S_1 = \begin{bmatrix} 0 & 0 & 1 & 1 \end{bmatrix} = 3$ } \\[.5cm] \hline
$S_2 = \begin{Bmatrix} 1 \end{Bmatrix}$ & \belowbaseline[-3pt]{$S_2 = \begin{bmatrix} 0 & 0 & 0 & 1 \end{bmatrix} = 1$ } \\[.5cm] \hline
$S_3 = \begin{Bmatrix} 1 & 3 \end{Bmatrix}$ &\belowbaseline[-3pt]{ $S_3 = \begin{bmatrix} 0 & 1 & 0 & 1 \end{bmatrix} = 5$ } \\[.5cm] \hline
$S_4 = \begin{Bmatrix} 4 \end{Bmatrix}$ & \belowbaseline[-3pt]{$S_4 = \begin{bmatrix} 1 & 0 & 0 & 0 \end{bmatrix} = 8$ } \\[.5cm] \hline
\end{tabular}%\quad}
\caption{\textbf{Conversion from set to binary representation}}\label{table:setToBin}
\end{table}
\end{document}
答案3
我只能使用超大分隔符来重现矩阵
\linespread{1.667}
和
\usepackage[doublespacing]{setspace}
分隔符具有正确的大小。
您可以在第一列插入一个支柱;此外,w{c}{5cm}
如果您想要固定大小,我建议使用,而不是说明m
符,但我不认为这合理,因此我建议使用不同的实现booktabs
。
\documentclass{article}
\usepackage{amsmath,array}
\usepackage{booktabs} % for the second table
\usepackage{caption}
\usepackage[doublespacing]{setspace}
\usepackage{lipsum} % for mock text
\captionsetup{font=bf}
\newcommand{\bs}{\vphantom{$\Big|$}}
\begin{document}
\lipsum[3]
\begin{table}[!htp]
\centering
\begin{tabular}{| >{\bs}w{c}{5cm} | w{c}{5cm} |}
\hline
\textbf{Set Representation} & \textbf{Binary Representation} \\
\hline
$U = \begin{Bmatrix} 1 & 2 & 3 & 4 \end{Bmatrix}$ &
$U = \begin{bmatrix} 1 & 1 & 1 & 1 \end{bmatrix} = 15$ \\
\hline
$S_1 = \begin{Bmatrix} 1 & 2 \end{Bmatrix}$ &
$S_1 = \begin{bmatrix} 0 & 0 & 1 & 1 \end{bmatrix} = 3$ \\
\hline
$S_2 = \begin{Bmatrix} 1 \end{Bmatrix}$ &
$S_2 = \begin{bmatrix} 0 & 0 & 0 & 1 \end{bmatrix} = 1$ \\
\hline
$S_3 = \begin{Bmatrix} 1 & 3 \end{Bmatrix}$ &
$S_3 = \begin{bmatrix} 0 & 1 & 0 & 1 \end{bmatrix} = 5$ \\
\hline
$S_4 = \begin{Bmatrix} 4 \end{Bmatrix}$ &
$S_4 = \begin{bmatrix} 1 & 0 & 0 & 0 \end{bmatrix} = 8$ \\
\hline
\end{tabular}
\caption{Conversion from set to binary representation}\label{table:setToBin}
\end{table}
\begin{table}[!htp]
\centering
\begin{tabular}{ cc }
\toprule
\textbf{Set Representation} & \textbf{Binary Representation} \\
\midrule
$U = \begin{Bmatrix} 1 & 2 & 3 & 4 \end{Bmatrix}$ &
$U = \begin{bmatrix} 1 & 1 & 1 & 1 \end{bmatrix} = 15$ \\
\addlinespace
$S_1 = \begin{Bmatrix} 1 & 2 \end{Bmatrix}$ &
$S_1 = \begin{bmatrix} 0 & 0 & 1 & 1 \end{bmatrix} = 3$ \\
\addlinespace
$S_2 = \begin{Bmatrix} 1 \end{Bmatrix}$ &
$S_2 = \begin{bmatrix} 0 & 0 & 0 & 1 \end{bmatrix} = 1$ \\
\addlinespace
$S_3 = \begin{Bmatrix} 1 & 3 \end{Bmatrix}$ &
$S_3 = \begin{bmatrix} 0 & 1 & 0 & 1 \end{bmatrix} = 5$ \\
\addlinespace
$S_4 = \begin{Bmatrix} 4 \end{Bmatrix}$ &
$S_4 = \begin{bmatrix} 1 & 0 & 0 & 0 \end{bmatrix} = 8$ \\
\bottomrule
\end{tabular}
\caption{Conversion from set to binary representation}\label{table:setToBin2}
\end{table}
\end{document}
\textbf
最后说明:的主体中不应有\caption
;这种格式决定最好在caption
包的全局基础上进行。