Outils pour utilisateurs

Outils du site


issue204:latex

Ceci est une ancienne révision du document !


This time, I am going to come back to a topic I did quickly before: tables. I’ll start with what was used in documents to present data in columns before word processing. Before computers, there were typewriters, mechanical devices. On a typewriter, to make columns, the user sets up tabulation positions, usually called tab stops. Each tab stop was a specific physical distance from the edge of the sheet of paper inserted into the machine. There are tab stops in word processing software, but they are rarely used except by very experienced operators. Tab stops in word processing have more options and more flexibility than on typewriters. Tables are even more flexible but require at least as much thinking as tab stops. Tabs can be used in Latex in the tab environment. It is relatively easy to set tab stops at specific distances from the left margin in either the tabbing or the tabto environments. However, if you want information arranged in columns and rows, the tabular environment offers more flexibility and more options.

This is not a simple topic. In The Latex Companion (Third Edition) there’s about seventy pages of detailed information discussing the various table environments and their various extensions. If that seems like more than you want to read, there are eleven pages dedicated to a simplified discussion of the table environment in LATEX2e: An unofficial reference manual available at https://latexref.xyz. These pages cover a selection of the possible extensions and packages for making tables. Remember, Latex is a modular tool. Instead of loading every possible tool and its variations when starting, Latex allows you to use the packages you need for your document when you need them, and it will generate a typeset document (PDF) to your specifications. Ideally tables are used to organize and present information so the contents of the table are clear to the reader. Tables organize information in vertical columns and horizontal rows. A table is a framework, a skeleton into which the writer puts information. A table is described by indicating how many rows and columns it has. Often a table has the same number of columns in each row and the same number of rows in each column. However table cells can be joined in various ways. Table cells are often separated from each other with vertical and / or horizontal lines. All of this is familiar to you, I am sure. In Latex, a table is built inside a table environment. As Erik showed in one of his last columns here is the basic code for a table with some contents (shown above).

{||l|c|c|c|c||} means set up a table with five columns, the left hand column is left-aligned, all the others are centred. The single vertical line between each alignment option indicates a vertical line will be placed between each column. The double line on the left-hand and right-hand ends indicated the outside lines will be doubled. The hline instruction places a horizontal line below each row, the repeated hline instruction makes a double line at the top and bottom of the table. The double back-slash marks the end of a row. As you can probably guess, the & ampersand symbol separates the contents of one cell from its neighbours in its row (see Table 1 in Figure 1 next page). In Latex tables are one of the items which are floated when typeset. That has two consequences for page designers and authors. The first consequence is the table will appear on the page where there is room for it. If there is not room on the current page then it is “floated” to the next page. The second consequence is a table in the tabular environment cannot extend past the end of a page, that is one table has a maximum size of one page. If you need more space than one page for your table, investigate the use of the supertabular and the longtabular environments.

The following example shows how to connect two cells in the same row together. There is a similar instruction for connecting two cells in two different rows. The & after {Ene} is necessary to build the third cell in the first row. \begin{tabular}{|c|c|c|} \hline \multicolumn{2}{|c|}{Ene} &
\hline Mene & Muh! & Curly
\hline \end{tabular} Similar to the parameters for multicol, multirow has three required parameters: how many rows, the width of the resulting cell, and the contents of the spanned cell. The hline instruction between rows one and two is omitted. If it is included it will cross into the spanned cell. A difference which you cannot see from the code below is that multirow is a separate package and must be loaded (\usepackage{multirow}) in the document preamble. Multicolumn is built into the version of Latex I use and into other versions as well. \begin{tabular}{|c|c|} \hline \multirow{2}{14mm}{Ene} & Mene
& Muh!
\hline \end{tabular}

Since I started this column by discussing tabs and tab stops, I am going to finish by showing two methods of getting and using tab stops in Latex. The first way is in the tabbing environment, the second is with the tabto package. (See Figure 2 - next page - for the results of this code.) \begin{tabbing} First \= Second \= Third \=\kill one \> two \> three \> four
one two \> two \> three \> four
\end{tabbing} The first line after the begin{tabbing} instruction sets the tab stops, the distance from the left margin for the start of the text (or other contents). Notice there are five characters plus one space between the margin and the first tab stop. The first item in the first line will display with no problems, it contains three characters and one space. The first item in the second line has six characters and two spaces. It will display as usual. However the second item in the second line starts with five characters and one space from the left margin, and will overwrite part of the first item. The next code example shows how to fix this problem.

\begin{tabbing} First tab stop \= Second \= Third \=\kill one \> two \> three \> four
one two \> two \> three \> four
\end{tabbing} As stated above, tabto is a package but it is not an environment, unlike tabbing. An author needs to load it in the preamble but can use the package without beginning and ending an environment. \tabto*{1cm} Tab 1 \tabto*{3cm} Tab 2 \tabto*{2cm} Tab 3 % in document The distances are distances from the left margin. Notice the text Tab 3 will be printed between Tab 1 and Tab 2. (The asterisk suppresses line breaks.) The tabto package will also allow a user to set a specific number of evenly spaced tabs or specify exactly where each tab stop should be (just as can be done with a typewriter). Both parts of the code below go into the document.

\TabPositions{0,0.25\linewidth,0.5\linewidth,0.75\linewidth} % in preamble Then use \tab in the document. (This is equivalent to \NumTabs{4} \NumTabs{4} % in document %Four evenly spaced tab stops starting at the left margin \tab Tab 1 \tab Tab 2 \tab Tab 3 \tab Tab 4
There is a brief one-page document for the Tabto package available on ctan.org. A little experimenting will be helpful if you are not used to using tab stops. That is enough Latex fun for one issue. I will spend more time working with tables in the next column. In the meantime, if there is anything in this column that is new to you, I encourage you to try it out, play with the code, experiment with the choices, perhaps even read the documentation. Learning something new takes some effort but the rewards with Latex are worth the effort.

issue204/latex.1714888175.txt.gz · Dernière modification : 2024/05/05 07:49 de d52fr