LaTeX----表格處理

著作權歸作者所有。
商業轉載請聯繫作者獲得授權,非商業轉載請註明出處。
作者:李阿玲
鏈接:http://zhuanlan.zhihu.com/LaTeX/19749566
來源:知乎

在LaTeX裏面,畫一個表格大致上是如此:

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

畫出來實際上是這樣:

這個tabular環境是LaTeX下面定義好了的,除了tabular環境外還有tabbing和array環境。還是講tabular環境好了,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}

我們給出的這個例子裏面的|c|這樣的,那個地方叫做導言區,來約定表格的每列屬性的:

% l c r: 居左,居中,居右
% | ||: 單線,雙線
% @{exp}: 插入到列中的文本
% *{n}{pre}: 重複pre內容n次
% p{len}: 將列放入一個parbox中

所以第一個例子我們可以這樣:

\begin{tabular}{*{3}{|c}|}

不過,真是要用這些個,別給自己繞暈了。在處理表格的時候,可以調節一下參數,比如這樣:

\tabcolsep20pt
\arrayrulewidth2pt

結果是這樣的:

這裏面的線存在着一定的空缺,這是因爲水平和垂直方向上面的線都是和行或列直接關聯的,如果線條寬度過大,只能保證線條在一部分上能夠接上,調太粗了就接不上了。TeX中默認的線條寬度是0.4pt,過於細了,如有可能,這有必要改一下的。

實際上,array包重新實現了tabular環境,加了不少新選項進去,如:

array給出了一個新的選項,可以定義列類型:

\newcolumntype{F}{>{$}c<{$}}

然後我們可以這樣:

%\usepackage{array}
\newcolumntype{F}{>{$}c<{$}}
\begin{tabular}{FFF}
\alpha & \beta    & \gamma   \\
\delta & \epsilon & \upsilon \\
\sigma & \tau     & \phi     \\
\end{tabular}

另一個改進,就是設定線寬度那個,代碼不變,加了array包之後就是這樣(貌似很神奇的樣子):

下面該說說三線表booktabs了,比如說:

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

輸出的效果是這樣:

其實,這三條線的定義裏面,\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]}}

差別在哪裏呢?來繼續看:

\belowrulesep=.65ex
\belowbottomsep=0pt

booktabs這個包就是提供了不同寬度的線條罷了。

接下來是比較常用的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}

當然也可以這樣:

% \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}

需要注意的是顏色搭配,搭配好了,事半功倍,搭配不好,那就鄉村非主流了。

還有個強烈推薦的,畫斜線表頭的diagbox包,作者嘛,劉海洋,文檔有中文版,你們肯定都能看得懂(也沒理由看不懂啊),那我就搬個例子好了:

% \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相對應的\multirow命令由multirow包提供,我們可以試着這樣套嵌一下:

% \usepackage{multirow}
\begin{tabular}{|ccc|}
\hline
2&9&4\\
7&\multicolumn{2}{c|}{\multirow{2}*{{?}}}\\
6&&\\
\hline
\end{tabular}

我個人覺得最好的一個包其實是tabu包。比如LaTeX/Tables這裏面的這個例子,如果用tabu包的話,可以是這樣的設定:

% \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}

效果是這樣(當然效果還可以再調整了):

這個X呢會讓tabu自動算出寬度來,所以不需要再怎麼仔細地設定寬度了,當然想要強制指定一個參數也不是不可以。

行了,今天就介紹這麼多了,還有什麼longtable之類的,懶得介紹了,表格的用法就這樣了,差不多。有個地方需要注意:TeX中的表格有個列數限制,最大256列。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章