6.3 Calcular anchos de columna

As described in Appendix A.3, LaTEX has two distinct modes for setting text: LR mode, in which the text is set in a single line, and paragraph mode, in which text is broken into lines of a specified length. This distinction strongly influences the design of the LaTEX table commands. The l, c, or r column types as well as the w and W types added by the array package, specify table entries set in LR mode, whereas p and the array package m and b types specify table entries set in paragraph mode.

The need to specify the width of paragraph mode entries in advance sometimes causes difficulties when setting tables, especially when the table also contains columns whose width is calculated by LaTEX. We describe several approaches that calculate the required column widths based on the required total width of the table and/or the table contents.

If we can predefine all columns, e.g., if we have only paragraph mode columns, then calculating the necessary column widths is a matter of a simple formula: subtract from the available space the amount taken up by the column gaps and divide that by the number of columns we want.

As an example we define the environment tabularc that can generate a table with a defined width (first argument) having a number of p columns (second argument) with equal width matching the total width of the table. This code uses for the calculation discussed in Appendix A.2.5 on page →II 657; alternatively, we could have used the calc package (from Appendix A.5.2, page →II 687).

The number of columns (let us call it x) is used to calculate the actual width of each column by subtracting two x times the column separation and (x + 1) times the width of the rules from the width of the table. The remaining distance is divided by x to obtain the width of a single column. The contents of the columns are centered, and hyphenation of the first word is allowed. Because of , we have to use the command in the table body, as discussed in Section 6.2.2. The alternative would have been to add in the preamble declaration. Instead of always using p columns, we could have used different column specifiers (including w or W), the only requirement being that it is a specifier that needs a width.

6.3.1 tabularx - Cálculo automático de anchos de columna

The package tabularx (by David Carlisle) implements a version of the tabular* environment in which the widths of certain columns are calculated automatically depending on the total width of the table. The columns whose widths are automatically calculated are denoted in the preamble by the X qualifier. The latter column specification is converted to p{some value} once the correct column width has been calculated.

Narrow columns often require a special format, which may be achieved using the > syntax. Thus, you may give a specification like >{}X. Another format that is useful in narrow columns is ragged right. As noted earlier, one must use the command to end the table row if the last entry in a row is being set ragged right. This specification may be saved in a new column specifier (perhaps additionally adding to make \ denote the end of a row again). You may then use this column specifier in a tabularx preamble argument.

The first example shows as a table with three multiline columns of equal width using the mentioned adjustments.

6.3.2 tabulary - Anchos de columna según el contenido

An alternative algorithm for determining column widths is provided by the tabulary package (also written by David Carlisle) providing the tabulary environment. It is most suitable for cases in which the column widths must be calculated based on the content of the table. This often arises when you use LaTEX to typeset documents originating as SGML/XML or HTML, which typically employ a different table model in which multiline material does not have a prespecified width and the layout is left more to the formatter.

The tabulary package provides the column types shown in Table 6.3 on the facing page plus those provided by the array package in Table 6.2 on page 439, and any other preamble options defined via .

6.3.3 Diferencias entre tabular, tabularx y tabulary

All three of these environments take the same arguments, with the goal of producing a table of a specified width. The main differences between them are described here:

  • tabularx and tabulary modify the widths of the columns, whereas tabular* modifies the widths of the inter-column spaces.

  • The tabular and tabular* environments may be nested with no restrictions. However, if one tabularx or tabulary environment occurs inside another, then the inner one must be enclosed within { }.

  • The bodies of tabularx and tabulary environments are, in fact, the arguments to commands, so certain restrictions apply. The commands \verb and \verb* may be used, but they may treat spaces incorrectly, and their arguments cannot contain a % or an unmatched { or }.

  • tabular* uses a primitive capability of TEX to modify the inter-column space of an alignment. tabularx has to set the table several times as it searches for the best column widths and is therefore much slower. tabulary always sets the table twice. For the latter two environments the fact that the body is expanded several times may break certain TEX constructs. Be especially wary of commands that write to external files, because the data may be written several times when the table is reset.

  • tabularx attempts to distribute space equally among the X columns to achieve the desired width, whereas tabulary attempts to allocate greater widths to columns with larger entries.

6.3.4 Gestión de tablas con entradas amplias

If a table has spanning entries that are wider than the columns they span, the white space between these narrow columns is not evenly distributed. For instance, the following table has a rather wide first row, above a series of narrow columns. As a result the space between the last two columns receives all the excess white space, which is not a very pleasing result.

6.3.5 widetable - Una alternativa a tabular

The standard LaTEX tabular* environment produces a table of a predefined width by spreading the columns apart, i.e., adding extra flexible space between the columns. This space has to be specified with an declaration within an @ or ! specifier in the table preamble. This works well enough for tables without vertical rules but looks somewhat strange if such rules are present because the extra space is added only on the right side of the rules separating two columns.

Below is a variation of Example 6-3-3 from page 449 (used there with tabularx) to allow for comparison. This time we explicitly ask for two 32mm wide p-columns followed by a centered one, and, as you can see, the space distribution looks rather bad because of the rules. We also add a heading line to show what happens with a cell spanning two columns.