15

I have two questions concerning pgfplotstable and number formatting with pgf. Consider the following piece of code:

\documentclass{article}
\usepackage{pgfplotstable}
\usepackage{amsmath}

\begin{document}
\pgfplotstableread{
A     B        C
1.1   12       1300000
21.1  200000   214300000
}\mytable
\pgfplotstabletypeset[columns={A, B, C},
  columns/A/.style={postproc cell content/.append style={
    /pgfplots/table/@cell content/.add={}{ s}}, fixed zerofill, precision=1
  },
  columns/C/.style={postproc cell content/.append style={
    /pgfplots/table/@cell content/.add={}{ s${}^{-1}$}}, fixed zerofill, precision=1
  } 
]\mytable

\end{document}

I would like to:

  1. align the content of the column A at the decimal separator (obviously I've tried dec sep align but the it messes up with the postproc cell content (it adds the " s" before the dec sep "." - e.g. : 21 s.1 s), other than that it aligns fine,
  2. print the number using engineering notation (the powers of ten must be multiples of three) for the column B - e.g. : $200\:\cdot\:10^{3}$ instead of $2\:\cdot\:10^{5}$)
  3. do all these for the column C: append content, print using engineering notation, align on the \cdot of the engineering notation (like sci sep align)

Do you know some way(s) to achieve any of those goals ?

EDIT Thanks to Jake's answer, I've got almost what I want, however I have one last problem with siunitx. The following piece of code compiles (note the alias to make it compile, otherwise siunitx fails..)

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

\begin{document}
\pgfplotstableset{
  alias/as/.initial=d,
}
\pgfplotstableread{
d
7000000
}\loadedtable
\pgfplotstabletypeset[columns={as},
  columns/as/.style={
    column name={$2 \times 3d$},
  }
]\loadedtable
\end{document}

This next piece doesn't:

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

\begin{document}
\pgfplotstableset{
  alias/as/.initial=d,
}
\pgfplotstableread{
d
7000000
}\loadedtable
\pgfplotstabletypeset[columns={as},
  columns/as/.style={
    column name={$2 \times 3d$},
    column type={S[round-mode=places, round-precision=1, scientific-notation=engineering, table-format=5.1e1, exponent-product = \cdot]},
    string type
  }
]\loadedtable
\end{document}

How to avoid siunitx parse the headers ? (that would also, I guess, make it possible to use column identifiers with the letters 'e' and 'd')

1 Answer 1

16

You could let siunitx take care of the of the alignment and of formatting the numbers: Since version 2.4, siunitx can format numbers to engineering notation.

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

\begin{document}
\pgfplotstableread{
A     B        C
1.1   12       1300000
21.1  200000   214300000
}\mytable
\pgfplotstabletypeset[columns={A, B, C},
  columns/A/.style={
    column type={S[table-format=2.1]},
    string type,
    postproc cell content/.append style={
        /pgfplots/table/@cell content/.add={}{\,s}
    }
  },
  columns/B/.style={
    column type={S[round-mode=figures, round-precision=3, scientific-notation=engineering, table-format=3e1]},
    string type,
    postproc cell content/.append style={
        /pgfplots/table/@cell content/.add={}{\,\si{\per\second}}
    }
  },
  columns/C/.style={
    column type={S[round-mode=figures, round-precision=3, scientific-notation=engineering, table-format=5.2e1]},
    string type,
    postproc cell content/.append style={
        /pgfplots/table/@cell content/.add={}{\,s}
    }
  },
]\mytable

\end{document}

May I suggest a slight alteration to your table setup? Instead of repeating the unit symbol with each value, I would put it into the header. Here's one example of how that can be accomplished. I've also used the booktabs package to make the table a bit easier to grasp:

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

\begin{document}
\pgfplotstableread{
A     B        C
1.1   12       1300000
21.1  200000   214300000
}\mytable
\pgfplotstabletypeset[columns={A, B, C},
  columns/A/.style={
    column type={S[table-format=2.1]},
    string type
  },
  columns/B/.style={
    column type={S[round-mode=figures, round-precision=3, scientific-notation=engineering, table-format=3e1]},
    string type
  },
  columns/C/.style={
    column type={S[round-mode=figures, round-precision=3, scientific-notation=engineering, table-format=3.2e1]},
    string type
  },
  every head row/.style={
    before row={\toprule},
    after row={\si{\second} & \si{\per\second} & \si{\second}\\ \midrule}
  },
  every last row/.style={after row=\bottomrule}
]\mytable

\end{document}

To stop siunitx from trying to parse the column names, issue the key multicolumn names. This will wrap the cells in the head row in \multicolumn{1}{c}{<column name>}, which protects them.

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

\begin{document}
\pgfplotstableread{
d
7000000
}\loadedtable
\pgfplotstabletypeset[
    multicolumn names,
    columns/d/.style={
        column name=$\gamma \times \epsilon$,
        column type={
            S[
                round-mode=places,
                round-precision=1,
                scientific-notation=engineering,
                table-format=1.1e1,
                exponent-product = \cdot
            ]
        },
        string type
  }
]\loadedtable
\end{document}
2
  • 1
    Great thanks! Any idea on how to avoid siunitx to crash when using column names with 'd' or 'e' in them ?
    – user8947
    Commented Jan 20, 2012 at 8:38
  • 2
    You can use multicolumn names. I've edited my answer accordingly.
    – Jake
    Commented Jan 20, 2012 at 9:26

You must log in to answer this question.

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