I found this post, illustrating how to typeset the header of a table in bold face. Combining it with \csvreader from the csvsimple package worked fine, as long as I don't format the numbers using siunitx package. Below \sisetup fixes two decimal points as in this answer.
\documentclass{article}
\usepackage{csvsimple, siunitx, booktabs, array}
\begin{filecontents}{sample.csv}
Player, Merlin, Mordred
Andrew, 0.6, 0.55
Ben, 0.54, 0.62
\end{filecontents}
\newcolumntype{+}{>{\global\let\currentrowstyle\relax}}
\newcolumntype{-}{>{\currentrowstyle}}
\newcommand{\rowstyle}[1]{\gdef\currentrowstyle{#1}#1\ignorespaces}
\csvstyle{boldhead}{
no head,
table head=\toprule\rowstyle{\bfseries},
late after first line=\ \midrule,
table foot=\bottomrule
}
\sisetup{round-mode=places, round-precision=2, round-integer-to-decimal}
\begin{document}
\csvreader[tabular=+l-c-c, boldhead]{sample.csv}{}{\csvcoli & \csvcolii & \csvcoliii}
\end{document}
I think there are three problems with this solution:
- The option
head to column namescannot be used because theno headoption is loaded. Is there a better way to display the header using\csvreaderthat preserves the possibility of usinghead to column names? - If I replace
tabular=+l-c-cwithtabular=+l-S-S, then the table no longer typesets because thesiunitxpackage does not recognize the header and throws the errorinvalid numerical input 'e'. - Personally, I'd prefer typing
lccinstead of+l-c-c.
Since the number formatting of the siunitx package can be escaped with a group, my idea for point 2 was to include a \bgroup \egroup pair into the rowstyle as follows:
\newcolumntype{+}{>{\global\let\currentrowstyle\relax\global\let\currentrowstylepost\relax}}
\newcolumntype{-}{>{\currentrowstyle}}
\newcolumntype{^}{<{\currentrowstylepost}}
\newcommand{\rowstyle}[1]{%
\gdef\currentrowstylepost{\egroup}%
\gdef\currentrowstyle{#1\bgroup}%
#1\bgroup\ignorespaces%
}
While the above code works for option tabular=+l^-c^-c^, it still does not work for tabular=+l^-S^-S^. What am I missing?
Regarding point 3, one would imagine that this can be fixed with the tokcycle package. This was my attempt.
\settcEscapechar{:}
\newif\iffirstchar
\Characterdirective{%
\ifx#1|%
#1%
\else%
\iffirstchar%
\global\firstcharfalse%
+#1%
\else%
-#1%
\fi%
\fi%
}
\newcommand{\csvboldreader}[4]{%
\global\firstchartrue%
\csvreader[tabular=\tokencyclexpress #1\endtokencyclexpress, boldhead]{#2}{#3}{#4}
}
\begin{document}
\csvboldreader{lcc}{sample.csv}{}{\csvcoli & \csvcolii & \csvcoliii}
\end{document}
Unfortunately, it throws an error illegal token 'c' used. If I call
\tokencyclexpress lcc\endtokencyclexpress
in isolation, the string is transformed correctly. What am I missing?
I have to admit I am not too familiar with the siunitx package and figured I'd tag on two more questions about it:
- What is the cleanest
\sisetupto align numbers centered with the header, but among each other aligned according to the decimal point? Is there a better way than setting{S[table-format=1.2]}etc. manually for each column? - What is the
@{}used for that appears in thesiunitxmanual? My first guess was that one doesn't have to use braces for each column, but that appears to be incorrect sincetabular=@{} l S S[table-format=2.2] @{}doesn't work.


\tokcyclexpresscannot be called within an argument because it is a fragile command (as, presumably, most other commands in the package?). Second,\the\cytoksneeds to be expanded before it is processed by\csvreader. Because\expandafterlooks ahead only one token, we need to store the rest in\tmp. Correct?I need some time to process the alternative answer as I was not aware of the
– S. Olafsson May 29 '21 at 07:47readarraypackage until today. Thanks for pointing me towards it!\tokcyclexpressis fragile, but because it is unexpandable (because its execution involves assignments, as in\def,\let, and\futurelet). The rest of your comment is on point. The\tmpstorage was particularly helpful here, otherwise, to expandafter an input like[tabular=would require\expandafter [\expandafter t\expandafter a\expandafter b\expandafter u\expandafter l\expandafter a\expandafter r\expandafter =. You can see the problem here. – Steven B. Segletes May 29 '21 at 12:08readarraypackage is due for an upgrade. See my SUPPLEMENT at https://tex.stackexchange.com/questions/498008/set-value-in-readarray/498009#498009 to see the sort of upgrade it will be. – Steven B. Segletes May 29 '21 at 12:14