
如何垂直居中前三列,使這些列中的所有值都位於中心?
\begin{center}
\begin{tabular}{|c| c |c| p{5cm}|}
\hline
\rowcolor[gray]{.8}Day & Min Temp & Max Temp & \multicolumn{1}{c|}{ Summary} \\ \hline
Monday & 11C & 22C & A clear day with lots of sunshine.
However, the strong breeze will bring down the temperatures \\ \hline
Tuesday & 9C & 19C & Cloudy with rain, across many northern regions.
Clear spells across most of Scotland and Northern Ireland, but rain
reaching the far northwest \\ \hline
Wednesday & 10C & 21C & Rain will still linger for the morning.
Conditions will improve by early afternoon and continue throughout the evening. \\ \hline
\end{tabular}
\end{center}
它應該看起來像這樣:
編輯:
這是我從評論中粘貼的問題中嘗試的:
\begin{center}
\renewcommand{\tabularxcolumn}[1]{m{#1}}
\begin{tabularx}{|m{1in}|c|c| p{5cm}|}
\hline
\rowcolor[gray]{.8}Day & Min Temp & Max Temp & \multicolumn{1}{c|}{ Summary} \\ \hline
Monday & 11C & 22C & A clear day with lots of sunshine. However, the strong breeze will bring down the temperatures \\ \hline
Tuesday & 9C & 19C & Cloudy with rain, across many northern regions. Clear spells across most of Scotland and Northern Ireland, but rain reaching the far northwest \\ \hline
Wednesday & 10C & 21C & Rain will still linger for the morning. Conditions will improve by early afternoon and continue throughout the evening. \\ \hline
\end{tabularx}
\end{center}
它仍然不起作用。
答案1
歡迎來到 TeX.SE!
請考慮下次發布完整的 MWE。我在答案中添加了一些缺少的部分,但錯過了標題行中的灰色背景顏色。將此視為您的作業;-)
LaTeX 基本上知道兩種類型的列。一種類型,LaTeX 計算列本身的寬度。字母l
,c
和r
代表這種類型的列。每當作者向其中添加文字時,列的寬度就會「增長」。
另一方面,我們確實有列的類型,作者定義了寬度。該列類型的識別碼是例如p{<width>}
。您已將其用於最後一列並p{5cm}
在 MWE 中進行了定義。該柱寬 5 公分。如果作者(即您!)添加更多寬度超過 5 厘米的文本,LaTeX 將中斷文本並開始新行。結果列將以兩端對齊的文字格式設置,如範例所示。
為了得到你想要的結果,你要做的就是
- 載入
array
包 - 使用
m{<width>}
列類型代替c
或p{<width>}
列
當然,這只是事實的一半。
該array
軟體包為您提供了m
和b
列類型。它們的作用類似於p
列,使用者必須定義其寬度。 LaTeX 會斷行並產生合理的文字。這些列類型之間的差異在於垂直對齊。經驗法則是:
p
對齊到磷,m
對齊於中號中間和b
對齊於乙奧托姆。
(請閱讀陣列手冊,以了解確切的行為!)
m
因此,在表格中使用某種類型的列將按照您的要求將所有行垂直居中對齊。
但我們還必須解決水平對齊問題。這些列的行為與我們剛剛替換的列m
相同。p
在最後一列中,這種行為是正常的。但在第一列中,我們希望文本也水平居中。
這是軟體包提供的下一個技巧array
。您可以使用>{cmd}
.將 可視>
化為箭頭,它將cmd
大括號內的 推到其旁邊的列。如果您在表格中定義類似的內容
\begin{tabular}{ l >{\raggedright}p{5cm} l }
您將p
在兩個常用列之間得到一個 5 公分寬的列,l
作為該表的第一列和最後一列。
但每次您輸入第二列時,都會>{\raggedright}
將命令推\raggedright
入該列。m
此命令會將第二列中的對齊方式變更為左側齊平,右側不齊整。
除了上述定義之外,您還可以輸入
\begin{tabular}{l p{5cm} l}
first cell & \raggedright lots of text in the second cell, which is flush left & third cell \\
next line, first cell & \raggedright again lots of text ... & third cell \\
new line first cell & \raggedright ... & ... \\
once again & \raggedright ... & end \\
\end{tabular}
但在這種情況下,每次進入第二列時都必須輸入命令\raggedright
,這可能很快就會變得煩人。>{\raggedright}
先前的定義中的技巧會自動為您完成此操作,而不是每次輸入該列時都鍵入它。對我來說聽起來很有希望。
要獲得真正乾淨的 LaTeX 程式碼,您應該添加\arraybackslash\hspace{0pt}
到上面的範例中。但是這個巨大的字串將使您的輸入代碼無法讀取。所以我提出下一個技巧。
該array
軟體包還提供了該\newcolumntype
命令。您應該在文件的序言中使用它(即之前\begin{document}
)。只需選擇一個字母作為新的列類型。您要求使用居中類型的列。因此我建議使用字母“C”,它之前沒有定義。
所以最簡單的解決方案是
\newcolumntype{C}{>{\raggedright\arraybackslash\hspace{0pt}}m{5cm}}
並將所有列定義(c
和p{5cm}
)替換為這種全新且閃亮的C
列類型。
\begin{tabular}{|C|C|C|C|}
有時,這樣簡單的解決方案有一些重大缺點。在您的具體情況下,表格中的每一列現在都是 5 公分寬。我想,這不完全是你想要的,對吧?前三列不需要 5 公分寬。我們要怎麼解決這個問題呢?
這是我的下一個增強功能。在第一行中,最寬的條目似乎是“星期三”一詞。因此,讓 LaTeX 確定其寬度並將其用作第一列的寬度。為此,您可以使用該命令\settowidth
來測量單字的寬度。但我們還必須更改 的新列定義C
。我們必須以更靈活的方式來定義它。透過仔細檢查,應該意識到,該C
列應該具有可變的寬度,正如p{<width>}
所發生的那樣。該p
柱有一對支撐。因此,讓我們將其添加到我們的C
專欄中。
\newcolumntype{C}[1]{>{\raggedright\arraybackslash\hspace{0pt}}m{#1}}
看到不同?我[1]
在定義的開頭添加了。這意味著該C
列必須有一對支撐。該對中給出的值在#1
我的定義末尾用 表示。這為我們提供了所需的靈活性。
完整的 MWE 現在看起來像這樣:
\documentclass{article}
\usepackage{array} % better control for tabular
%% Define the length of the longest day name
\newlength{\daylength}
\settowidth{\daylength}{Wednesday}
%% Create a new columntype, where the text is vertically and
%% horizontally centered. The new identifier should be "C".
\newcolumntype{C}[1]{%
>{\centering\arraybackslash\hspace{0pt}}m{#1}}
\begin{document}
\begin{center}
\begin{tabular}{|C{\daylength}| C{2cm} | C{2cm} | m{5cm} |}
\hline
Day & Min Temp & Max Temp & \multicolumn{1}{c|}{ Summary} \\
\hline
Monday & 11C & 22C & A clear day with lots of sunshine.
However, the strong breeze will bring down the temperatures \\
\hline
Tuesday & 9C & 19C & Cloudy with rain, across many northern regions.
Clear spells across most of Scotland and Northern Ireland, but rain
reaching the far northwest \\
\hline
Wednesday & 10C & 21C & Rain will still linger for the morning.
Conditions will improve by early afternoon and continue throughout the evening. \\
\hline
\end{tabular}
\end{center}
\end{document}
並產生此輸出
就我個人而言,我還會定義一種新R
類型的列,它有助於左對齊列,因為這種小列在合理時看起來不太好。我會把它加到你的家庭作業中。 :-)
編輯
\arraybackslash
再次使您能夠用於\\
結束表格行並開始新行。 \hspace{0pt}
使 LaTeX 能夠開始插入第一個單詞,這在非常小的列中很重要。