1

I am importing a matrix of this kind:

0  1  0  0  0 -1  0
0  1  0  1  0  0  0
0  0 -1  0  1  0  0

I would like to add a header row and an index column so that it looks the following way:

   e1 e2 x1 x2 x3 x4 x5
y1 0  1  0  0  0 -1  0
y2 0  1  0  1  0  0  0
y3 0  0 -1  0  1  0  0

I have been trying hard with the pgfplots table, but I have not achieved anything useful.

Any idea is welcomed.

  • 1
    If the first line contains data or [header=false], the columns will be accessed by index number, e.g [index] 0. You can assign names using /columns/{[index]0}/.style={column name=...} – John Kormylo Oct 3 '18 at 5:22
  • 1
    You will need to create a new column (probably as a comma delimited list) for the "row names". and assign it an empty column name. – John Kormylo Oct 3 '18 at 5:27
  • Regarding the first comment I am not sure where I have to put the command. Should it go here? \pgfplotstabletypeset[/columns/{[index]1}/.style={column name=hla}]\connectivitytable – Fabio Oct 4 '18 at 17:24
  • As for the second comment I know how to create a new column however it would be better if one could create the names in a loop because of being concatenated. – Fabio Oct 4 '18 at 17:28
  • In all the examples, there is already a column containing 1,2,3,... One can create a comma delimited list using a foreach loop and \xdef. See tex.stackexchange.com/questions/317347/… for example. – John Kormylo Oct 4 '18 at 18:02
1

Feel free to add more rows or columns in

enter image description here

\documentclass[border=5pt, varwidth]{standalone}
\usepackage{pgfplotstable}

\pgfplotstableread[]{
0  1  0  0  0 -1  0
0  1  0  1  0  0  0
0  0 -1  0  1  0  0
}\test

\pgfplotstablegetrowsof{\test}
\pgfmathsetmacro\LastRow{\pgfplotsretval}
\pgfplotstablegetcolsof{\test}
\pgfmathsetmacro\LastColNo{\pgfplotsretval-1}

\begin{document}
Old: \pgfplotstabletypeset[every head row/.style={output empty row}]{\test} 
\bigskip

% Specify head row
\pgfplotsinvokeforeach{0,1} {%
\pgfplotstableset{
columns/#1/.style = {column name=$e_{\pgfmathparse{int(#1+1)} \pgfmathresult}$},
}}%

\pgfplotsinvokeforeach{2,...,\LastColNo} {%
\pgfplotstableset{
columns/#1/.style = {column name=$x_{\pgfmathparse{int(#1-1)} \pgfmathresult}$},
}}%

% Create new column
\pgfplotstableset{
create on use/newcol/.style={
create col/set list={1,...,\LastRow}
},
columns/newcol/.style={column name={},
postproc cell content/.style={@cell content=$y_{##1}$}
},
}

New: \pgfplotstabletypeset[columns={newcol,0,1,...,\LastColNo},
column type=r,   %string type, 
]{\test}
\end{document}

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.