# Create LaTeX tables from plain numerical data?

I have a bunch of MATLAB results like this

                   0  -2.000000000000000   2.000000000000000
1.000000000000000  -1.926829268292683   3.707317073170731
2.000000000000000   0.716910656478955  -6.475399900458298
3.000000000000000   0.718921416773616   0.516843960338833


Here leftmost column is always 0,1,2,3,... so I have to get rid of those trailing zeros or to do row-numbering in LaTeX somehow.

However I want to convert all of them results to something like

But anything simple and informative would do. I generated the table above using MathType editor, so I'm not even sure what some of those commands mean

\begin{array}{*{20}{c}}
i&\vline& {{x_i}}&{{y_i}}\\
\hline
0&\vline& {{\rm{ - 2}}{\rm{.000000000000000}}}&{{\rm{2}}{\rm{.000000000000000}}}\\
1&\vline& {{\rm{ - 1}}{\rm{.926829268292683}}}&{{\rm{3}}{\rm{.707317073170731}}}\\
2&\vline& {{\rm{0}}{\rm{.716910656478955}}}&{{\rm{ - 6}}{\rm{.475399900458298}}}\\
3&\vline& {{\rm{0}}{\rm{.718921416773616}}}&{{\rm{0}}{\rm{.516843960338833}}}
\end{array}


What would you suggest?

-
Related Question: Divide table cells by a certain amount. I would use the collcell pacakge so that the first column was truncated to an integer value, and simply r align the other two columns. – Peter Grill Feb 15 '13 at 22:03
Do not use “MathType editor” again. Its output uses wrong syntax (\rm is an argument-less switch not a one-argument-taking macro) and deprecated commands (again: \rm is old). You could typeset numbers very comfortable with siunitx (in addition to datatool and/or pgfplotstable (which also has the PGF math macros and a conditional to check whether a number is an integer)). siunitx has the zero-decimal-to-integer option. – Qrrbrbirlbel Feb 15 '13 at 22:36
Probably an easy way might be to export the data to csv and use the excel converter plugin. – Latex Newbie Feb 20 '13 at 17:08

## 3 Answers

pgfplotstable's syntax takes a bit of getting used to, but then it can really speed up your work, especially because it can read data files straight from the file system. Here's an example using your data:

\documentclass{article}
\usepackage{pgfplotstable, booktabs}
\begin{document}
\pgfplotstableread{ % Read the data into a macro we call \datatable
0  -2.000000000000000   2.000000000000000
1.000000000000000  -1.926829268292683   3.707317073170731
2.000000000000000   0.716910656478955  -6.475399900458298
3.000000000000000   0.718921416773616   0.516843960338833
}\datatable
% If your data is in a file called data.csv, you could also just do:
%\pgfplotstableread{data.csv}\datatable

\pgfplotstabletypeset[
columns/0/.style={column name={$i$}}, % Set the name to be used for the first column
columns/1/.style={
column name=$x_i$,  % ... and the second
dec sep align,      % align on the decimal marker
/pgf/number format/fixed zerofill,  % print trailing zeros
/pgf/number format/precision=14     % print 14 digits
},
columns/2/.style={
column name=$y_i$,
dec sep align,
/pgf/number format/fixed zerofill,
/pgf/number format/precision=14
},
every head row/.style={
before row=\toprule,    % booktabs rules
after row=\midrule
},
every last row/.style={
after row=\bottomrule
}]{\datatable}
\end{document}

-

If you are more or less in a hurry and do not want to delve too much into the works of datatools and/or pgfplotstable and your data is final, I would use the raw data and construct the tabular (with siunitxS column) manually.

With a decent editor you can column-select the space between your numbers and simply add & and \\ where needed.

The siunitx setup is pretty easy:

• table-format: specifies numbers of integer digits, decimal digits, …
• table-auto-round: rounds the input number according to table-format
• zero-decimal-to-integer: formats any trailing .0000… to a integer number

(This is actually not needed as we already set the format for the first column to zero decimals and use the table-auto-round mode.)

In the example I set the x_i column to round at the fifth decimal, the y_i prints the decimals as they are, the rounding option has to be set off/false, because the number is too big for TeX.

I also used the booktabs package for nice tables but if you having other tabulars already in your document stay consistent!

Note that I have used a tabular not an array (math-mode). You need to add the table float environment and a table caption yourself (as usual).

The columns’ headers have to be hidden from the siunitx parser with a set of { } braces.

## Code

\documentclass{article}
\usepackage{siunitx,booktabs}
\sisetup{
table-auto-round
}
\begin{document}
\begin{tabular}{S[table-format=1.0]S[table-format=-1.5]S[table-format=-1.15,table-auto-round=false]}
\toprule
{$i$}             & {$x_i$}            & {$y_i$}            \\ \midrule
0                 & -2.000000000000000 & 2.000000000000000  \\
1.000000000000000 & -1.926829268292683 & 3.707317073170731  \\
2.000000000000000 & 0.716910656478955  & -6.475399900458298 \\
3.000000000000000 & 0.718921416773616  & 0.516843960338833  \\ \bottomrule
\end{tabular}
\end{document}


## Output

-
Thanks man. It looks much better than what I came up with. I just added group-digits=false and it's good to go – Kapitonas Feb 15 '13 at 23:32
Thanks once again! And sorry I edited out the original comment question (I found an answer before seeing your response) – Kapitonas Feb 15 '13 at 23:38

You can use the pgfplotstable package. It will let you read in datafiles and is able to output the data in many ways. Have a look through the manual. There are many examples for you to try.

Another option is the datatool package.

-
I don't want to be rude, but maybe you can give any working example for my data in the original post? My job has to be finished in the morning and it's almost 1AM in my country. I am looking at the documentation, but without prior knowledge it might be too hard in this situation. – Kapitonas Feb 15 '13 at 22:40