[Tex/LaTex] pgfplotstable multiple columns same heading name

pgfplotstable

I have some x y data that I wish to typeset using pgfplotstable, rather than having say 9 rows of 2 columns, I would like to have 3 rows, with 3 sets of xy pairs.

Consider the following:

\documentclass{article}
\usepackage{pgfplotstable,filecontents}

\begin{filecontents}{mydata.dat}
    x y x y x y
    0 0 3 3 6 6
    1 1 4 4 7 7
    2 2 5 5 8 8
\end{filecontents}

\begin{document}
    \pgfplotstableset{
        columns/x/.style={
             column name={$X$},
        },
        columns/y/.style={
              column name={$Y$},
              column type/.add={}{|}
        },
    }
    \pgfplotstableread{mydata.dat}\loadedtable
    \pgfplotstabletypeset{\loadedtable}
\end{document}

Which produces the following output:

Output

Clearly columns 2 to 6, in this MWE are not being typeset as desired. I am aware that this can be resolved by declaring unique column style for each column as follows:

\pgfplotstableset{
  columns/A/.style={
    column name={$X$},
  },
  columns/B/.style={
    column name={$Y$},
    column type/.add={}{|}
  },
  columns/C/.style={
   column name={$X$},
  },
  columns/D/.style={
    column name={$Y$},
    column type/.add={}{|}
  },
  columns/E/.style={
    column name={$X$},
  },
  columns/F/.style={
    column name={$Y$},
    column type/.add={}{|}
  },
}

Also slightly modifying the input data structure:

\begin{filecontents}{mydata.dat}
    A B C D E F
    0 0 3 3 6 6
    1 1 4 4 7 7
    2 2 5 5 8 8
\end{filecontents}

Producing the desired format, with column names [X Y X Y X Y]:

Desired

But I am trying to avoid having to effectively re-type the same code over and over. How can I use the same pgfplotstableset column style, for multiple columns in the same table.

Best Answer

You can use the every odd column and every even column styles for this (noting that the column numbering starts at zero):

\documentclass{article}
\usepackage{pgfplotstable,filecontents}

\begin{filecontents}{mydata.dat}
    x y x y x y
    0 0 3 3 6 6
    1 1 4 4 7 7
    2 2 5 5 8 8
\end{filecontents}

\begin{document}
    \pgfplotstableset{
        every even column/.style={
            column name={$X$}
        },
        every odd column/.style={
            column name={$Y$},
            column type/.add={}{|}
        }
    }
    \pgfplotstableread{mydata.dat}\loadedtable
    \pgfplotstabletypeset{\loadedtable}
\end{document}