I need to skip the first row of a csv file, which is auto-generated by some magic measurement apparatus. It contains its own filename as the first line. The second line contains the column names (no problem) and then the data follows:

"F_1k", ....
134.56, ....

I have found the method of reading the whole file line by line and then writing every line but the first one to a temporary file, but that seems brutal. TH.'s answer to File input and output can be modified to do just that.

As far as I know, pgfplots and datatool can not skip lines of a csv file.

Manually removing the first line is not an option, I have several hundreds of these beasts. I also try to avoid external programs because the resulting code will be used on different platforms.

2 Answers 2


The code below adds a key to \DTLloaddb, which I called omitlines (please propose a better name): it is an integer which tells us how many lines to omit from the beginning of the file. Removing the lines is done through a \loop...\repeat construction, before the datatool really starts parsing the file (but after that file is opened).

The filecontents package is not part of the solution, but is useful to produce external files from a TeX file (and have a self-contained MWE).


% The idea is to add a key to \DTLloaddb which gives
% the number of lines that should be skipped. Then
% use etoolbox to patch the internal command \\@dtlloaddb
% which does the file loading, and add a loop at the right
% place to discard some lines.
    \ifnum \dtl@omitlines > \z@
      \advance\dtl@omitlines by \m@ne
      \read\@dtl@read to \@dtl@line

% Produce the csv file.
More junk!
  % Load the db.
  % Display it.
  • I really like how easy the option is to use! Thanks!
    – Christoph
    Aug 9, 2011 at 15:06

According to Section 4.2.2 of the pgfplots manual you can add the »skip first« option to a plot command.

\addplot file[skip first] {datafile.dat};
  • That seems simpler indeed :) Aug 8, 2011 at 10:13
  • This is only useful when the file contents are plotted directly, but not if the data is used in a different way or has to be pre-processed.
    – Christoph
    Aug 9, 2011 at 15:03
  • Use /pgfplots/table/skip first n={integer i} for \addplot table. Example: \addplot table[skip first n=1, ...
    – ComFreek
    Sep 4, 2014 at 19:59
  • 3
    This does not work using \pgfplotstableread{data.dat}{\data}; and then \addplot table[skip first n=2, x index=0, y index=1] {\data}. The skip seems to be only accepted if put at the place where the file is actually loaded. Would be nice to be able to skip for individual plot commands when using the same data file! Mar 29, 2016 at 10:20
  • @Christoph90 table[y expr={\coordindex<0 ? nan : \thisrowno{1}}] will work. It seems that doing this just for y is sufficient. I'm using <0 because \coordindex, unlike \lineno, does not count comments and the header.
    – bgfvdu3w
    Feb 2 at 3:37

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.