0

I would like to read data from a .tex file using \pgfplotstableread. The .tex file contains table with \begin etc. tags and I am asking for a clean way to strip this. I can skip the first lines but not the last line.

Here is a minimal example:

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{filecontents}
\begin{document}
\begin{filecontents}{data.tex}
  1 & 2\\
  3 & 4\\
\end{filecontents}
\begin{filecontents}{datatable.tex}
\begin{longtable}{|r|r|}\hline
 a & b\hline
\endhead
  1 & 2\\
  3 & 4\\
\end{longtable}
\end{filecontents}

Works:
\pgfplotstableread[col sep=&,header=false]{data.tex}{\data}

Doesn't work:
\pgfplotstableread[col sep=&,header=false, skip first n={3}]{datatable.tex}{\datatwo}

\end{document}
5
  • Are you sure that reading data.tex works? You will have \\ as part of the second data item; is this intended? \pgfplotstableread is used to read csv-data, not LaTeX tables.
    – gernot
    Commented Jan 25, 2021 at 13:34
  • Yes it works for me, I think you can tell it that lines end with \\ but I didn't need to in my examples (I didn't use the last column). I think it is even in the docs. Alternatively, I could read the file line by and only keep lines starting with a number and pass it to pgfplotstableread. Or is there a package reading the values from a LaTeX table, I am not aware of? I just don't want to reinvent the wheel.
    – Marcel
    Commented Jan 25, 2021 at 15:22
  • Indeed, it seems that one can declare \\ as row separator. Sorry, no idea how to skip the last line directly. I'd filter the tex file with a script before feeding it to \pgfplotstableread. Alternatively, you could read the table as a single column and then filter the rows in LaTeX. pgfplots allows to skip rows by index (/pgfplots/table/skip rows between index={begin}{end}, see pgfplotstable manual), but then you have to split the rows afterwards.
    – gernot
    Commented Jan 25, 2021 at 17:54
  • Okay, I got something working since the data is numerical by ignoring \ and making e a comment character.
    – Marcel
    Commented Jan 26, 2021 at 3:55
  • @gernot, that is also a nice idea! Have read it wrong the first time.
    – Marcel
    Commented Jan 26, 2021 at 3:58

0

You must log in to answer this question.

Browse other questions tagged .