Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I have a CSV file (db.txt)like this:

30.0,   0.0,    0.0
60.0,   1.9098, 5.8779
90.0,   6.9098, 9.5106
120.0,  13.09,  9.5106
150.0,  18.09,  5.8779
180.0,  20.0,   0.0

I need to typeset it. My problem is related with the wrong alignment of decimal point and the number of decimals (must be a constant for each column):

\renewcommand*{\dtlrealalign}{r}

\DTLloaddb[
    noheader,
    keys={x,y,theta},
    headers={
        \shortstack{$\boldsymbol{\theta_{2,i}}$},
        \shortstack{X},
        \shortstack{Y}
             }
  ]{db}{db.txt}
\begin{table}[t]
  \caption{Database file}
  \centering
  \DTLdisplaydb{db}
\end{table}

Table Snapshot

Any advice, please?

share|improve this question
2  
Is a re-exporting of the csv file into a format that has decimal-aligned zeros a possibility? –  Werner Jul 27 '11 at 18:20

2 Answers 2

up vote 18 down vote accepted

You can use pgfplotstable for this. If you load the array package, you can set the option dec sep align for pgfplotstabletypeset to get the alignment at the decimal point. Padding the numbers with zeros can be accomplished by setting fixed zerofill, the number of decimals can be set using precision, either for all columns or for individual columns by using display column/<index>/precision=<digits>.

pgfplotstable and array

\documentclass{article}
\usepackage{filecontents}       % For the example data
\usepackage{pgfplotstable}
\usepackage{array}      % For aligining at decimal point

\begin{filecontents}{testdata.csv}
theta, x, y
30.0,   0.0,    0.0
60.0,   1.9098, 5.8779
90.0,   6.9098, 9.5106
120.0,  13.09,  9.5106
150.0,  18.09,  5.8779
180.0,  20.0,   0.0
\end{filecontents}

\begin{document}
\pgfplotstableread[col sep=comma]{testdata.csv}{\table}

\pgfplotstabletypeset[
    dec sep align,      % Align at decimal point
    fixed zerofill,     % Fill numbers with zeros
    precision=4,        % Set number of decimals
    display columns/0/.style={precision=1}, % Change for first column (column 0)
    ] {\table}
\end{document}

Alternatively, you can use pgfplotstable and the siunitx package to align the columns. I've used it here in conjunction with booktabs and properly formatted column headers:

pgfplotstable and siunitx

\documentclass{article}
\usepackage{pgfplotstable}
\usepackage{siunitx}
\usepackage{booktabs}

\begin{filecontents}{testdata.csv}
theta, x, y
30.0,   0.0,    0.0
60.0,   1.9098, 5.8779
90.0,   6.9098, 9.5106
120.0,  13.09,  9.5106
150.0,  18.09,  5.8779
180.0,  20.0,   0.0
\end{filecontents}

\begin{document}
\pgfplotstableread[col sep=comma]{testdata.csv}{\table}

\pgfplotstabletypeset[
    dec sep align=S,    % Use the siunitx `S` column type for aligning at decimal point
    fixed zerofill,     % Fill numbers with zeros
    precision=4,        % Set number of decimals
    display columns/0/.style={
        precision=1,    % Change for first column (column index 0)
        column name=$\theta_{2,i}$
    },
    display columns/1/.style={column name=$X$},
    display columns/2/.style={column name=$Y$},
    every head row/.style={before row=\toprule, after row=\midrule},
    every last row/.style={after row=\bottomrule},
    ] {\table}
\end{document}
share|improve this answer
    
Thanks Jake, it works better than I expected ! –  Fernando Nadal Jul 28 '11 at 12:57
    
One question, why I can't name any column like column name=$i$? I think that siunitx package think it's a complex number or.. –  Fernando Nadal Jul 28 '11 at 14:20
    
You can use ${i}$ instead, to hide the imaginary unit from siunitx. –  Jake Jul 28 '11 at 23:04

There's no problem to do this with the datatool package and siunitx as well. Here's your example done in that way:

\documentclass{article}
\usepackage{datatool}
\usepackage{siunitx}
\usepackage{booktabs} % for nicer tables
\usepackage{caption} % improve caption spacing (among other things)
\usepackage{bm}
\renewcommand*\dtldisplaystarttab{\toprule}
\renewcommand*\dtldisplayendtab{\tabularnewline\bottomrule}
\renewcommand*\dtldisplayafterhead{\midrule}

\begin{document}
\DTLloaddb
 [
    noheader,
    keys={x,y,theta},
    headers={
    \shortstack{$\boldsymbol{\theta_{{2,i}}}$},
    \shortstack{X},
    \shortstack{Y}}
  ]
  {db}{db.txt}
\begin{table}[t]
\sisetup{
    parse-numbers   = false,
    table-number-alignment = left,
    table-figures-integer = 4,
    table-figures-decimal = 4,
    input-decimal-markers = .
    }
    \renewcommand*\dtlrealalign{S}
  \caption{Database file}
  \centering
  \DTLdisplaydb{db}
\end{table}

\end{document}

output of code

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.