latex表格居中显示(latex表格宽度设置的方法)
我们可以使用makecell命令对表格单元格中的数据进行一些变换的控制。我们可以使用 命令进行换行,也可以使用p{(宽度)}选项控制列表的宽度
使用makecell 命令我们需要在导言区添加usepackage{makecell}才能正常编译通过。makecell命令的内容是默认居中对齐的,也可以选用选项t,b,l,r,c等分别控制表格单元格中的格式。
举个例子:
代码如下:
documentclass[UTF8]{ctexart}
usepackage{makecell}
begin{document}
begin{tabular}{|r|r|}
hline
makecell{处理前\ 数据} && makecell{处理后 \ 数据} \ hline
1234 && 5678 \
hline
end{tabular}
end{document}
12345678910111213141516
makecell 宏包这种表项分行常用在表头中。在Latex中还单独定义了类似的thead命令,它产生的字体较小,上下间距较大的单元更适合文字较多的多行表头使用。
先贴代码
begin{tabular}{|r|r|}
hline
thead{处理前 \ 数据} && thead{处理后 \ 数据} \
hline
1234 && 5678 \
hline
end{tabular}
1234567
documentclass[UTF8]{ctexart}
usepackage{makecell}
begin{tabular}{|r|r|}
hline
thead{处理前 \ 数据} && thead{处理后 \ 数据} \
hline
1234 && 5678 \
hline
end{tabular}
end{document}
12345678910111213
我们可以对比一下使用makecell 和使用thead之前表格的区别:
直观的感受就是字体变小了。
在makecell的rothead 命令则相当于旋转了90° 的thead命令,这个命令还依赖rotating宏包,在我们使用rothead时需要给旋转表头的宽度rotheadsize赋值,否则就会就没有我们想要的效果
表头的字体由theadfont 命令控制
例如:
documentclass[UTF8]{ctexart}
usepackage{makecell,rotating}
begin{document}
settowidthrotheadsize{theadfont 数学课}
begin{tabular}{|c|c|}
hline
thead{姓名} && rothead{数学课\成绩} \
hline
Hebe && 100 \
hline
end{tabular}
end{document}
12345678910111213141516
可以得到的效果如下所示:
如果我们想要画下面的表格:
代码如下:
documentclass[UTF8]{ctexart}
usepackage{makecell,rotating,multirow,diagbox}
begin{document}
begin{tabular}{|c|*{4}{c}|}
hline
diagbox{序号1}{序号2} && 我 && 爱 && hebe && 哈哈 \
hline
数字 && 1 && 2 && 3 && 4 \
hline
数字 && 2 && 4 && 6 && 8 \
hline
end{tabular}
end{document}
123456789101112131415
都学了这么多,下面是我们的DIY时间,我们总结一下我写论文时候用到的一些表格,下次大家用到的时候直接拿去用吧。
documentclass[UTF8]{ctexart}
usepackage{makecell,multirow,diagbox}
begin{document}
begin{tabular}{|c|c|c|c|c|c|}
hline
multirow{2}*{} && system && multicolumn{2}{c|}{4.0} && multicolumn{2}{c|}{6.0} \
cline{2-6}
&& Device && D1 && D2 && D3 && D4 \
hline
multirow{2}*{Runtime} && 600 byte && 12/23/34 && 23/2/1 && 12/1/2 && 1/2/3 \
cline{2-6}
&& 1000 byte && 12 && 21 && 12 && 12 \
hline
multirow{2}*{System} && 600 byte && 12 && 23 && 12 && 1 \
cline{2-6}
&& 1000 byte && 12 && 21 && 12 && 12 \
hline
end{tabular}
end{document}