Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Consider the following plots

\documentclass{article}
\usepackage{pgfplots}

\begin{document} 

\pgfplotstableread{
1 3 6 4
2 2 5 2
3 4 4 1
4 5 2 3
}\table

\begin{tikzpicture}
  \begin{axis}
    \foreach \i in {1,...,3}{%
      \addplot table[x index=0,y index=\i] {\table};
                            }
  \end{axis}
\end{tikzpicture}

\end{document}

I would like to use it as a template for input files of varying number of columns. For this I need to replace the 3 ine the foreach command by the number of columns of the loaded table. Is there something similar to \pgfplotstablecols that would be understood in this environment?

share|improve this question

1 Answer 1

up vote 8 down vote accepted

There is \pgfplotstablegetcolsof{<table>}, which stores the number of columns of the given table in the macro \pgfplotsretval. You'll need to subtract one from this number to get the correct index of the last column:

\documentclass{article}
\usepackage{pgfplots}

\begin{document} 

\pgfplotstableread{
1 3 6 4 2
2 2 5 2 1
3 4 4 1 5
4 5 2 3 1
}\table

\begin{tikzpicture}
  \begin{axis}
  \pgfplotstablegetcolsof{\table}
  \pgfmathparse{\pgfplotsretval-1}
    \foreach \i in {1,...,\pgfmathresult}{%
      \addplot table[x index=0,y index=\i] {\table};
                            }
  \end{axis}
\end{tikzpicture}

\end{document}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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