
請問如何自動設定列寬並保持對齊?是否可以使用 X 列設定 lc 對齊方式?謝謝
\documentclass{article}
\begin{document}
hdghdggggggggfhgfhdgfhd hggdgfh gfhdgfhjghjhj hgfhdgjghjgd ghfhgdhgfh hgfdhghdgh hfghgdh fghgf fdghgd gfdhd
\begin{table}
\caption[]{Caption}
\label{pyttab}
\small
\centering
\begin{tabular*}{\textwidth}{lccc}
Parameter & AAAA & BBBBBBBBBBBBBB & C\\
Parameter & A & B & C\\
\noalign{\smallskip}\hline\noalign{\smallskip}
\end{tabular*}
\end{table}
\end{document}
答案1
像這樣?
(引入垂直線是為了更簡單地觀察列寬度。在真實表格中,您可以刪除它們)
- 目前尚不清楚「自動」是什麼意思。
- 從顯示的 MWE 可以得出結論,您喜歡具有指定寬度 (=
\textwidth
) 的表格。 - 對於列,
X
您需要使用tabularx
表。在其中您可以使用X
列(至少一個)和為tabular
表定義的列。 - 一個有吸引力的選擇是使用該
tabularray
包。 - 以下是使用
tabularx
和tblr
(在套件中定義tabularray
)表格的範例:
\documentclass{article}
\usepackage{lipsum}
\usepackage{tabularx}
\newcolumntype{C}{>{\centering\arraybackslash}X}
\newcolumntype{L}{>{\raggedright\arraybackslash}X}
\newcolumntype{R}{>{\raggedleft\arraybackslash}X}
\usepackage{tabularray}
\UseTblrLibrary{booktabs}
\begin{document}
\lipsum[1][1-2]
\begin{table}[ht]
\caption[short caption]{Caption}
\label{pyttab}
\begin{tabularx}{\textwidth}{|L|C|c|R|}
\hline % ?
Parameter & AAAA & BBBBBBBBBBBBBB & C\\
Parameter & A & B & C\\
\hline
\end{tabularx}
\end{table}
\lipsum[1][3-5]
\begin{table}[ht]
\caption[short caption]{Caption}
\label{tab:tblr}
\begin{tblr}{colspec={X[l] X[c] c X[r]}, vlines}
\toprule % ?
Parameter & AAAA & BBBBBBBBBBBBBB & C\\
Parameter & A & B & C\\
\bottomrule
\end{tblr}
\end{table}
\end{document}
答案2
{NiceTabular}
的環境為水平對齊nicematrix
提供了列寬可選參數: 、等。X
X[l]
X[c]
\documentclass{article}
\usepackage{geometry}
\usepackage{lipsum}
\usepackage{nicematrix}
\begin{document}
\lipsum[1]
\begin{table}
\caption{Caption}
\label{pyttab}
\small
\centering
\begin{NiceTabular}{X[l]X[c]X[c]X[c]}[hvlines]
Parameter & AAAA & BBBBBBBBB BBBBB BBB BBB & C\\
Parameter & A & B & C\\
\end{NiceTabular}
\end{table}
\end{document}