0

I know that I can create a new column via

create on use/newCol/.style={
    create col/set list={Some content, Some other content}
},
columns/newCol/.style={string type},
columns = {newCol, [index] 0, [index] 1, [index] 2, [index] 3},

That is all well but what if I don't know how many columns the table has in total?
Is there a way to simply (pre)append the new column to the existing ones without having to specify all other columns in the table?

2 Answers 2

0

Since you don't know the number of the columns, I presume you're reading a table from an external file.

In that case, you could simply add a normal one-column table before your pgfplotstable.

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

\begin{filecontents*}{\jobname.dat}
    x y
    1 2
    2 3
    3 4
    4 5
\end{filecontents*}

\pgfplotstableset{% global config, for example in the preamble
    every last row/.style={after row=\bottomrule}
}

\begin{document}
    \begin{table*}
        \begin{tabular}{c}
            \toprule
            This\\
            \midrule
            is \\
            a column \\
            added \\
            before \\
            \bottomrule
        \end{tabular}%
        \pgfplotstabletypeset[
            every head row/.style={before row=\toprule,after row=\midrule}
            ]{\jobname.dat}
    \end{table*}
\end{document}

enter image description here

1

Maybe I missunderstood the task, but this seems to be no problem:

enter image description here

\documentclass{article}
\usepackage{pgfplotstable}
\pgfplotsset{compat=newest}
\begin{document}
\section{Example-Table with unknown / random Number of Columns and Rows}
\subsection{Raw Data}
\pgfmathsetmacro\xrand{random(3, 7)}
\pgfmathsetmacro\yrand{random(5, 11)}
\xrand\, colums and \yrand\, rows. 

\newwrite\mytable
\immediate\openout\mytable=\jobname-table.txt%
\newcommand{\row}{}% reserv Name
\foreach \Y  in {1,...,\yrand}{%%%
\let\row=\empty% create List
\foreach \X  in {1,...,\xrand}{%%
\pgfmathparse{\Y}
  \ifx\empty\row{} \xdef\row{\pgfmathresult}%
  \else \xdef\row{\row,\pgfmathresult}%
  \fi
}%%
\noindent\row \\ % show
\immediate\write\mytable{\row}% save
}%%%
\immediate\closeout\mytable

%Test: \pgfplotstabletypeset[string type, col sep=comma]{\jobname-table.txt}

\subsection{Add a new Column}
\pgfplotstableset{create on use/NewCol/.style={
create col/set list={a,b,c,d},
},
columns/NewCol/.style={string type},
}

\pgfmathsetmacro\Xrand{\xrand-1}
\pgfplotstabletypeset[col sep=comma, 
columns={NewCol,0,1,...,\Xrand},
empty cells with={---},
every head row/.style={after row=\hline}
]{\jobname-table.txt}
\end{document}

Note, that the part \newwrite\mytable ....... has nothing to do with the problem itself. It is only a "sophisticated" method of creating an arbitrary table with an unknown or random number of rows and columns.

0

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.