# Adding a toprule below the header of a table read from csv where some rows are multicolumn

I have created a table using data from a csv, such that multicolumn rows are automatically detected in the csv. In order to do this, I placed all the \\ before each line rather than after, otherwise a multicolumn in an ifthenelse did not work (see csvsimple misplaced \omit. \multispan when using multicolumn for entire row)

However, I do not know now how to place a thick line between the column names and the rest of the table.

This is what I have:

\documentclass[11pt]{article}
\usepackage{csvsimple}

\begin{filecontents}[overwrite]{data-mwe.csv}
a,b
c,d
text,
e,f
\end{filecontents}

\begin{document}

\begin{tabular}{|l|l|}
\hline
column 1 & column 2
\ifthenelse{\equal{\csvcolii}{}}{
\\ \hline \multicolumn{2}{|c|}{\csvcoli}
}{%
\\ \hline \csvcoli & \csvcolii
}
}
\\ \hline
\end{tabular}

\end{document}


In the spirit of Tabularx, csvsimple and multicol on first row lead to \omit error, this is almost what I want (thick line emulated by double hline), except it has of course way to much space after the header line:

Adding \\ \hline \hline \multicolumn{1}{c}{} after the header row results in

I wanted to use \csviffirstrow from the csvsimple docs (page 12), but when I replace {\\ \hline \csvcoli & \csvcolii} with {\\ \csviffirstrow{\hline\hline}{\hline} \csvcoli & \csvcolii} this gives me

Unfortunately it is not the case that the first data row is always the same, otherwise I could of course simply have hardcoded that row and put the \toprule above it.

Answers suggesting completely different solutions (perhaps LuaTeX?) are of course also valid, if they are for LaTeX and can determine multicolumn/no multicolumn from the csv (so not hardcoded in the LaTeX file). csv format is not fixed, I can change that.

The environment {NiceTabular} of nicematrix gives you tools to draw rules with Tikz after the construction of the tabular. So, it's possible to draw the heavy rule without changing the code concerning \csvreader (and even simplifying that code).

\documentclass[11pt]{article}
\usepackage{csvsimple}
\usepackage{nicematrix,tikz}

\begin{filecontents}[overwrite]{data-mwe.csv}
a,b
c,d
text,
e,f
\end{filecontents}

\begin{document}

\begin{NiceTabular}{ll}[hvlines]
column 1 & column 2
\ifthenelse{\equal{\csvcolii}{}}{
\\ \multicolumn{2}{c}{\csvcoli}
}{%
\\ \csvcoli & \csvcolii
}
}
\CodeAfter \tikz \draw [very thick] (2-|1) -- (2-|3) ;
\end{NiceTabular}

\end{document}


You need several compilations because nicematrix uses PGF/Tikz nodes.

The \csviffirstrow seems like a way to go. Instead of only putting the \csviffirstrow around the \hline, put it around the entire row (but keep it inside your outer \ifthenelse:

\csviffirstrow{%
\\ \hline \hline \multicolumn{2}{|c|}{\csvcoli}
}{%
\\ \hline \multicolumn{2}{|c|}{\csvcoli}
}


To place a thick line in the first row, but also keep the height of the cells, use a \specialrule from the booktabs package:

\specialrule{\heavyrulewidth}{0pt}{0pt}


where \heavyrulewidth is the (default) width of a \toprule.

Together, this gives

\begin{tabular}{|l|l|}
\hline
column 1 & column 2
\ifthenelse{\equal{\csvcolii}{}}{
\csviffirstrow{%
\\ \specialrule{\heavyrulewidth}{0pt}{0pt} \multicolumn{2}{|c|}{\csvcoli}
}{%
\\ \hline \multicolumn{2}{|c|}{\csvcoli}
}
}{%
\csviffirstrow{%
\\ \specialrule{\heavyrulewidth}{0pt}{0pt} \csvcoli & \csvcolii
}{%
\\ \hline \csvcoli & \csvcolii
}
}
}
\\ \hline
\end{tabular}