最常用的表格:
\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*}
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} |
以下为转载:http://en.wikibooks.org/wiki/LaTeX/Tables
LaTeX/Tables
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 longtable, tabularx 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]
Contents
[hide]- 1 The tabular environment
- 1.1 Basic examples
- 1.2 Text wrapping in tables
- 1.3 Manually broken paragraphs in table cells
- 1.4 Space between columns
- 1.5 Space between rows
- 1.6 Other environments inside tables
- 1.7 Defining multiple columns
- 1.8 Column specification using >{\cmd} and <{\cmd}
- 1.9 @-expressions
- 1.10 Aligning columns at decimal points using dcolumn
- 2 Row specification
- 3 Spanning
- 4 Controlling table size
- 5 Colors
- 6 Width and stretching
- 7 Table across several pages
- 8 Partial vertical lines
- 9 Vertically centered images
- 10 Footnotes in tables
- 11 Professional tables
- 12 Sideways tables
- 13 Table with legend
- 14 The eqparbox package
- 15 Floating with table
- 16 Using spreadsheets
- 17 Need more complicated features?
- 18 References
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 } |
Expanding upon that by including some vertical lines:
\begin{
tabular}{
l | c || r } |
To add horizontal lines to the very top and bottom edges of the table:
\begin{
tabular}{
l | c || r } |
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{
center} |
\begin{
tabular}{
|r|l|} |
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} |