7

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

  • 1
    For 1., you can either use a column separator that's not a space (col sep=comma, for example), or enclose the string in {...} ({Item of first row}). For 2. you can similarly put {$\alpha$} into your table instead of just alpha. – Jake Jul 19 '13 at 12:23
7

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

  • Thank you! The 'trim cells' option helps a lot. It also works for tabbed separated tables. Control sequences is smooth way to do handle Greek symbols. Is there a similar sequence if I just want to set a math environment '$'? – Paul Jul 22 '13 at 9:48
  • @Paul You can simply use $ in the preproc. Am I misundertanding your question? – percusse Jul 22 '13 at 10:01
  • No,you don't. I just want to get rid of any preproc, e.g. column-wise adding '$' to the tables beforehand. That can be exhausting when getting tables from everywhere. It would be nice to do it with pgfplotstable directly. – Paul Jul 22 '13 at 16:16
  • @Paul Well, if you rename your style somewhere in the preamble with some short name such as mmode you can just use that simple keyword instead of all that key copy/pasted over and over again. Otherwise I don't get your last sentence because this is the most direct I can think of. – percusse Jul 22 '13 at 18:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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