1

In Latex, I am using pgfplotstable and longtable to tabulate values from a csv file containing comma-separated values. As seen below, each row contains 8 values separated by commas in the csv file, which I convert to a table with the following code:

\documentclass[a4paper, 12pt, oneside]{book} \usepackage{pgfplotstable} \usepackage{longtable} \begin{document} % For skipping headers \pgfkeysifdefined{/pgfplots/table/output empty row/.@cmd}{ \pgfplotstableset{ empty header/.style={ every head row/.style={output empty row}, } } } \pgfplotstabletypeset[ empty header, begin table={ \centering \begin{longtable}{l l l l l l | l l} \caption{data_table_name} \\ \toprule Col_1 & Col_2 & Col_3 & Col_4 & Col_5 & Col_6 & Col_7 & Col_8 \\ \midrule \endfirsthead % for first table \toprule Col_1 & Col_2 & Col_3 & Col_4 & Col_5 & Col_6 & Col_7 & Col_8 \\ \midrule \endhead % for later tables \midrule \multicolumn{8}{r}{\textit{Continued on next page}} \\ \bottomrule \endfoot % for first table \bottomrule \endlastfoot, % for later tables }, end table=\end{longtable}, col sep=comma, string type, header=true, ]{my_data.csv} \end{document} 

I am able to create my table in latex without any problems, but for some reason I end up with ,cccccccc in the first cell (together with the first value of the first row) before all the other values follow correspondingly. This only happens when I combine I also use longtable; pgfplotstable alone gives me the formatted table I need, but my data spans several pages. How do I get rid of this ,cccccccc and why is this produced to begin with? Is it something to do with the formatting "{l l l l l l | l l}" ?

Sample of outputted table, containing the error in the first cell:

strange cccc entering the table

6
  • 1
    Welcome to TeX.SE. Please tell us which document class you employ and which packages need to be loaded in order to make your code fragment compilable. Better yet, please edit your posting and add whatever commands are needed, beginning with \documentclass and ending with \end{document}, to make your code fragment compilable. Commented May 21 at 15:39
  • 1
    unrelated but don't use \centering with longtable (it won't centre it) Commented May 21 at 15:39
  • My document is very long so I've added the parts I believe are relevant to make the compilation work. The image attached is the outcome I wish to remove, that is, the strange "cccc" that appears in the first cell. Commented May 23 at 16:39
  • @mariestr We can't test your document as we don't have access to your csv file Commented May 23 at 16:42
  • Note that there exists the csvsimple package that can assist you in creating tables from CSV filesy Commented May 23 at 16:52

1 Answer 1

0

You put the column specification and the header/footer definition within begin table option, there is also a comma not needed after \endlastfoot.

Moreover, you're using vertical rules with booktabs: they are incompatible.
I used \specialrule from ctable package.

Also, use \textunderscore instead of _, which is a special character for LaTeX.

The corrected MWE follows. You do not need to add \begin{filecontents}...\end{filecontents} because you already have the file (I do not copy all the numbers from your image because is boring):

% The following environment is here only to create the file, you do not need it, since you already have the file \begin{filecontents}{my_data.csv} kernel_method,dynamic_system,input_signal,approximating_function,kernel,sigma,data_misfit,mean_rel_error KKR,TC,Step,Leg,Gaus,s01,22.32864,0.265484 KKR,TC,Step,Leg,Lap,s01,13.76,0.16 KKR,TC,Step,Leg,Bil,s01,22.495,0.281 KKR,MSD,Step,Leg,Gaus,s01,21.162,0.838 KKR,MSD,Step,Leg,Lap,s01,12.290,0.503 \end{filecontents} \documentclass[a4paper, 12pt, oneside]{book} \usepackage{pgfplotstable} \pgfplotsset{compat=1.18} \usepackage{longtable} \usepackage{ctable} \usepackage{array} \renewcommand{\arraystretch}{1.3} \begin{document} % For skipping headers \pgfkeysifdefined{/pgfplots/table/output empty row/.@cmd}{ \pgfplotstableset{ empty header/.style={ every head row/.style={output empty row}, } } } \pgfplotstabletypeset[ empty header, begin table=\begin{longtable},% \centering useless for a longtable column type=l, columns/sigma/.style={string type,column type={l|}}, every first row/.append style={before row={ \caption{data\textunderscore table\textunderscore name} \\ \specialrule{.1em}{.05em}{.05em} Method & Dynamic & Input & Col\textunderscore4 & Col\textunderscore5 & Col\textunderscore6 & Col\textunderscore7 & Col\textunderscore8 \\ \hline \endfirsthead % for first table % \specialrule{.1em}{.05em}{.05em} Method & Dynamic & Input & Col\textunderscore4 & Col\textunderscore5 & Col\textunderscore6 & Col\textunderscore7 & Col\textunderscore8 \\ \hline \endhead % for later tables \hline \multicolumn{8}{r}{\textit{Continued on next page}} \\ \specialrule{.1em}{.05em}{.05em} \endfoot % for first table \specialrule{.1em}{.05em}{.05em} \endlastfoot % leave out the comma here }}, end table=\end{longtable}, col sep=comma, string type, header=true, ]{my_data.csv} \end{document} 

enter image description here

See also this elegant solution by David Carlisle as a possible alternative.

4
  • Thank you so much @CarLaTeX !!!!! This solved my problem perfectly!! Commented May 25 at 20:12
  • @mariestr You're welcome! If my answer solved your problem, you could accept it tex.meta.stackexchange.com/questions/1852/…. Thank you! Commented May 25 at 21:44
  • Thanks also for this guide! Commented May 26 at 9:06
  • @mariestr You're welcome and thank you for accepting my answer! Commented May 26 at 10:24

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.