latex下的表格处理
LaTeX下的表格處理
https://zhuanlan.zhihu.com/p/19749566?columnSlug=latex
作者:李阿玲
鏈接:https://zhuanlan.zhihu.com/p/19749566
來源:知乎
著作權(quán)歸作者所有。商業(yè)轉(zhuǎn)載請(qǐng)聯(lián)系作者獲得授權(quán),非商業(yè)轉(zhuǎn)載請(qǐng)注明出處。
在LaTeX里面,畫一個(gè)表格大致上是如此:
\begin{tabular}{|c|c|c|} \hline 2&9&4\\ \hline 7&5&3\\ \hline 6&1&8\\ \hline \end{tabular}畫出來實(shí)際上是這樣:
這個(gè)tabular環(huán)境是LaTeX下面定義好了的,除了tabular環(huán)境外還有tabbing和array環(huán)境。還是講tabular環(huán)境好了,LaTeX還定義了如下的一條命令:
%\multicolumn{n}{format}{item} \begin{tabular}{|c|c|c|} \hline \multicolumn{3}{|c|}{?}\\ \hline 7&5&3\\ \hline 6&1&8\\ \hline \end{tabular}我們給出的這個(gè)例子里面的|c|這樣的,那個(gè)地方叫做導(dǎo)言區(qū),來約定表格的每列屬性的:
% l c r: 居左,居中,居右 % | ||: 單線,雙線 % @{exp}: 插入到列中的文本 % *{n}{pre}: 重復(fù)pre內(nèi)容n次 % p{len}: 將列放入一個(gè)parbox中所以第一個(gè)例子我們可以這樣:
\begin{tabular}{*{3}{|c}|}不過,真是要用這些個(gè),別給自己繞暈了。在處理表格的時(shí)候,可以調(diào)節(jié)一下參數(shù),比如這樣:
\tabcolsep20pt \arrayrulewidth2pt結(jié)果是這樣的:
這里面的線存在著一定的空缺,這是因?yàn)樗胶痛怪狈较蛏厦娴木€都是和行或列直接關(guān)聯(lián)的,如果線條寬度過大,只能保證線條在一部分上能夠接上,調(diào)太粗了就接不上了。TeX中默認(rèn)的線條寬度是0.4pt,過于細(xì)了,如有可能,這有必要改一下的。
實(shí)際上,array包重新實(shí)現(xiàn)了tabular環(huán)境,加了不少新選項(xiàng)進(jìn)去,如:
array給出了一個(gè)新的選項(xiàng),可以定義列類型:
\newcolumntype{F}{>{$}c<{$}}然后我們可以這樣:
%\usepackage{array} \newcolumntype{F}{>{$}c<{$}} \begin{tabular}{FFF} \alpha & \beta & \gamma \\ \delta & \epsilon & \upsilon \\ \sigma & \tau & \phi \\ \end{tabular}另一個(gè)改進(jìn),就是設(shè)定線寬度那個(gè),代碼不變,加了array包之后就是這樣(貌似很神奇的樣子):
下面該說說三線表booktabs了,比如說:
% \usepackage{booktabs} \begin{tabular}{ccc} \toprule 2&9&4\\ \midrule 7&5&3\\ 6&1&8\\ \bottomrule \end{tabular}輸出的效果是這樣:
其實(shí),這三條線的定義里面,\toprule和\bottomrule的定義非常類似,你看看booktabs.sty中的定義就知道了:
\def\toprule{\noalign{\ifnum0=`}\fi\@aboverulesep=\abovetopsep\global\@belowrulesep=\belowrulesep\global\@thisruleclass=\@ne\@ifnextchar[{\@BTrule}{\@BTrule[\heavyrulewidth]}} \def\midrule{\noalign{\ifnum0=`}\fi\@aboverulesep=\aboverulesep\global\@belowrulesep=\belowrulesep\global\@thisruleclass=\@ne\@ifnextchar[{\@BTrule}{\@BTrule[\lightrulewidth]}} \def\bottomrule{\noalign{\ifnum0=`}\fi\@aboverulesep=\aboverulesep\global\@belowrulesep=\belowbottomsep\global\@thisruleclass=\@ne\@ifnextchar[{\@BTrule}{\@BTrule[\heavyrulewidth]}}差別在哪里呢?來繼續(xù)看:
\belowrulesep=.65ex \belowbottomsep=0ptbooktabs這個(gè)包就是提供了不同寬度的線條罷了。
接下來是比較常用的colortbl包了,用來填色:
% \usepackage{colortbl} \begin{tabular}{ccc} \rowcolor[gray]{.9} 2&9&4\\ \rowcolor[gray]{.8} 7&5&3\\ \rowcolor[gray]{.7} 6&1&8\\ \end{tabular}當(dāng)然也可以這樣:
% \usepackage{colortbl} \begin{tabular}% {>{\columncolor[gray]{.9}}c% >{\columncolor[gray]{.8}}c% >{\columncolor[gray]{.7}}c} 2&9&4\\ 7&5&3\\ 6&1&8\\ \end{tabular}還可以是這樣:
\begin{tabular}{ccc} \cellcolor[rgb]{.9,.9,.9}2& \cellcolor[rgb]{.8,.9,.9}9& \cellcolor[rgb]{.7,.9,.9}4\\ \cellcolor[rgb]{.9,.8,.9}7& \cellcolor[rgb]{.8,.8,.9}5& \cellcolor[rgb]{.7,.8,.9}3\\ \cellcolor[rgb]{.9,.7,.9}6& \cellcolor[rgb]{.8,.7,.9}1& \cellcolor[rgb]{.7,.7,.9}8\\ \end{tabular}需要注意的是顏色搭配,搭配好了,事半功倍,搭配不好,那就鄉(xiāng)村非主流了。
還有個(gè)強(qiáng)烈推薦的,畫斜線表頭的diagbox包,作者嘛,劉海洋,文檔有中文版,你們肯定都能看得懂(也沒理由看不懂啊),那我就搬個(gè)例子好了:
% \usepackage{diagbox} \begin{tabular}{|l|ccc|} \hline \diagbox{Time}{Room}{Day} & Mon & Tue & Wed \\ \hline Morning & used & used & \\ Afternoon & & used & used \\ \hline \end{tabular}是這樣:
和文中開頭的\multicolumn相對(duì)應(yīng)的\multirow命令由multirow包提供,我們可以試著這樣套嵌一下:
% \usepackage{multirow} \begin{tabular}{|ccc|} \hline 2&9&4\\ 7&\multicolumn{2}{c|}{\multirow{2}*{{?}}}\\ 6&&\\ \hline \end{tabular}我個(gè)人覺得最好的一個(gè)包其實(shí)是tabu包。比如LaTeX/Tables這里面的這個(gè)例子,如果用tabu包的話,可以是這樣的設(shè)定:
% \usepackage{tabu} \begin{tabu} to \hsize {|X|X|X|X[3,l]|} \hline Day & Min Temp & Max Temp & Summary \\ \hline Monday & 11C & 22C & A clear day with lots of sunshine. However, the strong breeze will bring down the temperatures. \\ \hline Tuesday & 9C & 19C & 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 & Rain will still linger for the morning. Conditions will improve by early afternoon and continue throughout the evening. \\ \hline \end{tabu}效果是這樣(當(dāng)然效果還可以再調(diào)整了):
這個(gè)X呢會(huì)讓tabu自動(dòng)算出寬度來,所以不需要再怎么仔細(xì)地設(shè)定寬度了,當(dāng)然想要強(qiáng)制指定一個(gè)參數(shù)也不是不可以。
行了,今天就介紹這么多了,還有什么longtable之類的,懶得介紹了,表格的用法就這樣了,差不多。有個(gè)地方需要注意:TeX中的表格有個(gè)列數(shù)限制,最大256列。
總結(jié)
以上是生活随笔為你收集整理的latex下的表格处理的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Matlab中巧用LaTex
- 下一篇: chrome 播放视频提示 adobe