http://en.wikibooks.org/wiki/LaTeX/Tables包含如何使用 p{5cm} 在表格中实现文本换行的示例:
\begin{center}
\begin{tabular}{ | l | l | l | p{5cm} |}
\hline
Day & Min Temp & Max Temp & 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{table}[h]
\centering
\resizebox{\textwidth}{!}{%
\begin{tabular}{p{20cm}}
\rowcolor[HTML]{3166FF}
\pbox{
\Huge Line 1 of my long title
\\ \Huge Line2 of my long title} \\ \hline
\huge Sub title
\end{tabular}
}
\end{table}
我想从 Word 中实现这样的效果:
我已经接近了,但在 LaTeX 中看起来仍然不够专业:
\begin{table}[h]
\centering
\resizebox{\textwidth}{!}{%
\begin{tabular}{p{13cm}}
\rowcolor[HTML]{3166FF}
\centering \Huge This is a very long title that spans multiple lines \\ \hline
\centering \huge Sub title
\end{tabular}
}
\end{table}
答案1
\newcolumntype
从包中使用array
并定义一种样式,比如C
自动包装和居中。
定义新的列类型使用现有的类型之一,即l
、r
或p
。c
语法是(带参数)
\newcolumntype{Y}[1]{>{some stuff before\arraybackslash}Z{#1}}
,其中 Y 代表新字母,Z
是已经存在的类型之一。
就在 之前\arraybackslash
,基本上任何东西都可以插入,比如\centering
,所以这将在 Z 型里面使用。
编辑一些改善表格外观的建议。
\documentclass{article}
\usepackage{siunitx}
\usepackage{booktabs}
\usepackage{array}
\newcolumntype{C}[1]{>{\centering\arraybackslash}p{#1}}
\begin{document}
\begin{center}
\begin{tabular}{l*{2}{C{2cm}}C{5cm}}
\multicolumn{4}{c}{ \large \textbf{ The good table...}} \tabularnewline[2ex]
\toprule
Day & Min Temp (\si{\degreeCelsius}) & Max Temp (\si{\degreeCelsius}) & Summary \tabularnewline
\midrule
Monday & 11 & 22 & A clear day with lots of sunshine.
However, the strong breeze will bring down the temperatures. \tabularnewline[1ex]
Tuesday & 9 & 19 & Cloudy with rain, across many northern regions. Clear spells
across most of Scotland and Northern Ireland,
but rain reaching the far northwest. \tabularnewline[1ex]
Wednesday & 10 & 21 & Rain will still linger for the morning.
Conditions will improve by early afternoon and continue
throughout the evening. \tabularnewline[1ex]
\bottomrule
\end{tabular}
\end{center}
\begin{center}
\begin{tabular}{ | l | l | l | C{5cm} |}
\multicolumn{4}{c}{ \large \textbf{ and the bad and ugly...}} \tabularnewline[2ex]
\hline
Day & Min Temp & Max Temp & Summary \tabularnewline \hline
Monday & 11C & 22C & A clear day with lots of sunshine.
However, the strong breeze will bring down the temperatures. \tabularnewline \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. \tabularnewline \hline
Wednesday & 10C & 21C & Rain will still linger for the morning.
Conditions will improve by early afternoon and continue
throughout the evening. \tabularnewline
\hline
\end{tabular}
\end{center}
\end{document}