0

In the following csv file example, I want to automatically replace a string pattern in all headers cells when importing with pgfplotstable:

\begin{filecontents*}{sample.csv}
    parameter,cubic parameter,revised parameter,"$quartic_{parameter}$"
    1 ,13 ,34 ,5
    2 ,3  ,7  ,11
\end{filecontents*}

I want to replace the pattern "parameter" by "param" in all header cells.

I tried this :

\documentclass{article}
\usepackage{pgfplotstable}
\usepackage{filecontents}

\begin{filecontents*}{sample.csv}
    parameter,cubic parameter,revised parameter,"$quartic_{parameter}$"
    1 ,13 ,34 ,5
    2 ,3  ,7  ,11
\end{filecontents*}

\begin{document}
        \pgfplotstabletypeset[col sep=comma,row sep=newline,ignore chars={"},string replace*={parameter}{param}]{sample.csv}%
\end{document}

I obtained this result (no expected changes in the headers cell): enter image description here

I would prefer this result where all the "parameter" words have been replaced by the string "param" even in "math mode" (last column header):

enter image description here

Is there a way to do that automatically in all header cells with pgfplotstable (I would prefer to use pgfplotstable but I am open to other solutions)?

1 Answer 1

1
+50

You can use the option assign column name of pgfplotstable package.
Two alternative solutions follow.

Fist solution
With the macro \mpath taken from this Mico's answer (pay attention, this should be compiled with LuaLaTeX):

\documentclass{article}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.18}

% From https://tex.stackexchange.com/a/604009/101651:
\newcommand\mpath[1]{\directlua{%
    tex.sprint(( string.gsub( "#1" , "parameter" , "param" )) )}}
   
\usepackage{filecontents}
\begin{filecontents*}{sample.csv}
    parameter,cubic parameter,revised parameter,"$quartic_{parameter}$"
    1 ,13 ,34 ,5
    2 ,3  ,7  ,11
\end{filecontents*}

\begin{document}
    \pgfplotstabletypeset[col sep=comma, row sep=newline,  ignore chars={"},
    header=has colnames,
    assign column name/.style={
            /pgfplots/table/column name={\mpath{#1}}},
    ]{sample.csv}
\end{document}

enter image description here

Second solution
Are you sure you like the last column header written in italics?
Isn't it better in the following way?
The following code uses \StrSubstitute from xstring package for string headers. The column header in math mode is modified explicitly with the option columns of pgfplotstable package.
It can be compiled with the ordinary pdfLaTeX.

\documentclass{article}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.18}
\usepackage{xstring}
\usepackage{amsmath}
   
\usepackage{filecontents}

\begin{filecontents*}{sample.csv}
    parameter,cubic parameter,revised parameter,"$quartic_{parameter}$"
    1 ,13 ,34 ,5
    2 ,3  ,7  ,11
\end{filecontents*}

\begin{document}
    \pgfplotstabletypeset[col sep=comma, row sep=newline,  ignore chars={"},
    header=has colnames,
    assign column name/.style={
        /pgfplots/table/column name={\StrSubstitute{#1}{parameter}{param}}},
    columns/$quartic_{parameter}$/.style={column name={quartic$_{\text{param}}$}},
    ]{sample.csv}
\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .