[latex]表格

本文详细介绍了在 LaTeX 中创建和格式化表格的方法,包括基本的`tabular`环境、多列定义、文本换行、行间距调整、颜色应用、跨行和跨列等。通过学习,你可以创建出专业级别的表格。
摘要由CSDN通过智能技术生成

最常用的表格:

\begin{table*}[htbp]
  \centering
  \caption{A typical table }
  \begin{tabular}{ccccccc}
    \hline
    M          & S    & F      & S    & T   & T & F \\ 
    \hline
    K          & 1    & 9      & 1    & 2   & 5 & 7 \\   
    T          & 8    & 1      & 1    & 3   & 3 & 1  \\
    \hline
  \end{tabular}
  \label{tab}
\end{table*}

注释: {table*}这个*表示横跨整页;
where the table is declared ( here)
t at the  top of the page
at the  bottom of the page
on a dedicated  page of floats
!override the default float restrictions. E.g., the maximum size allowed of a  b float is normally quite small; if you want a large one, you need this  ! parameter as well.



Defining multiple columns[edit]

It is possible to define many identical columns at once using the *{ ''num''}{''str''} syntax. This is particularly useful when your table has many columns.

Here is a table with six centered columns flanked by a single column on each side:

\begin{ tabular}{ l*{ 6}{ c}r}
Team              & P & W & D & L & F  & A & Pts \\
\hline
Manchester United & 6 & 4 & 0 & 2 & 10 & 5 & 12  \\
Celtic            & 6 & 3 & 0 & 3 &  8 & 9 &  9  \\
Benfica           & 6 & 2 & 1 & 3 &  7 & 8 &  7  \\
FC Copenhagen     & 6 & 2 & 1 & 3 &  5 & 8 &  7  \\
\end{ tabular
}


以下为转载:http://en.wikibooks.org/wiki/LaTeX/Tables

LaTeX/Tables

LaTeX
LaTeX logo.svg

LaTeX

Tables are a common feature in academic writing, often used to summarize research results. Mastering the art of table construction in LaTeX is therefore necessary to produce quality papers and with sufficient practice one can print beautiful tables of any kind.

Keeping in mind that LaTeX is not a spreadsheet, it makes sense to use a dedicated tool to build tables and then to export these tables into the document. Basic tables are not too taxing, but anything more advanced can take a fair bit of construction; in these cases, more advanced packages can be very useful. However, first it is important to know the basics. Once you are comfortable with basic LaTeX tables, you might have a look at more advanced packages or the export options of your favorite spreadsheet. Thanks to the modular nature of LaTeX, the whole process can be automated in a fairly comfortable way.

For a long time, LaTeX tables were quite a chaotic topic, with dozens of packages doing similar things, while not always being compatible with one another. Sometimes you had to make trade-offs. The situation changed recently (2010) with the release of the tabu package which combines the power of longtabletabularx and much more. The tabu environment is far less fragile and restricted than the older alternatives. Nonetheless, before attempting to use this package for the first time it will be beneficial to understand how the classic environment works, since tabu works the same way. Note however that the author of tabu will not fix bugs to the current version, and that the next version introduces new syntax that will likely break existing documents.[1]

The tabular environment[edit]

The tabular environment can be used to typeset tables with optional horizontal and vertical lines. LaTeX determines the width of the columns automatically.

The first line of the environment has the form:

\begin{ tabular}[pos]{table spec}

The table spec argument tells LaTeX the alignment to be used in each column and the vertical lines to insert.

The number of columns does not need to be specified as it is inferred by looking at the number of arguments provided. It is also possible to add vertical lines between the columns here. The following symbols are available to describe the table columns (some of them require that the package array has been loaded):

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

By default, if the text in a column is too wide for the page, LaTeX won’t automatically wrap it. Using p{'width'} you can define a special type of column which will wrap-around the text as in a normal paragraph. You can pass the width using any unit supported by LaTeX, such as 'pt' and 'cm', or command lengths, such as \textwidth. You can find a list in chapterLengths.

The optional parameter pos can be used to specify the vertical position of the table relative to the baseline of the surrounding text. In most cases, you will not need this option. It becomes relevant only if your table is not in a paragraph of its own. You can use the following letters:

b bottom
c center (default)
t top

To specify a font format (such as bold, italic, etc.) for an entire column, you can add >{ \format} before you declare the alignment. For example \begin{ tabular}{  >{\bfseries}l c >{ \itshape}r } will indicate a three column table with the first one aligned to the left and in bold font, the second one aligned in the center and with normal font, and the third aligned to the right and in italic. The "array" package needs to be activated in the preamble for this to work.

In the first line you have pointed out how many columns you want, their alignment and the vertical lines to separate them. Once in the environment, you have to introduce the text you want, separating between cells and introducing new lines. The commands you have to use are the following:

& 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)
\cline{ i-j} partial horizontal line beginning in column i and ending in column j

Note, any white space inserted between these commands is purely down to ones' preferences. I personally add spaces between to make it easier to read.

Basic examples[edit]

This example shows how to create a simple table in LaTeX. It is a three-by-three table, but without any lines.

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

\begin{array}{lcr} 1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9 \\ \end{array}

Expanding upon that by including some vertical lines:

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

\begin{array}{l|c||r} 1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9 \\ \end{array}

To add horizontal lines to the very top and bottom edges of the table:

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

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

And finally, to add lines between all rows, as well as centering (notice the use of the center environment - of course, the result of this is not obvious from the preview on this web page):

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

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

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

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

\begin{ tabular}{ |r|l|}
  \hline
  7C0 & hexadecimal \\
  3700 & octal \\ \cline{ 2-2}
  11111000000 & binary \\
  \hline \hline
  1984 & decimal \\
  \hline
\end{ tabular
}

Latex example tabular cline.svg

Text wrapping in tables[edit]

LaTeX's algorithms for formatting tables have a few shortcomings. One is that it will not automatically wrap text in cells, even if it overruns the width of the page. For columns that will contain text whose length exceeds the column's width, it is recommended that you use the p attribute and specify the desired width of the column (although it may take some trial-and-error to get the result you want). For a more convenient method, have a look at The tabularx package, or The tabulary package.

Instead of p, use the m attribute to have the lines aligned toward the middle of the box or the b attribute to align along the bottom of the box.

Here is a simple example. The following code creates two tables with the same code; the only difference is that the last column of the second one has a defined width of 5 centimeters, while in the first one we didn't specify any width. Compiling this code:

\documentclass{ article}
\usepackage[english]{ babel}

\begin{ document}

Without specifying width for last column:
\begin{ center}
    \begin{ tabular}{ | l | l | l | 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. \\ \

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值