![自動平衡表格列?](https://rvso.com/image/399824/%E8%87%AA%E5%8B%95%E5%B9%B3%E8%A1%A1%E8%A1%A8%E6%A0%BC%E5%88%97%EF%BC%9F.png)
如何平衡此類聲明的各欄:
\begin{tabularx}{\linewidth}{
>{\hsize=0.4\hsize}X% 10% of 4\hsize
>{\hsize=1.6\hsize}X% 30% of 4\hsize
>{\hsize=0.4\hsize}X% 10% of 4\hsize
>{\hsize=1.6\hsize}X% 30% of 4\hsize
}
\end{tabularx}
顯然這些值0.4
必須1.6
在建置時自動調整...
例子:
.--------- Same width ---------------.
| |
<----+---> <---+---->
┌────────┬────────────────────────────┬────────┬───────────────────────────┐
│ A │ B │ A │ B │
├────────┼────────────────────────────┼────────┼───────────────────────────┤
│ 132132 │ Sed rutrum elit quis neque │ 42 │ Cras porta │
└────────┴────────────────────────────┴────────┴───────────────────────────┘
<------------+---------------> <-------------+------------->
| |
'------------- Same width -------------'
奇數列有大小A
,偶數列有大小嗎B
?這樣我保證第 1 列和第 3 列始終具有相同的大小,並且第 2 列和第 4 列在空間中均勻分佈。
換句話說,列的最小寬度決定如下:
min_widths[0] = min_widths[2] = max(map(width, columns[0] + columns[2]))
min_widths[1] = min_widths[3] = max(map(width, columns[1] + columns[3]))
然後一切都拉伸以適應\linewidth
:
if sum(min_widths) > \linewidth:
% Attempts to wrap some text and reduce the column sizes
else:
widths = map(lambda x: x * (\linewidth / sum(min_widths), min_widths)
此功能的良好參數是:
- Column alignment: left, center, right, raggedleft, raggedright
- Maximum width: e.g. 10em
- Minimum width: e.g. 0em
- Desired width ratio: 1
- Automatic wrapping: yes, no
答案1
{NiceTabular}
這是 (≥ 6.25a)的解nicematrix
。在該環境中,您有一個鍵columns-width=auto
,它要求所有列具有相同的寬度,除了列(與的列X
類似)。X
tabularx
\documentclass{article}
\usepackage{nicematrix}
\usepackage{lipsum} % for dummy text
\begin{document}
\lipsum[1]
\begin{center}
\begin{NiceTabular}{lXlX}[columns-width=auto,hvlines]
A & B & A & B \\
132132 & Sed rutrum elit quis neque & 42 & Cras viverra metus rhoncus sem. Nulla et lectus vestibulum urna
fringilla ultrices.
\end{NiceTabular}
\end{center}
\lipsum[2]
\end{document}
答案2
編輯20190807在答案的最後,我添加了我推薦的代碼,它是紮科和我的答案的合併。
您可能需要透過將 -columns 的寬度計算為 的分數tabular
來使用整體。只要確保列寬總數累加到 即可。您必須減去正確的 s 數量,即每列兩個(四列總共八個)。如果你有垂直規則,你也必須減去正確的s 數。linewidth
p{}
linewidth
linewidth
tabcolsep
arrayrulewidth
我已經「改進」了前面的例子。如果您查看 -column 的定義A
,它現在顯示為:
\newcolumntype{A}{>{\RaggedRight\arraybackslash}p{\dimexpr(1\textwidth)*25/100/2-1.5\tabcolsep\relax}}
- 如果您喜歡居中或右對齊的列,請更改
\RaggedRight
為\Centering
或\RaggedLeft
。 - 如果您需要總寬度小於 full 的表格
linewidth
,請變更(1\linewidth)
為正確的分數。例如,如果您需要 80% 的表格linewidth
,則可以使用(0.8\linewidth)
. - 公式
25/100/2
是-column的相對寬度A
,這裡是25%。該數字旨在設定為使 的總數為newcolumntype
100B
。最後一位數字是- 或- 列75/100
的數量。A
B
- 如果拆下左右側軸承 (
@{}
),則要減去的 s 數tabcolsep
將比總軸承數少 2tabcolsept
。如果你有四列,你必須減去六,如果你有六列,你必須減去十,ETC。公式將是-(2\tabcolsep-(\tabcolsep/<no. col.type>))
.如果您有三A
列,則必須減去-(2\tabcolsep-(\tabcolsep/3))
。 - 如果您使用垂直規則,您也必須正確分配數量
arrayruleswidth
。我把它留給你當作練習。
上面,我假設您將使用相同數量的每種列類型(ABAB、ABBA、ABBBAAETC.)。在自動計算中,您必須考慮到列類型的數量不一定是相同的數量(您可能有 ABBAA、ABABA、ETC.)。所以你很容易得到一個非常複雜的包。也許最好使用計算機或筆和紙來固定列寬。顯然,它比用 LaTeX 編程要快得多。
注意! 我強調這一點tabularx
並且tabulary
已經為你做了所有的數學計算。不過,請查看tabularx
手冊中的 4.3 節。
下面我附上了六列表格的程式碼:
\documentclass{article}
\usepackage{array, booktabs, ragged2e}
\newcolumntype{A}{>{\RaggedRight\arraybackslash}p{\dimexpr(1\textwidth)*25/100/3-(2\tabcolsep-(\tabcolsep/3))\relax}}
\newcolumntype{B}{>{\Centering\arraybackslash}p{\dimexpr(1\textwidth)*75/100/3-(2\tabcolsep-(\tabcolsep/3))\relax}}
\usepackage{showframe}
\renewcommand*\ShowFrameColor{\color{red}}
\renewcommand*\ShowFrameLinethickness{.4pt}
\setlength{\tabcolsep}{2pt}
\begin{document}
{\centering
\begin{tabular}{@{}ABABBA@{}}
\toprule
A & B & A & B & A & B\\
\midrule
Short left alig\-ned column & Wide centered column, which has lots of text & Short left alig\-ned column & Wide centered column, which has lots of text & Wide centered column, which has lots of text & Short left alig\-ned column \\
\bottomrule
\end{tabular}%
}
\end{document}
推薦範例程式碼
\documentclass{article}
\usepackage{booktabs, ragged2e}
\usepackage{tabularx}
\newcolumntype{D}{>{\small\RaggedRight\arraybackslash\hsize=0.4\hsize}X}
\newcolumntype{E}{>{\small\Centering\arraybackslash\hsize=1.6\hsize}X}
\setlength{\tabcolsep}{1.5pt}
\begin{document}
\noindent\begin{tabularx}{\linewidth}{@{}DEDEED@{}}
\toprule
D & E & D & E & E & D\\
\midrule
Short left alig\-ned column & Wide centered column, which has lots of text & Short left alig\-ned column & Wide centered column, which has lots of text & Wide centered column, which has lots of text & Short left alig\-ned column \\
\bottomrule
\end{tabularx}
\end{document}
答案3
據我了解這個問題,表格的寬度應該等於文字的寬度。所以,我沒有看到任何優勢(也許是因為我的假期?)為什麼不是tabularx
按照我在下面的評論中建議的問題使用:
\documentclass{article}
\usepackage{tabularx}
\begin{document}
\begin{table}[ht]
\begin{tabularx}{\linewidth}{|*{2}{>{\hsize=0.4\hsize}X| >{\hsize=1.6\hsize}X|} }
\hline
xxx xxx & xxx xxx xxx xxx xxx xxx xxx xxx
& xxx xxx & xxx xxx xxx xxx xxx xxx xxx xxx\\
\hline
\end{tabularx}
\caption{Four column}
\end{table}
\begin{table}[ht]
\begin{tabularx}{\linewidth}{|*{3}{>{\hsize=0.4\hsize}X| >{\hsize=1.6\hsize}X|} }
\hline
xxx xxx & xxx xxx xxx xxx xxx xxx xxx xxx
& xxx xxx & xxx xxx xxx xxx xxx xxx xxx xxx
& xxx xxx & xxx xxx xxx xxx xxx xxx xxx xxx\\
\hline
\end{tabularx}
\caption{Sixt column}
\end{table}
\end{document}
(紅線表示文字邊框)