我們可以使用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}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
makecell 宏包這種表項分行常用在表頭中。在Latex中還單獨定義了類似的thead命令,它產生的字體較小,上下間距較大的單元更適合文字較多的多行表頭使用。
先貼代碼
begin{tabular}{|r|r|}
hline
thead{處理前 \ 數據} & thead{處理後 \ 數據} \
hline
1234 & 5678 \
hline
end{tabular}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
documentclass[UTF8]{ctexart}
usepackage{makecell}
begin{tabular}{|r|r|}
hline
thead{處理前 \ 數據} & thead{處理後 \ 數據} \
hline
1234 & 5678 \
hline
end{tabular}
end{document}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13

我們可以對比一下使用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}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
可以得到的效果如下所示:

如果我們想要畫下面的表格:

代碼如下:
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}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
都學了這麼多,下面是我們的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}
原創文章,作者:投稿專員,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/202655.html
微信掃一掃
支付寶掃一掃