18

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?

1
  • 2
    Is a re-exporting of the csv file into a format that has decimal-aligned zeros a possibility?
    – Werner
    Jul 27, 2011 at 18:20

2 Answers 2

20

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}
2
  • 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.. Jul 28, 2011 at 14:20
  • You can use ${i}$ instead, to hide the imaginary unit from siunitx.
    – Jake
    Jul 28, 2011 at 23:04
9

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .