[Tex/LaTex] Define and use column styles in pgfplotstable

pgfplotspgfplotstable

Is it possible to define custom styles and use them to display columns?

I have 10 columns, 3 of which are strings and the rest are floating point numbers with a series of options I've defined. I'd like to define both styles (one for the strings and one for the numbers) and reference them in my TeX code for each column instead of copying and pasting them multiple times.

Best Answer

Yes, this is certainly possible, as demonstrated in, for example, text size in pgfplotstable tables

In the code below, the relevant parts are:

% reduce repetition of column styles
\pgfplotstableset{
    mittelmania siunitx column/.style={%
        /pgfplots/table/display columns/#1/.style={%
            string type,column type=S,
        }
    },
    mittelmania string column/.style={%
        /pgfplots/table/display columns/#1/.style={%
            string type,
        }
    }
}

which are then used with

mittelmania siunitx column/.list={0,2},
mittelmania string column/.list={1},

Remember that pgfplotstable column indexes start at 0.

If you use the line

outfile=myfile.tex

then you'll see that the output is, as expected:

\begin {tabular}{ScS}%
11.1&mittlemainia&12.1\\%
11.2&mittlemainia&12.2\\%
11.3&mittlemainia&12.3\\%
11.4&mittlemainia&12.4\\%
11.5&mittlemainia&12.5\\%
11.6&mittlemainia&12.6\\%
11.7&mittlemainia&12.7\\%
11.8&mittlemainia&12.8\\%
11.9&mittlemainia&12.9\\%
11.10&mittlemainia&12.10\\%
\end {tabular}%

Here's a complete MWE:

% arara: pdflatex
% !arara: indent: {overwrite: yes}
\documentclass{article}

\usepackage{pgfplotstable}
\usepackage{siunitx}
\usepackage{filecontents}

\begin{filecontents}{data.txt}
    11.1  mittlemainia 12.1
    11.2  mittlemainia 12.2
    11.3  mittlemainia 12.3
    11.4  mittlemainia 12.4
    11.5  mittlemainia 12.5
    11.6  mittlemainia 12.6
    11.7  mittlemainia 12.7
    11.8  mittlemainia 12.8
    11.9  mittlemainia 12.9
    11.10 mittlemainia 12.10
\end{filecontents}

\pgfplotstableread{data.txt}\mittelmaniaData

% reduce repetition of column styles
\pgfplotstableset{
    mittelmania siunitx column/.style={%
        /pgfplots/table/display columns/#1/.style={%
            string type,column type=S,
        }
    },
    mittelmania string column/.style={%
        /pgfplots/table/display columns/#1/.style={%
            string type,
        }
    }
}

\begin{document}

\pgfplotstabletypeset[
    % remember that columns start at 0
    mittelmania siunitx column/.list={0,2},
    mittelmania string column/.list={1},
    % outfile=myfile.tex, % use this line to see the output
]{\mittelmaniaData}

\end{document}