1

I created a dynamic table in order to cast inline CSV data easily to a table and want to use it now as a custom command, in order to make the table command easy to use for my colleagues.

This first table will generate the table I'd like to have:

\documentclass{article} \usepackage[T1]{fontenc} \usepackage[utf8]{inputenc} \usepackage[table]{xcolor} \usepackage{pgfplotstable}\pgfplotsset{compat=newest} \usepackage{float} \begin{document} \begin{table}[H] \begin{center} \pgfplotstabletypeset[ col sep = comma, string type, string replace*={_}{\_}, every head row/.style={before row=\rowcolor{blue}\hline,after row=\hline}, every last row/.style={after row=\hline}, display columns/0/.style={string type,column type = {|l}}, every last column/.style={string type,column type = {l|}}, every head row/.append style={ before row={\rowcolor{blue}}, typeset cell/.code={ \ifnum\pgfplotstablecol=\pgfplotstablecols \pgfkeyssetvalue{/pgfplots/table/@cell content}{\textcolor{white}{##1}\\}% \else \pgfkeyssetvalue{/pgfplots/table/@cell content}{\textcolor{white}{##1}&}% \fi } } ]{ a,b,c,d aa,bb,cc,dd } \end{center} \caption{yay} \label{yayy} \end{table} \end{document} 

This does work and I have no issues. But it's huge and I'd like to wrap it in the following command:

\documentclass{article} \usepackage[T1]{fontenc} \usepackage[utf8]{inputenc} \usepackage[table]{xcolor} \usepackage{pgfplotstable}\pgfplotsset{compat=newest} \usepackage{float} \newcommand{\csvtable}[2]{% \begin{table}[H] \begin{center} \pgfplotstabletypeset[ col sep = comma, string type, string replace*={_}{\_}, every head row/.style={before row=\rowcolor{blue}\hline,after row=\hline}, every last row/.style={after row=\hline}, display columns/0/.style={string type,column type = {|l}}, every last column/.style={string type,column type = {l|}}, every head row/.append style={ before row={\rowcolor{blue}}, typeset cell/.code={ \ifnum\pgfplotstablecol=\pgfplotstablecols \pgfkeyssetvalue{/pgfplots/table/@cell content}{\textcolor{white}{##1}\\}% \else \pgfkeyssetvalue{/pgfplots/table/@cell content}{\textcolor{white}{##1}&}% \fi } } ]{% #2 }% \end{center} \caption{#1} \label{#1} \end{table} } \begin{document} \csvtable{my caption}{ this, is, csv, data a, b, c, d } \end{document} 

Now I get this error message, which does not help me, because I tried all those separator commands:

results.tex, line 69 Package pgfplots Error: Could not read table file '" this, is, csv, data a, b, c, d "' in 'search path=.'. In case you intended to provide inline data: maybe TeX screwed up your end-of-lines? Try `row sep=crcr' and terminate your lines with `\\' (refer to the pgfplotstable manual for details). 

Long story short. How may I embed the csv data correctly? I know there is also a method with \begin{custom} \end{custom} but I never worked with it and newcommand worked fine for me up to now.

Help appreciated!

Edit: Updated the sample with Rmanos suggestion.

1 Answer 1

0

Well --- the error tells you to use row sep=crcr and \\ as separator. When passing arguments, new lines and spaces are the same for LaTeX, so you "lose" the separation of lines. It is similar to the nesting of verbatim environment. There could be smarter tricks, but this works:

\documentclass{article} \usepackage[T1]{fontenc} \usepackage[utf8]{inputenc} \usepackage[table]{xcolor} \usepackage{pgfplotstable}\pgfplotsset{compat=newest} \usepackage{float} \newcommand{\csvtable}[2]{% <- you add an extra space without this \begin{table}[H] \begin{center} \pgfplotstabletypeset[ col sep = comma, string type, row sep=crcr, string replace*={_}{\_}, every head row/.style={before row=\rowcolor{blue}\hline,after row=\hline}, every last row/.style={after row=\hline}, display columns/0/.style={string type,column type = {|l}}, every last column/.style={string type,column type = {l|}}, every head row/.append style={ before row={\rowcolor{blue}}, typeset cell/.code={ \ifnum\pgfplotstablecol=\pgfplotstablecols \pgfkeyssetvalue{/pgfplots/table/@cell content}{\textcolor{white}{##1}\\}% \else \pgfkeyssetvalue{/pgfplots/table/@cell content}{\textcolor{white}{##1}&}% \fi } } ]{% #2 }% \end{center} \caption{#1} \label{#1} \end{table}% } \begin{document} \csvtable{my caption}{ this, is, csv, data \\ a, b, c, d \\ } \end{document} 

enter image description here

Please, next time, post a complete example so that people do not have to chase packages and options when trying to help...

2
  • Hi Rmano, thanks I also tried this variant, but it loses the first line of my table as you can see in your screenshot. Is there a way of passing an argument with newlines? I found \obeylines but it did not work for me. Sorry for the minimum working example, next time I'll do that including packages. Commented Oct 15, 2021 at 7:46
  • I think this is caused by ##1 being interpreted by the command instead of pgfplots. Is there a way to escape this? Commented Oct 15, 2021 at 9:49

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.