I was trying to compose an answer to Bold one cell in table using CSV reader using the collcell
package, and can't seem to get it to work either with the csvsimple
tabluar, nor datatool
.
With the cvssimple
, I get:
Runaway argument? ! Paragraph ended before \collect@cell@arg was complete. <to be read again> \par
With the datatool
I get:
! TeX capacity exceeded, sorry [input stack size=5000]. \reserved@a ->\def \reserved@a *{\let \@xs@assign \@xs@expand@and@detokenize... l.56 \end{tabular}
! ==> Fatal error occurred, no output PDF file produced!
Note:
Apologies for the formatting screw up: But before you attempt to edit it to fix it, see Preview not displaying identical to final post
The MWE below code compiles fine and produces the output shown above as the
cvssimple
anddatatool
tables below do not use thecollcell
pacakge. In order to attempt to use thecollcell
package the two tabular column specifications need to be changed froml l
toL L
so that the\newcolumntype
is used.
Code:
\documentclass{article}
\usepackage{csvsimple}
\usepackage{collcell}
\usepackage{datatool}
\usepackage{booktabs}
\usepackage{xstring}
\usepackage{xcolor}
% Make csv in question
\usepackage{filecontents}
\begin{filecontents*}{scientists2.csv}
name,surname,
Albert,Einstein,
Marie,Curie,
Thomas,Edison,
\end{filecontents*}
\newcommand*{\FormatCell}[1]{%
\IfStrEq{#1}{Curie}{\textcolor{red}{#1}}{#1}%
}%
% Need this so that the header rows is not passed to \collectcell
\newcommand*{\Header}[1]{\multicolumn{1}{l}{\textbf{#1}}}
\newcolumntype{L}{>{\collectcell\FormatCell}l<{\endcollectcell}}
\begin{document}
\noindent
Normal \textbf{tabular}:\par
\begin{tabular}{L L}\toprule
\Header{First Name} & \Header{Last Name}\\\cmidrule{1-2}
Albert & Einstein \\
Marie & Curie \\
Thomas & Edison \\
\end{tabular}
\bigskip\noindent
Using \verb|\csvreader|:\par
\csvreader[tabular=l l,
table head=\Header{First Name} & \Header{Last Name} \\\hline]%
{scientists2.csv}{name=\name,surname=\surname}%
{\name & \surname}%
\DTLloaddb[keys={Name,Surname}]{myDB}{scientists2.csv}
\bigskip\noindent
Using \verb|\DTLforeach|:\par
\begin{tabular}{l l}\toprule
\Header{First Name} & \Header{Last Name}\\\cmidrule{1-2}
\DTLforeach*{myDB}{\Name=Name,\Surname=Surname}{%
\Name & \Surname \\
}%
\end{tabular}
\end{document}
\FormatCell{\Surname}
. (You may need to expand\Surname
first. I can't remember if\IfStrEq
performs any expansion.)