Latex对于学术创作来说十分方便,尤其是对于经常编程的人来说,应当更喜欢用代码码论文。创作时不用再忍受office复杂而又难以捉摸的排版字体,也不用忍受MathType的卡顿与不兼容。
在使用Latex的过程中我认为最难的就是表格,每次都要在网上根据自己的需求搜索相关的命令,所以这里就整理一下我最近写论文遇到的表格问题。
tabular后需要设置垂向位置和表格格式
\begin{tabular}[pos]{table spec}
pos可以设置的参数有b(bottom), c(center), t(top)
1、表格宽度设置
1.1 固定列宽
设置列宽是我是用latex以来,个人认为最复杂的。因为在tabular和tabular*环境下,有不同的设置,每个设置差异又不明显,经常出现我随便怎么修改,列宽和格式都不变的情况。
列宽设置是在\begin{tabular}后的“{}”中编写,在tabular环境中,设置有:
加入array包,
\documentclass{article}
\usepackage{array}
\begin{document}
\begin{center}
\begin{tabular}{ | m{5em} | m{1cm}| m{1cm} | }
\hline
cell1 dummy text dummy text dummy text& cell2 & cell3 \\
\hline
cell1 dummy text dummy text dummy text & cell5 & cell6 \\
\hline
cell7 & cell8 & cell9 \\
\hline
\end{tabular}
\end{center}
\end{document}
1.2 设置表格宽度
tabular*环境在tabular基础上,增加了设置表格宽度的功能
\begin{tabular*}{0.75\textwidth}{ | c | c | c | r | }
\hline
label 1 & label 2 & label 3 & label 4 \\
\hline
item 1 & item 2 & item 3 & item 4 \\
\hline
\end{tabular*}
为了进一步简化表格宽度设置,我们还可以使用tabularx包中的说明符“X”拉伸相应的列使表格自动达到设置的宽度
\usepackage{tabularx}
% ...
\begin{tabularx}{\textwidth}{ |X|X|X|X| }
\hline
label 1 & label 2 & label 3 & label 4 \\
\hline
item 1 & item 2 & item 3 & item 4 \\
\hline
\end{tabularx}
2、表格中文本对齐方式
2.1 默认的对齐方式
最基本的对齐方式有左对齐、居中对齐和右对齐,纵向上还有顶部对齐、垂直居中和底部对齐。默认情况下都是根据内容调整宽度,但是当文本过长的时候就会出现超出页面的情况。
l | left-justified column |
c | centered 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 |
2.2 自定义对齐
从上边的介绍可以发现,表格中没有水平和上下同时居中的选项,这就很不科学了,完全居中是经常使用的一种对齐方式。编辑表格的时候也可以没有,我就经常放弃上下的对齐方式,只考虑水平对齐。虽然tabular没有,但是在网上找到了这样的命令。
\usepackage{array}
\newcolumntype{L}[1]{>{\raggedright\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\newcolumntype{C}[1]{>{\centering\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\newcolumntype{R}[1]{>{\raggedleft\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
只需要将原有的“l”,“c”和“r”,换成大写,就可以附加一个上下居中。
参考:
LaTeX - Fixed table column width while aligning text left/center/right · GitHub
3、合并单元格
3.1 合并列
\usepackage{multirow}
\documentclass{article}
\usepackage{multirow}
\begin{document}
\begin{tabular}{ |p{3cm}||p{3cm}|p{3cm}|p{3cm}| }
\hline
\multicolumn{4}{|c|}{Country List} \\
\hline
Country Name or Area Name& ISO ALPHA 2 Code &ISO ALPHA 3 Code&ISO numeric Code\\
\hline
Afghanistan & AF &AFG& 004\\
Aland Islands& AX & ALA &248\\
Albania &AL & ALB& 008\\
Algeria &DZ & DZA& 012\\
American Samoa& AS & ASM&016\\
Andorra& AD & AND &020\\
Angola& AO & AGO&024\\
\hline
\end{tabular}
\end{document}
3.2 合并行
\usepackage{multirow}
\documentclass{article}
\usepackage{multirow}
\begin{document}
\begin{center}
\begin{tabular}{ |c|c|c|c| }
\hline
col1 & col2 & col3 \\
\hline
\multirow{3}{4em}{Multiple row} & cell2 & cell3 \\
& cell5 & cell6 \\
& cell8 & cell9 \\
\hline
\end{tabular}
\end{center}
\end{document}
PS:即存在跨行又存在跨列的时候好像就不好设置了,上周遇到一次,试了半天最终还是放弃了。