[Tex/LaTex] pgfplotstable manage strings and values

pgfplotspgfplotstablestringstables

I like the pgfplotstable package for reading and plotting tables but handling of strings and values (especially when strings contain spaces) drives me crazy…
I often use a table as shown here:

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.8}
\usepackage{pgfplotstable}
\usepackage{booktabs}
%
\begin{filecontents*}{table.dat}
a   b   c   d   e   f   g
1   Item1   alpha   m   1   1   10
2   Item2   beta    s   1.0 11  20
3   Item3   gamma   kg  10.0    111 30
4   Item4   omega   deg 10.00   1111    40
5   Item5   xi  \%  10.99   11111   50
\end{filecontents*}
\begin{document}
\begin{figure}[hp]
\pgfplotstableread{testMod.dat}\loadTestTable
\pgfplotstabletypeset[
%   header=false,
    brackets/.style={%
        postproc cell content/.append style={/pgfplots/table/@cell content/.add={\relax[}{]}},
    },
    greek/.style={%
        postproc cell content/.append style={/pgfplots/table/@cell content/.add={\textbackslash}{}},
    },
    column type=r,
    columns/a/.style={string type},
    columns/b/.style={string type,column type=l},
    columns/c/.style={string type,column type=l},
    columns/d/.style={string type,column type=l},
    columns/c/.append style={greek},
    columns/d/.append style={brackets},
    fixed,
    fixed zerofill,
    precision=2,
%   dec sep align,
    every head row/.style={%
        output empty row,
        before row={%
            \toprule
            No. & Item & Symbol & Unit & Column1 & Column2 & Column3\\
            },
        after row=\midrule,
        },
        every last row/.style={%
            after row=\bottomrule}]{table.dat}
\end{figure}
\end{document}

I need to have two things:

  1. spaces in strings, i.e. 'Item of first row'
  2. set greek symbols automatically (comment out line 'columns/c/.style={string type,column type=l},' did not work

Thanks for any advice!

Paul

Best Answer

As Jake noted, it is the notorious CSV regexp problem of finding which space is escaped which is used for delimiters etc. So if you want spaces to be respected then use something else for delimiter. But make sure you add trim cells option otherwise your data and column names become whitespace-sensitive.

For the Greek characters, you can use $\csname #1 \endcsname$ syntax which makes #1 a control sequence, say $\csname alpha \endcsname becomes \alpha. (c)ontrol (s)equence name --> \csname

\documentclass{article}
\usepackage{pgfplotstable}
\usepackage{booktabs}
\pgfplotsset{compat=1.8}


\pgfplotstableread[col sep=semicolon,trim cells]{
a ; b      ;  c     ;  d   ;e      ; f     ;  g
1 ; Item 1 ;  alpha ;  m   ;1      ; 1     ;  10
2 ; Item 2 ;  beta  ;  s   ;1.0    ; 11    ;  20
3 ; Item 3 ;  gamma ;  kg  ;10.0   ; 111   ;  30
4 ; Item 4 ;  omega ;  deg ;10.00  ; 1111  ;  40
5 ; Item 5 ;  xi    ;  \%  ;10.99  ; 11111 ;  50
}\loadTestTable

\begin{document}
\begin{table}
\pgfplotstabletypeset[
    brackets/.style={%
        postproc cell content/.append style={/pgfplots/table/@cell content/.add={\relax[}{]}},
    },
    greek/.style={%
        preproc cell content/.append style={/pgfplots/table/@cell content/.add={$\csname}{\endcsname$}},
    },
    column type=r,
    columns/a/.style={string type},
    columns/b/.style={string type,column type=l},
    columns/c/.style={string type,column type=l},
    columns/d/.style={string type,column type=l},
    columns/c/.append style={greek},
    columns/d/.append style={brackets},
    fixed,
    fixed zerofill,
    precision=2,
    every head row/.style={%
        output empty row,
        before row={%
            \toprule
            No. & Item & Symbol & Unit & Column1 & Column2 & Column3\\
            },
        after row=\midrule,
        },
        every last row/.style={%
            after row=\bottomrule}]{\loadTestTable}
\end{table}
\end{document}

enter image description here

Related Question