From spreadsheets to LaTeX tables

Definition of the target

You've a table with some columns and rows (in the example: four columns and six rows) in a spreadsheet.
You need to transpose the table in a LaTeX document.
Yes, I know! You could use the features of an IDE to add speedly everything you need.
But considering that we are inside a worksheet, why not use the column management functions of the worksheet itself? To reach the target directly from the spreadsheet, you can proceed in three steps.

  1. The first step is entirely inside the spreadsheet.
  2. The second one consist of the transfer of the prepared data to the LaTeX document.
  3. The third is the completion of the code in LaTeX.

    First Step: data preparation

Here is the starting point:

Starting Point

Using the column insertion functions of each spreadsheet, you have to insert a column of ampersands (&) between each column of data:

image

image

A column of double backslashes (\\) has to be added to the right of the last data column:

image

This is the coded version of the data prepared in the worksheet:


YEAR    &   DATA1   &   DATA2   &   DATA3   \\
2012    &   12345   &   54321   &   54321   \\
2013    &   23456   &   65432   &   65432   \\
2014    &   34567   &   76543   &   76543   \\
2015    &   45678   &   87654   &   87654   \\
2016    &   56789   &   98765   &   98765   \\
2017    &   67890   &   87654   &   87654   \\

End of first step. Now you need to bring the entire worksheet inside LaTeX.

Second Step: data transport

When the data preparation is finished, you can simply copy and paste it from the spreadsheet to the LaTeX document.

You can load data without paying attention to columns: the important thing is that data includes the reference characters, i.e. ampersand and backslash:


  YEAR    & DATA1   &   DATA2   &   DATA3   \\
  2012  &   12345       &   54321   &   54321   \\
2013    &   23456   &   65432   &     65432 \\
  2014  &   34567   &   76543     & 76543     \\
  2015  &   45678   &   87654   &   87654   \\
2016    &   56789   &       98765   &   98765     \\
  2017  &     67890 &   87654   &   87654   \\

All spacing recalculation and alignment work is carried out by the automatic layout algorithms of LaTeX.

Third Step: completing the code

All you have to do is complete the code with the elements required for the source processing.

Then you can move the column headers under the toprule; adding e midrule; adding a caption and a label and giving the formatting instructions (in the example the centering and the cccc sequence of characters.


\begin{table}[hbt]
    \caption{Test Table}
    \centering
    \begin{tabular}{cccc}
    \toprule
    YEAR  & DATA1 & DATA2   & DATA3 \
    \midrule
  YEAR    & DATA1   &   DATA2   &   DATA3   \
  2012  &   12345       &   54321   &   54321   \
2013    &   23456   &   65432   &     65432 \
  2014  &   34567   &   76543     & 76543     \
  2015  &   45678   &   87654   &   87654   \
2016    &   56789   &       98765   &   98765     \
  2017  &     67890 &   87654   &   87654   \
\bottomrule\\
\end{tabular}
\label{tab:table}
\end{table}

The amazing result!

Now launch the compilation and marvel at the result!

A brief comment: focus on content

Almost everything in LaTeX is planned to be as efficient as possible.
The management of the tables is no exception.
I mean: in LaTeX you don't have to measure the spaces in the table data because the system itself takes care of it for you.
You don't have to worry about aligning the data: the program guarantees the best possible alignment.
This way you can focus on the content without wasting time adjusting the table.
You just have to put some specific characters between the data, in a simple frame, and the program will do the job for you.
The specific characters for the tables are the ampersand and the backslash.

Thank you for your attention.

Originally published at my Tumblr blog

Comments

Popular posts from this blog

Vim: Cut, Copy and Paste to and from the system clipboard

Vim: searching for text containing a slash or a question mark