Outils pour utilisateurs

Outils du site


issue182:latex

Ceci est une ancienne révision du document !


Tables were introduced in Part 5. To review, here (top right) is a simplified version of the code needed to produce the table on p. 23 of FCM#171.

This will produce a table without the title and without the column for minimum temperatures. There are also no Fahrenheit temperatures and no degree symbols. There is one column for each of the four months and one row for each municipality. To get the fancier features we have at least two choices. We could code the fancy features in Latex or we could build the table in a spreadsheet and export it to Latex. Since the point of these articles is to help you learn Latex we will choose the first alternative.

Converting tables to Latex: • For those who like using spreadsheets there is an Add-In for Excel that will export a selected block of cells into a CSV file that is compatible with Latex. • There is an extension for LibreOffice Calc called calc2latex but it has not been updated since 2008. Apparently it worked with LibreOffice 2. I tried it in LibreOffice 7. First it refused to run because of a coding error and then it crashed Calc. • You could build the table in Calc and then Save As a csv file. Then import the csv file into Latex. • You could build the table in Calc and export into a graphic file format, jpeg/jpg or pdf are acceptable in Latex. Then import the graphic into your Latex document. • You can build this table in LibreOffice Write since it is just text and values. (Tables that require calculations cannot be built in Write.) The WritertoLatex extension works well in Write although it will generate a tex file that may contain more complex Latex instructions than required. In this example (above) I have replaced the code from Part 5 with the same table built in Calc and saved as a csv file.

I replaced the commas in the generated CSV file with space&space. Do that in a text editor in the CSV file so you do not replace commas in other parts of your Latex document. As noted in earlier issues, Tex / Latex requires either two back slashes or two presses of the <Enter> key to generate a line break. In a table, a line break moves down to the next row in the table.

This is an adequate table, roughly 50% of the desired result. The goal is a table like this (above). (If we wanted to get very fancy we could add the Fahrenheit temperatures as a separate line in each cell.)

Two things to do next: • Set the widths of all the columns. • Merge the pairs of columns for each month.

To set column widths, this code is altered: \begin{tabular}{||l|c|c|c|c|c|c|c|c|}

Keep the alignment instructions, the “l” and the “c”, and add a new instruction “p” with a size and a unit. I am going to set the Max and Min columns to 1.5 cm and the remaining width to the left hand column. (Other units are available, investigate those yourself.)

\begin{tabular}{|lp{2.4cm}|cp{1.5cm}|cp{1.5cm}|cp{1.5cm}|cp{1.5cm}|cp{1.5cm}|cp{1.5cm}|cp{1.5cm}|cp{1.5cm}|}

If you do not like the vertical lines between columns remove the “|” in the \begin{tabular} command.

Now comes the tricky part: merging the four pairs of Max-Min columns and keeping the eight columns of temperatures in the correct places. Here is the start of the new line of code with explanation.

Municipality& \multicolumn{2}{c}{January}&

Municipality is the text for the current Row 1 Column 1 cell. ( R1C1). The ampersand “&” indicates the start of the next cell in this row.

\multicolumn is followed by three arguments. • The first argument {2} indicates how many columns are to be merged. • The second one {c} indicates the alignment of the text (l, c, r are possible). • The third {January} is the contents of this multicolumn cell.

Now you should understand everything in the following line of code. The other three columns, April, July and October, follow the same pattern as January.

Municipality&\multicolumn{2}{c}{January}&\multicolumn{2}{c}{April}&\multicolumn{2}{c}{July}&\multicolumn{2}{c}{October}

(Remember the double back-slash indicates the end of the row.)

Make sure the total number of merged and not-merged columns in the row with the multicolumn command matches the number of columns in the \begin{tabular} command. In this row we have 1+2+2+2+2 = 9 and the \begin{tabular} command has 1 – l and 8 – c which also equals 9.

We need to add a title to our table and then we will be done. That can be done using the \caption command. That will automatically add the word “Table” and a sequence number, something like “Table 1”. Since this is a one page – one table document, we do not need Table numbering. It can also be done by adding a multicolumn row before the line starting “Municipality”

\multicolumn{9}{c}{Average Temperatures in Four BC Locations} should work.

The test is always: will the code compile and generate the desired layout in the PDF file.

Please note:

The \multicolumn command applies only to the row in which it appears. There is a \multirow command which requires the use of the multirow package. You can investigate that if you wish.

There are at least two other ways to merge cells in a Tex table.

Next issue: packages in Latex / Tex

issue182/latex.1656314672.txt.gz · Dernière modification : 2022/06/27 09:24 de auntiee