1 2 3 4 5 \begin {tabular}[pos]{cols} table content\end {tabular}
where options can be:
pos
: Vertical position. It can assume the following values:t
the line at the top is aligned with the text baseline (可以理解为一横文字的底部基线, 所有文字在这个基线上排布) b
the line at the bottom is aligned with the text baseline c
or nonethe table is centred to the text baseline
cols
: Defines the alignment and the borders of each column. It can have the following values:l
left-justified column c
centred column r
right-justified column p{'width'}
paragraph column with text vertically aligned at the top m{'width'}
paragraph column with text vertically aligned in the middle (requires array
package) b{'width'}
paragraph column with text vertically aligned at the bottom (requires array
package) |
vertical line ||
double vertical line *{num}{form}
the format form is repeated num times; for example *{3}{|l}|
is equal to |l|l|l|
To separate between cells and introducing new lines use the following commands:
&
column separator \\\\
start new row (additional space may be specified after \\
using square brackets, such as \\[6pt]
) \\hline
horizontal line between rows \\newline
start a new line within a cell (in a paragraph column) \\cline{i-j}
partial horizontal line beginning in column i and ending in column j
1. 用table制作封面:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 \title {\makebox { \begin {tabular}{c} \includegraphics [width=\textwidth ]{img/nankai.jpg} \\ \textbf {课程报告} \\ xxxxxx \end {tabular} } }\author {\makebox { \begin {tabular}{cc} 姓名: & xxxx \\ 学号: & xxxx \end {tabular} }}\date {\today }
2. 将表格进行整体缩放(并不推荐, 除非迫不得已)1 2 3 4 5 6 7 8 9 10 \resizebox {\textwidth }{15mm}{ \begin {tabular}{|c|c|} \hline \ & 平均运行时间(单位: s) \\ \hline 训练阶段 & 2333.5593 \\ 测试阶段 & \ \\ \hline \end {tabular} }
3. 整体调整每一列宽度1 2 3 4 5 6 7 8 9 10 11 12 13 14 \setlength {\tabcolsep }{7mm}{ \begin {tabular}{cccccc} \toprule Models & $ \hat c$ & $ \hat \alpha $ & $ \hat \beta _ 0$ & $ \hat \beta _ 1$ & $ \hat \beta _ 2$ \\ \hline model & 30.6302 & 0.4127 & 9.4257 & - & - \\ model & 12.4089 & 0.5169 & 18.6986 & -6.6157 & - \\ model & 14.8586 & 0.4991 & 19.5421 & -7.0717 & 0.2183 \\ model & 3.06302 & 0.41266 & 0.11725 & - & - \\ model & 1.24089 & 0.51691 & 0.83605 & -0.66157 & - \\ model & 1.48586 & 0.49906 & 0.95609 & -0.70717 & 0.02183 \\ \bottomrule \caption {哈哈哈} \end {tabular} }
4. 设置列高\rule{线的宽度}{线撑起的高度}
线设置为0pt宽度, 就可以起到撑起单元格的作用了
1 2 3 4 5 6 7 8 \begin {tabular}{|c|c|} \hline \ & 平均运行时间(单位: s) \\ \hline \rule {0pt}{8pt}训练阶段 & 2333.5593 \\ 测试阶段 & \ \\ \hline \end {tabular}
5. 三线表1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 \documentclass {article} \usepackage {float}\usepackage {booktabs} \begin {document} \begin {table}[H] \caption {\textbf {Example 1}} \centering \begin {tabular}{c{80pt}c{80pt}c{80pt}c{80pt}} \toprule Item 1& Item 2& Item 3& Item 4 \\ \midrule Data1& Data2& Data3& Data4 \\ Data5& Data6& Data7& Data8 \\ \bottomrule \end {tabular} \end {table} \end {document}
6. 复杂表格多列表格中用\cline
控制哪几列有横线: \cline{3-5}
表示第3列和第5列有横线.
1 2 3 4 5 \begin {tabular}{llrr} a & b & c & d \\ \cline {1-1} \cline {3-4} e & f & g & h \end {tabular}
如果用了booktabs
, 想要\cline
类似的多列横线, 则可以使用\cmidrule
, 这个比\midrule
更细
\usepackage{multirow}
\multirow{nrows}[bigstruct]{width}[fixup]{text}
nrows: 占用的行数 bigstructs: 可选, 在使用了bigstruct宏包时用 width: 该栏宽度, 如果为*则由LaTex自行决定 fixup: 可选项, 调整文本的垂直位置 text: 所要排版的文本, 可以用 \ 强制换行 1 2 3 4 5 6 7 8 9 10 \begin {tabular}{|c|c|c|c|c|} \hline \multirow {2}{*}{1} & 11 & 111 & 1111 & 11111 \\ \cline {2-5} & 22 & 222 & 2222 & 22222 \\ \hline 3 & 33 & 333 & 3333 & 33333 \\ \hline \end {tabular}
\multicolumn{ncols}{指明对齐方式以及方格线}{单元格内容}
1 2 3 4 5 6 7 8 9 10 11 \begin {tabular}{|c|c|c|c|c|} \hline \multicolumn {2}{|c|}{1} & 111 & 1111 & 11111 \\ \hline 2 & 22 & 222 & 2222 & 22222 \\ \hline 3 & 33 & 333 & 3333 & 33333 \\ \hline \end {tabular}
7. 表格的列方向上分散对齐\makebox[width][position]{text}
\makebox[4em][s]{content}
l - left r - right s - interword space adjusted so text fills box exactly 1 2 3 4 5 6 \begin {tabular}{rl} \makebox [4em][s]{论文题目}: & \@title \\ \makebox [4em][s]{专\hspace {\fill }业}:& \@major \\ \makebox [4em][s]{博\hspace {\fill }士\hspace {\fill }生}: & \@author \\ \makebox [4em][s]{指导教师}: & \@advisorname \, \, \@advisortitle \end {tabular}
表格内换行 \makecell
1 2 3 4 5 6 7 \usepackage {makecell}\begin {tabular}{cc} \makecell [l]{a \\ b \\ c } & 1 \\ \end {tabular}
除了使用\makecell
外, 还可以用嵌套tabular的方式
1 2 3 4 5 6 7 8 9 10 11 \begin {tabular}{cccc} \toprule \begin {tabular}{c} 物理机\\ 资源类型 \end {tabular} & CPU资源(单位:核) & 内存资源(单位:G) & 机器数量 \\ \hline 类型一 & 32 & 64 & 6000 \\ 类型二 & 92 & 288 & 3000 \\ \bottomrule \end {tabular}
设置合理列宽 需要导入makecell
包
1 2 3 4 5 6 7 8 9 10 11 12 13 14 \begin {table }[ htbp] \centering \begin {tabular}{p{80 pt}p{80 pt}p{80 pt}} \toprule 操作系统 & 发行版 & 编辑器 \\ \midrule Windows & MikTeX & TexMakerX \\ Unix/Linux & teTeX & Kile \\ Mac OS & MacTeX & TeXShop \\ 通用 & TeX Live & TeXworks \\ \bottomrule \end {tabular}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 \begin {table }[ htbp] \centering \begin {tabular }{p{80 pt}<{\centering }p{80 pt}<{\raggedleft \arraybackslash }p{80 pt}} \toprule 操作系统 & 发行版 & 编辑器 \\ \midrule Windows & MikTeX & TexMakerX \\ Unix/Linux & teTeX & Kile \\ Mac OS & MacTeX & TeXShop \\ 通用 & TeX Live & TeXworks \\ \bottomrule \end {tabular}\end {table}
9. 将表旋转90度显示使用宏包rotating
将table
环境改为sidewaytable
\usepackage[figuresleft]{rotating}
: 顺时针90度\usepackage[figuresright]{rotating}
: 逆时针90度1 2 3 4 5 6 7 8 9 10 11 12 \usepackage [figuresright]{rotating}\begin {sidewaystable}[htp] \caption {This is a caption} \centering \footnotesize \begin {tabular}{l*{7}{L{3cm}}} \end {tabular}\end {sidewaystable}
multicolumn 小结m{5cm}
: 居中p{5cm}
: 顶部b{5cm}
: 底部
1. 垂直居中1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 \begin {center}\begin {tabular}{|l|l|l|l|}\hline Day & Min Temp & Max Temp & Summary \\ \hline Monday & 11C & 22C & \multicolumn {1}{|p{5cm}|}{A clear day with lots of sunshine. However, the strong breeze will bring down the temperatures.} \\ \hline Tuesday & 9C & 19C & \multicolumn {1}{|m{5cm}|}{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 & \multicolumn {1}{|b{5cm}|}{Rain will still linger for the morning. Conditions will improve by early afternoon and continue throughout the evening.} \\ \hline \end {tabular}\end {center}
2. 垂直且水平居中1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 \documentclass [a4paper,twoside,12pt]{article}\usepackage {amsbsy}\usepackage {graphicx}\usepackage {array}\usepackage {calc}\newcolumntype {N}{>{\centering \arraybackslash }m{.5in}} \newcolumntype {G}{>{\bfseries \centering \arraybackslash }m{2in+6\tabcolsep }}\begin {document}\begin {table}[H]\centering \begin {tabular}{*{9}{N|}}\cline {2-9}\multicolumn {1}{N|}{} & \multicolumn {4}{G|}{Emissions (Pre Catalytic Converter)} & \multicolumn {4}{G|}{Emissions (Post Catalytic Converter)} \tabularnewline \hline \multicolumn {1}{|N|}{\textbf {Dyno Load (\% )}} & \textbf {CO (\% )} & \textbf {HC (ppm)} & \textbf {Nox (ppm)} & \textbf {$ \boldsymbol {\mathrm {CO_ 2}}$ (\% )} & \textbf {CO (\% )} & \textbf {HC (ppm)} & \textbf {Nox (ppm)} & \textbf {$ \boldsymbol {\mathrm {CO_ 2}}$ (\% )} \\ \hline \multicolumn {1}{|N|}{10} & 0.77 & 23 & 523 & 14.0 & 0.09 & 18 & 130 & 13.8 \\ \hline \multicolumn {1}{|c|}{30} & 0.78 & 13 & 555 & 14.2 & - & - & - & - \\ \hline \multicolumn {1}{|c|}{70} & 0.70 & 14 & 580 & 14.4 & - & - & - & - \\ \hline \multicolumn {1}{|c|}{90} & 0.74 & 15 & 630 & 14.5 & 0.70 & 15 & 618 & 14.6 \\ \hline \multicolumn {1}{|c|}{Neutral} & 0.06 & 7 & 21 & 15.6 & 0.06 & 5 & 27 & 15.7 \\ \hline \end {tabular}\caption {\textbf {}}\label {2000Emissions}\end {table}\end {document}
3. newcolumntype是如何工作的呢?
详情可见: https://tex.stackexchange.com/questions/257128/how-does-the-newcolumntype-command-work
\arraybackslash
As for \arraybackslash
, the \raggedright
, \raggedleft
, and \centering
declarations refine \
in a way that conflicts with its use in a tabular or array environments. The command \arraybackslash
(implemented in array and tabularx) restoresthe meaning of \
for use in array and tabular (you would only need \arraybackslash
for the last column).