[Tex/LaTex] How to combine engineering notation from siunitx with pgfplotstable

engineeringpgfplotstablesiunitx

I'm trying to get engineering notation (i.e. scientific notation in which the powers of ten must be multiples of three) in my tables produced by pgfplotstable.
Because pgfplotstable does not support this kind of notation, I'd like to use the siunitx-package to achieve this.
This is my example data-file called "testje.dat":

-0.974e-4 -7.46e-3
-0.948e-3 -7.45e-3
-0.923e-3 -7.43e-3
-0.845e-3 -7.4e-3

And this is my LaTeX-code:

\documentclass{article}
\usepackage{siunitx}
\usepackage{pgfplotstable}
\begin{document}
\pgfkeys{
   /pgfplots/table/assign cell content/.code={%
    \def\pgfmathresult{#1}%
    \ifx\pgfmathresult\pgfutil@empty
        \pgfkeyslet{/pgfplots/table/@cell content}\pgfmathresult
    \else
        \pgfkeyssetvalue{/pgfplots/table/@cell content}{%
            \num{scientific-notation = engineering}{#1}%
        }%
    \fi
},
}
Now I'd like my table-values to be in engineering notation
\begin{table}[h!]
 \begin{center}
   \pgfplotstabletypeset[
    col sep=space,
    display columns/0/.style={column name=$U_O$, column type=|c|},
    display columns/1/.style={column name=$U_I$, column type=c|,verbatim},
    every head row/.style={before row=\hline,after row=\hline},
    every last row/.style={after row=\hline},
   ]{testje.dat}
 \end{center}
\caption{Auto generated table from space separated values file
\label{tab:data}}
\end{table}
\end{document}

When trying to compile this by LaTeX I get the following errors:

siunitx error: "invalid-token-in-number"    
Invalid token 's' in numerical input.    
See the siunitx documentation for further information.
For immediate help type H <return>.    
l.30    ]{testje.dat}    
? H    
Numbers can only contain tokens defined using the 'input-...' options:    
the token 's' is not set up as a valid part of a number.

Could anyone here help me solving this issue?

thanks in advance
hugo

Best Answer

Instead of rolling your own code for typesetting the numbers using the \num macro, you can just use the S column type that's provided by siunitx (note that you need to additionally use the string type keyword). That also allows you to use the table-format alignment options:

\documentclass[border=5mm]{standalone}
\usepackage{siunitx}
\usepackage{pgfplotstable}
\usepackage{booktabs}
\begin{document}

   \pgfplotstabletypeset[
    columns/0/.style={
        column name=$U_O$,
        string type,
        column type={S[scientific-notation = engineering, table-format=-3.1e-1]},
    },
    columns/1/.style={
        column name=$U_I$,
        string type,
        column type={S[scientific-notation = engineering, table-format=-1.2e-1]},
    },
    every head row/.style={before row=\toprule,after row=\midrule},
    every last row/.style={after row=\bottomrule},
   ]{
-0.974e-4 -7.46e-3
-0.948e-3 -7.45e-3
-0.923e-3 -7.43e-3
-0.845e-3 -7.4e-3
   }
\end{document}

As Joseph Wright noted in his comment, it might be a better idea to divide all the numbers in each column by a common factor to keep the table more readable. You can do this on the fly using the key preproc/expr={##1 / 1e-6}, for instance:

\documentclass[border=5mm]{standalone}
\usepackage{siunitx}
\usepackage{pgfplotstable}
\usepackage{booktabs}
\begin{document}

   \pgfplotstabletypeset[
    multicolumn names,
    columns/0/.style={
        column name=$U_O / 10^{-6}$,
        string type,
        column type={S[scientific-notation = engineering, round-mode=figures, round-precision=3, table-format=-3.1]},
        preproc/expr={##1/1e-6}
    },
    columns/1/.style={
        column name=$U_I / 10^{-3}$,
        string type,
        column type={S[scientific-notation = engineering, round-mode=figures, round-precision=3, table-format=-1.2]},
        preproc/expr={##1/1e-3}
    },
    every head row/.style={before row=\toprule,after row=\midrule},
    every last row/.style={after row=\bottomrule},
   ]{
-0.974e-4 -7.46e-3
-0.948e-3 -7.45e-3
-0.923e-3 -7.43e-3
-0.845e-3 -7.4e-3
   }
\end{document}
Related Question