
如何在表格環境中調整列間距並使內容居中?
我使用的程式碼是這樣的:
\documentclass[12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{unicode-math}
\begin{document}
\begin{center}
\begin{tabular}{|p{8cm}|p{2cm}|}
\hline
1 & 2\\
\hline
3 & 4\\
\hline
\end{tabular}
\end{center}
\end{document}
編輯一:
另外,當我在該程式碼中嘗試在第一個表下建立第二個表時,兩個表的寬度都相同,但最終第二個表更大,儘管我給出的寬度是相同的。為什麼會發生這種情況?
\documentclass[12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{unicode-math}
\begin{document}
\begin{center}
\begin{tabular}{|p{8cm}|p{2cm}|}
\hline
1 & 2\\
\hline
3 & 4\\
\hline
\end{tabular}
\begin{tabular}{|p{3cm}|p{3cm}|p{4cm}|}
\hline
1 & 2 & 3\\
\hline
4 & 5 & 6\\
\hline
\end{tabular}
\end{center}
\end{document}
答案1
您可以使用以下命令將單一條目(實際上只是一個\parbox
)居中
1 &\centering 2 & 3\\
但是\centering
重新定義\\
,因此在最後一列中您需要使用\tabularnewline
1 &\centering 2 &\centering 3\tabularnewline
通常,雖然您想對整個列執行此操作,但請使用套件array
,然後使用列規範
>{\centering\arraybackslash}p{3cm}
在該列的每個條目中套用居中並重新定義\\
以結束表格行。
在第一個表中,我將單一條目居中,4
在第二個表中,我將所有條目居中在中間列中。兩張桌子都是10公分寬。
\documentclass[12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{array,calc}
\begin{document}
\begin{center}
\begin{tabular}{|p{8cm-2\tabcolsep-2\arrayrulewidth}|
p{2cm-2\tabcolsep-\arrayrulewidth}|}
\hline
1111 & 2222222\\
\hline
3 & \centering\arraybackslash 4\\
\hline
\end{tabular}
\begin{tabular}{|p{3cm-2\tabcolsep-\arrayrulewidth}|
>{\centering\arraybackslash}p{3cm-2\tabcolsep-\arrayrulewidth}|
p{4cm-2\tabcolsep-2\arrayrulewidth}|}
\hline
111 & 2222 & 3333\\
\hline
4 & 5 & 6\\
\hline
\end{tabular}
\end{center}
\end{document}