1

Have tried all of the prior responses questions but just can not seem to get any of them to work. I have tried all the various csvreader but can't seem to figure it out.

I am trying to import the following csv file. CSV example below.

Contract,Today,One Week Ago,One Year Ago,1 Week Change,1 Year Change
Petroleum (NYM, WTI Crude Cash, $ per barrel),,,,,
October 19,55.68,55.25,62.61,1%,-11%
March 20,53.89,53.55,61.04,1%,-12%
Natural Gas (NYM, $ per million BTU's),,,,,
October 19,2.177,2.154,2.732,1%,-20%
March 20,2.261,2.262,2.535,0%,-11%
Gasoline (NYMEX, RBOB, $ per gallon),,,,, 
October 19,1.5639,1.5402,1.8366,2%,-15%
March 20,1.6884,1.6657,2.0045,1%,-16%
Heating Oil (NYM, $ per gallon),,,,,
October 19,1.864,1.851,,1%,
March 20,1.835,1.827,,0%,
  • 1
    Hi, welcome. By import, do you mean "make a table of the file"? – Torbjørn T. Aug 23 at 18:48
3

You probably run into problems because of your datafile. First of all, you're using comma as a column separator, but the first column has several entries which contain a comma. This will confuse pgfplotstable for example. One workaround could be to use a different column separator, for example a semi colon. Another is to wrap the cells in question in {}.

Another problem could be the dollar and percent characters. These are special characters in TeX, so need to be handled somehow. There might be some packages that can do this automatically, but in the example below I just escaped the dollar signs by using \$ instead of $ in the file. I could have done the same with the percent signs, but ended up removing them and adding them back in the table settings.

This is very likely not the best method, but I suppose it does work. It's perhaps a bit wide, but depending on the layout of your document that may or may not be a problem.

enter image description here

% the filecontents environment writes its content to the file
% specified, you don't need it for your own code
% just to make the example self contained, while showing the 
% changes to the file
\begin{filecontents*}{datafile.csv}
Contract,Today,One Week Ago,One Year Ago,1 Week Change,1 Year Change
{Petroleum (NYM, WTI Crude Cash, \$ per barrel)},,,,,
October 19,55.68,55.25,62.61,1,-11
March 20,53.89,53.55,61.04,1,-12
{Natural Gas (NYM, \$ per million BTU's)},,,,,
October 19,2.177,2.154,2.732,1,-20
March 20,2.261,2.262,2.535,0,-11
{Gasoline (NYMEX, RBOB, \$ per gallon)},,,,, 
October 19,1.5639,1.5402,1.8366,2,-15
March 20,1.6884,1.6657,2.0045,1,-16
{Heating Oil (NYM, \$ per gallon)},,,,,
October 19,1.864,1.851,,1,
March 20,1.835,1.827,,0,
\end{filecontents*}

\documentclass[border=5mm]{standalone}
\usepackage{pgfplotstable}
\usepackage{booktabs}  % for improved table rules
\usepackage{etoolbox} % for ifstrequal

% read file to table
\pgfplotstableread[col sep=comma]{datafile.csv}\MyTable

\begin{document}

\pgfplotstabletypeset[
  assign column name/.style={
    /pgfplots/table/column name={\textbf{#1}} % bold header row
  },
  every head row/.style={
   % add rules above and below header
   before row=\toprule,
   after row=\midrule,
  },
  % add rule after last row
  every last row/.style={after row=\bottomrule},
  %modify first column
  columns/Contract/.style={
    string type,
    % left aligned
    column type={l},
    assign cell content/.code={
      % kind of a hack to change the "sub headers"
     \pgfmathtruncatemacro\TmpRow{mod(\pgfplotstablerow,3)}%
     \ifnum\TmpRow=0% for every third row
       % typeset the content in an \rlap, which makes a zero size box, with the content sticking out on the right
       \pgfkeyssetvalue{/pgfplots/table/@cell content}{\rlap{\textit{##1}}}% 
     \fi
    }
  },
  % make a style for the last two columns
  percents/.style={
   postproc cell content/.append code={%
     \pgfmathtruncatemacro\TmpRow{mod(\pgfplotstablerow,3)}%
     \ifnum\TmpRow=0
     \else
      % if not empty, add a percent character
      \ifstrequal{##1}{}{}{\pgfkeyssetvalue{/pgfplots/table/@cell content}{$##1\%$}}%
     \fi
  }
  },
  % use style for last two columns
  columns/1 Week Change/.style={percents},
  columns/1 Year Change/.style={percents},
  % add some space before each subheading
  every nth row={3[2]}{after row={\addlinespace}}
]\MyTable

\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.