5

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
        \csvreader[no head]{data-mwe.csv}{}{%
            \ifthenelse{\equal{\csvcolii}{}}{
                \\ \hline \multicolumn{2}{|c|}{\csvcoli}
            }{%
                \\ \hline \csvcoli & \csvcolii
            }
        }
        \\ \hline
    \end{tabular}

\end{document}

enter image description here

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

enter image description here

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

enter image description here

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.

3
+100

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{array}
\usepackage{csvsimple}
\usepackage{nicematrix}
\usepackage{tikz}

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

\begin{document}

    \begin{NiceTabular}{ll}%
      [ 
        hvlines,
        code-after = { \tikz \draw [very thick] (row-2-|col-1) -- (row-2-|col-3) ; }
      ]
        column 1 & column 2 
        \csvreader[no head]{data-mwe.csv}{}{%
            \ifthenelse{\equal{\csvcolii}{}}{
                \\ \multicolumn{2}{c}{\csvcoli}
            }{%
                \\ \csvcoli & \csvcolii
            }
        }
        \\ 
    \end{NiceTabular}

\end{document}

You need several compilations.

Output of the above code

1

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
        \csvreader[no head]{data-mwe.csv}{}{%
            \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}

enter image description here

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.