5

Does anyone know why this isn't working? I'm trying to add horizontal lines to my graphs, and it's breaking at the \midrule command

\documentclass{article}
\usepackage{etoolbox}
\usepackage{pgfplotstable}
\usepackage{pgfplots}
\usepackage{booktabs}

\begin{filecontents*}{test.csv}
    ColA, ColB, ColC
    0.17, 0.91, 0.67
    0.15, 0.17, 0.92
    0.48, 0.1, 0.28
\end{filecontents*}

\def\buildTable#1{%
    \pgfplotstableread[col sep = comma]{#1}\rawdata%
    \xdef\theTable{\noexpand\pgfplotstabletypeset[every row/.style={before row=\midrule}]}%
    \theTable{\rawdata}%
}%

\begin{document}
\begin{table}
    \buildTable
    {%CSV file
        test.csv%
    }
\end{table}
\end{document}
  • 2
    need tabular environment? – AML Jun 8 '18 at 19:12
  • @AML \pgfplotstabletypeset makes one. – Torbjørn T. Jun 8 '18 at 19:48
6

If you really want \midrules before every row use simply the before row key

\documentclass{article}
\usepackage{etoolbox}
\usepackage{pgfplotstable}
\usepackage{pgfplots}
\usepackage{booktabs}

\begin{filecontents*}{test.csv}
    ColA, ColB, ColC
    0.17, 0.91, 0.67
    0.15, 0.17, 0.92
    0.48, 0.1, 0.28
\end{filecontents*}

\def\buildTable#1{%
    \pgfplotstableread[col sep = comma]{#1}\rawdata%
    \pgfplotstabletypeset[before row= \midrule]\rawdata
}%

\begin{document}
\begin{table}
    \buildTable
    {%CSV file
        test.csv%
    }
\end{table}
\end{document}

enter image description here

5

I do not know, why the code makes a detour via \xdef and \TheTable. But, I would use \noexpand before \midrule inside the \xdef.

Also, I have not found the style every row in the documentation, but every head row exists:

\documentclass{article}
\usepackage{pgfplotstable}
\usepackage{booktabs}
\pgfplotsset{compat=newest}

\begin{filecontents*}{test.csv}
    ColA, ColB, ColC
    0.17, 0.91, 0.67
    0.15, 0.17, 0.92
    0.48, 0.1, 0.28
\end{filecontents*}

\newcommand{\buildTable}[1]{%
    \pgfplotstableread[col sep = comma]{#1}\rawdata
    \xdef\theTable{%
        \noexpand\pgfplotstabletypeset[
            every head row/.style={after row=\noexpand\midrule}%
        ]}%
    \theTable{\rawdata}%
}

\begin{document}
\begin{table}
    \buildTable
    {%CSV file
        test.csv%
    }
\end{table}
\end{document}

Result

With alignment at the decimal separator:

\documentclass{article}
\usepackage{array}
\usepackage{booktabs}
\usepackage{pgfplotstable}
\pgfplotsset{compat=newest}

\begin{filecontents*}{test.csv}
    ColA, ColB, ColC
    0.17, 0.91, 0.67
    0.15, 0.17, 0.92
    0.48, 0.1, 0.28
\end{filecontents*}

\newcommand{\buildTable}[1]{%
    \pgfplotstableread[col sep = comma]{#1}\rawdata
    \xdef\theTable{%
        \noexpand\pgfplotstabletypeset[
            dec sep align={c},
            every head row/.style={after row=\noexpand\midrule}%
        ]}%
    \theTable{\rawdata}%
}%

\begin{document}
\begin{table}
    \buildTable
    {%CSV file
        test.csv%
    }
\end{table}
\end{document}

Result

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.