当前位置:   article > 正文

LaTeX 表格

latex \begin{table}[h] \newcolumntype{d}


本系列文章由 @YhL_Leo 出品,转载请注明出处。
文章链接: http://blog.csdn.net/yhl_leo/article/details/50066137


一些LaTeX中表格的使用方法如下:

1.table

  1. \documentclass{article}
  2. \begin{document}
  3. \begin{table}[h]
  4. \centering
  5. \begin{tabular}{|l|c|c|}\hline
  6. Setting&\multicolumn{2}{c|}{A4 size paper}\\\hline
  7. &mm&inches\\
  8. Top&25&1.0\\
  9. Bottom&25&1.0\\
  10. Left&20&0.8\\
  11. Right&20&0.8\\
  12. Column Width&82&3.2\\
  13. Column Spacing&6&0.25\\\hline
  14. \end{tabular}
  15. \caption{Margin settings for A4 size paper}
  16. \label{tab:Margin_settings}
  17. \end{table}
  18. \end{document}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21


20151126235124271


2.tabular

  1. \begin{tabular}{|c|c|c|}
  2. \hline 2&9&4\\
  3. \hline 7&5&3\\
  4. \hline 6&1&8\\
  5. \hline
  6. \end{tabular}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6


20151126235208486


例子里面的|c|这样的,那个地方叫做导言区,来约定表格的每列属性的:

l
left-justified column
c
centered column
r
right-justified column
b
bottom
c
center (default)
t
top
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

另外,还有一些常用的命令

&
column separator
\\
start new row (additional space may be specified after \\ using square brackets, such as \\[6pt]
\hline
horizontal line
\newline
start a new line within a cell (in a paragraph column)
\tabularnewline
start a new line within a cell (in a paragraph column) instead of \\ to avoid a Misplaced \noalign error in the tabular
\cline{i-j}
partial horizontal line beginning in column i and ending in column j

除了最简单的用例外,还可以这样:

  1. % example 1:
  2. \begin{center}
  3. \begin{tabular}{ | l || c || r }
  4. \hline
  5. 1 & 2 & 3 \\ \hline
  6. 4 & 5 & 6 \\ \hline \hline
  7. 7 & 8 & 9 \\
  8. \hline
  9. \end{tabular}
  10. \end{center}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10


20151130120159258
example 1

  1. % example 2:
  2. %\multicolumn{n}{format}{item}
  3. \begin{tabular}{|c|c|c|}
  4. \hline \multicolumn{3}{|c|}{?}\\
  5. \hline 7&5&3\\
  6. \hline 6&1&8\\
  7. \hline
  8. \end{tabular}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8


20151127000056370
example 2

  1. % example 3:
  2. \begin{tabular}{|r|l|}
  3. \hline
  4. 7C0 & hexadecimal \\
  5. 3700 & octal \\ \cline{2-2}
  6. 11111000000 & binary \\
  7. \hline \hline
  8. 1984 & decimal \\
  9. \hline
  10. \end{tabular}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10


20151130120417806
example 3


array环境中(使用时需要包含:\usepackage{array}),重新实现了tabular环境,加了不少新选项进去:


20151127000641108

对于tabular的第一个例子略作修改,可以发现在不含有array包和含有时效果是不一样的:

  1. % example 1:
  2. %\usepackage{array}
  3. \tabcolsep20pt
  4. \arrayrulewidth2pt
  5. \begin{tabular}{*{3}{|c}|}
  6. %\begin{tabular}{|c|c|c|}
  7. \hline 2&9&4\\
  8. \hline 7&5&3\\
  9. \hline 6&1&8\\
  10. \hline
  11. \end{tabular}
  12. % example 2:
  13. ...
  14. \usepackage{array}
  15. ...
  16. \tabcolsep20pt
  17. \arrayrulewidth2pt
  18. \begin{tabular}{*{3}{|c}|}
  19. %\begin{tabular}{|c|c|c|}
  20. \hline 2&9&4\\
  21. \hline 7&5&3\\
  22. \hline 6&1&8\\
  23. \hline
  24. \end{tabular}
  • 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


20151129221513400
example 1


20151129221522422
example 2


对于表格内有长文本时,直接排下来不但很难看而且很容超出页面的范围,可以这么做:

  1. Without specifying width for last column:
  2. \begin{center}
  3. \begin{tabular}{| l | l | l | l |}
  4. \hline
  5. Day & Min Temp & Max Temp & Summary \\ \hline
  6. Monday & 11C & 22C & A clear day with lots of sunshine.
  7. However, the strong breeze will bring down the temperatures. \\ \hline
  8. Tuesday & 9C & 19C & Cloudy with rain, across many northern regions. Clear spells
  9. across most of Scotland and Northern Ireland,
  10. but rain reaching the far northwest. \\ \hline
  11. Wednesday & 10C & 21C & Rain will still linger for the morning.
  12. Conditions will improve by early afternoon and continue
  13. throughout the evening. \\
  14. \hline
  15. \end{tabular}
  16. \end{center}
  17. With width specified:
  18. \begin{center}
  19. \begin{tabular}{ | l | l | l | p{5cm} |}
  20. \hline
  21. Day & Min Temp & Max Temp & Summary \\ \hline
  22. Monday & 11C & 22C & A clear day with lots of sunshine.
  23. However, the strong breeze will bring down the temperatures. \\ \hline
  24. Tuesday & 9C & 19C & Cloudy with rain, across many northern regions. Clear spells
  25. across most of Scotland and Northern Ireland,
  26. but rain reaching the far northwest. \\ \hline
  27. Wednesday & 10C & 21C & Rain will still linger for the morning.
  28. Conditions will improve by early afternoon and continue
  29. throughout the evening. \\
  30. \hline
  31. \end{tabular}
  32. \end{center}
  • 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
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33


20151130121219058

另外使用宏包tabulary也可以实现:

  1. \usepackage{tabulary}
  2. ...
  3. \begin{center}
  4. \begin{tabulary}{0.7\textwidth}{LCL}
  5. Short sentences & \# & Long sentences \\
  6. \hline
  7. This is short. & 173 & This is much loooooooonger, because there are many more words. \\
  8. This is not shorter. & 317 & This is still loooooooonger, because there are many more words. \\
  9. \end{tabulary}
  10. \end{center}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

20151130141418600

其中的对齐方式除了 L, C外,还有 R, J


让我们看下\parbox的用法,如果我们想把文段分割成为表的形式:

  1. \begin{tabular}{cc}
  2. boring cell content & \parbox[t]{5cm}{rather long par\\new par}
  3. \end{tabular}
  • 1
  • 2
  • 3


20151130122319236


调整行列间距可以使用命令:

  1. \setlength{\tabcolsep}{5pt} %colums, default value is 6pt
  2. \renewcommand{\arraystretch}{1.5} %rows, default value is 1.0
  • 1
  • 2
  • 3

另外一种方式是在\hlinecline{i-j}后添加\noalign{\smallskip}, 例如:

  1. \begin{center}
  2. \begin{tabular}{ | l | l | r | }
  3. \hline
  4. \multicolumn{2}{c}{Item} \\
  5. \cline{1-2}
  6. Animal & Description & Price (\$) \\
  7. \hline
  8. Gnat & per gram & 13.65 \\
  9. & each & 0.01 \\
  10. Gnu & stuffed & 92.50 \\
  11. Emu & stuffed & 33.33 \\
  12. Armadillo & frozen & 8.99 \\
  13. \hline
  14. \end{tabular}
  15. example 1
  16. $ $
  17. \setlength{\tabcolsep}{4pt}
  18. \begin{tabular}{ | l | l | r | }
  19. \hline
  20. \multicolumn{2}{c}{Item} \\
  21. \cline{1-2}
  22. Animal & Description & Price (\$) \\
  23. \hline
  24. Gnat & per gram & 13.65 \\
  25. & each & 0.01 \\
  26. Gnu & stuffed & 92.50 \\
  27. Emu & stuffed & 33.33 \\
  28. Armadillo & frozen & 8.99 \\
  29. \hline
  30. \end{tabular}
  31. example 2
  32. $ $
  33. \renewcommand{\arraystretch}{1.5}
  34. \begin{tabular}{ | l | l | r | }
  35. \hline
  36. \multicolumn{2}{c}{Item} \\
  37. \cline{1-2}
  38. Animal & Description & Price (\$) \\
  39. \hline
  40. Gnat & per gram & 13.65 \\
  41. & each & 0.01 \\
  42. Gnu & stuffed & 92.50 \\
  43. Emu & stuffed & 33.33 \\
  44. Armadillo & frozen & 8.99 \\
  45. \hline
  46. \end{tabular}
  47. example 3
  48. $ $
  49. \setlength{\tabcolsep}{6pt}
  50. \renewcommand{\arraystretch}{1.0}
  51. \begin{tabular}{ | l | l | r | }
  52. \hline\noalign{\smallskip}
  53. \multicolumn{2}{c}{Item} \\
  54. \cline{1-2}\noalign{\smallskip}
  55. Animal & Description & Price (\$) \\
  56. \noalign{\smallskip}\hline\noalign{\smallskip}
  57. Gnat & per gram & 13.65 \\
  58. & each & 0.01 \\
  59. Gnu & stuffed & 92.50 \\
  60. Emu & stuffed & 33.33 \\
  61. Armadillo & frozen & 8.99 \\
  62. \noalign{\smallskip}\hline
  63. \end{tabular}
  64. example 4
  65. \end{center}
  • 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
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77


20151130124045628

其他用法如在某一行后空出一定间距:

  1. \begin{tabular}{ll}
  2. \hline
  3. Mineral & Color \\[0.5cm]
  4. Ruby & red \\
  5. Sapphire & blue \\
  6. \hline
  7. \end{tabular}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7


20151130124810980


一些LaTeX的环境也可以插入表格中:

  1. \begin{tabular}{m{5cm} c} % here the m can be replaced by p or b.
  2. \\ \hline
  3. \begin{verbatim}
  4. I love coding
  5. \end{verbatim}
  6. & LaTeX
  7. \\ \hline
  8. \end{tabular}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8


20151130125341386

使用列规格设置命令:>{\cmd} and <{\cmd},如设置表格中的字符都是数学手写字体:

  1. ...
  2. \usepackage{array}
  3. ...
  4. \newcolumntype{F}{>{$}c<{$}}
  5. \begin{tabular}{FFF}
  6. \alpha & \beta & \gamma \\
  7. \delta & \epsilon & \upsilon \\
  8. \sigma & \tau & \phi \\
  9. \end{tabular}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10


20151129222012983


@-expressions的用法:

  1. \begin{tabular}{|@{}l|l@{}|}
  2. \hline
  3. stuff & stuff \\ \hline
  4. stuff & stuff \\
  5. \hline
  6. \end{tabular}
  7. \begin{tabular}{r@{.}l}
  8. 3 & 14159 \\
  9. 16 & 2 \\
  10. 123 & 456 \\
  11. \end{tabular}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12


20151130131243817


行的特别设置,给出几个例子:

  1. \begin{tabular}{ |l|l| }
  2. \hline
  3. \multicolumn{2}{|c|}{Team sheet} \\
  4. \hline
  5. GK & Paul Robinson \\
  6. LB & Lucas Radebe \\
  7. DC & Michael Duberry \\
  8. DC & Dominic Matteo \\
  9. RB & Dider Domi \\
  10. MC & David Batty \\
  11. MC & Eirik Bakke \\
  12. MC & Jody Morris \\
  13. FW & Jamie McMaster \\
  14. ST & Alan Smith \\
  15. ST & Mark Viduka \\
  16. \hline
  17. \end{tabular}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17


20151130140412310

  1. ...
  2. \usepackage{multirow}
  3. ...
  4. \begin{tabular}{ |l|l|l| }
  5. \hline
  6. \multicolumn{3}{ |c| }{Team sheet} \\
  7. \hline
  8. Goalkeeper & GK & Paul Robinson \\ \hline
  9. \multirow{4}{*}{Defenders} & LB & Lucas Radebe \\
  10. & DC & Michael Duburry \\
  11. & DC & Dominic Matteo \\
  12. & RB & Didier Domi \\ \hline
  13. \multirow{3}{*}{Midfielders} & MC & David Batty \\
  14. & MC & Eirik Bakke \\
  15. & MC & Jody Morris \\ \hline
  16. Forward & FW & Jamie McMaster \\ \hline
  17. \multirow{2}{*}{Strikers} & ST & Alan Smith \\
  18. & ST & Mark Viduka \\
  19. \hline
  20. \end{tabular}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21


20151130140706536

  1. ...
  2. \usepackage{multirow}
  3. ...
  4. \begin{tabular}{cc|c|c|c|c|l}
  5. \cline{3-6}
  6. & & \multicolumn{4}{ c| }{Primes} \\ \cline{3-6}
  7. & & 2 & 3 & 5 & 7 \\ \cline{1-6}
  8. \multicolumn{1}{ |c }{\multirow{2}{*}{Powers} } &
  9. \multicolumn{1}{ |c| }{504} & 3 & 2 & 0 & 1 & \\ \cline{2-6}
  10. \multicolumn{1}{ |c }{} &
  11. \multicolumn{1}{ |c| }{540} & 2 & 3 & 1 & 0 & \\ \cline{1-6}
  12. \multicolumn{1}{ |c }{\multirow{2}{*}{Powers} } &
  13. \multicolumn{1}{ |c| }{gcd} & 2 & 2 & 0 & 0 & min \\ \cline{2-6}
  14. \multicolumn{1}{ |c }{} &
  15. \multicolumn{1}{ |c| }{lcm} & 3 & 3 & 1 & 1 & max \\ \cline{1-6}
  16. \end{tabular}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17


20151130140848237

  1. \begin{tabular}{ r|c|c| }
  2. \multicolumn{1}{r}{}
  3. & \multicolumn{1}{c}{noninteractive}
  4. & \multicolumn{1}{c}{interactive} \\
  5. \cline{2-3}
  6. massively multiple & Library & University \\
  7. \cline{2-3}
  8. one-to-one & Book & Tutor \\
  9. \cline{2-3}
  10. \end{tabular}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10


20151130140958289


3.booktabs

三线表

  1. \usepackage{booktabs}
  2. \begin{tabular}{ccc}
  3. \toprule
  4. 2&9&4\\
  5. \midrule
  6. 7&5&3\\
  7. 6&1&8\\
  8. \bottomrule
  9. \end{tabular}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10


20151130105817522


4.colortbl

常用的表格填色colortbl包:

  1. % example 1:
  2. \usepackage{colortbl}
  3. \begin{tabular}{ccc}
  4. \rowcolor[gray]{.9}
  5. 2&9&4\\
  6. \rowcolor[gray]{.8}
  7. 7&5&3\\
  8. \rowcolor[gray]{.7}
  9. 6&1&8\\
  10. \end{tabular}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11


20151130111217977
example 1

  1. % example 2:
  2. \begin{tabular}
  3. {>{\columncolor[gray]{.9}}c|
  4. >{\columncolor[gray]{.8}}c|
  5. >{\columncolor[gray]{.7}}c}
  6. 2&9&4\\
  7. 7&5&3\\
  8. 6&1&8\\
  9. \end{tabular}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9


20151130112044241
example 2

  1. %example 3:
  2. \begin{tabular}{ccc}
  3. \cellcolor[rgb]{.9,.9,.9}2&
  4. \cellcolor[rgb]{.8,.9,.9}9&
  5. \cellcolor[rgb]{.7,.9,.9}4\\
  6. \cellcolor[rgb]{.9,.8,.9}7&
  7. \cellcolor[rgb]{.8,.8,.9}5&
  8. \cellcolor[rgb]{.7,.8,.9}3\\
  9. \cellcolor[rgb]{.9,.7,.9}6&
  10. \cellcolor[rgb]{.8,.7,.9}1&
  11. \cellcolor[rgb]{.7,.7,.9}8\\
  12. \end{tabular}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12


20151130112427746
example 3


5.diagbox

画斜线表头的包diagbox

  1. \usepackage{diagbox}
  2. ...
  3. \begin{tabular}{|l|ccc|}
  4. \hline
  5. \diagbox{Time}{Room}{Day} & Mon & Tue & Wed \\
  6. \hline
  7. Morning & used & used & \\
  8. Afternoon & & used & used \\
  9. \hline
  10. \end{tabular}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

20151130141759121

参考:

转载于:https://www.cnblogs.com/hehehaha/p/6332214.html

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/237957
推荐阅读
相关标签
  

闽ICP备14008679号