Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Is it possible to bold a single desired cell (or number of cells) using csvreader (besides the head)? Here is an example of making the table.

\documentclass{article}
\usepackage{csvsimple}

% Make csv in question
\begin{filecontents*}{scientists2.csv}
name,surname,
Albert,Einstein,
Marie,Curie,
Thomas,Edison
\end{filecontents*}

\begin{document}

\begin{center}
\csvreader[tabular=|l|l|,
    table head=\hline \textbf{ First Name} & \textbf{Last Nmae} \\\hline,
    late after line = \\\hline]%
{scientists2.csv}{name=\name,surname=\surname}%
{\name & \surname}%

\end{center}


\end{document} 

I tried to use a filter to apply different rules, but was unsuccessful. Thanks,

share|improve this question
add comment

3 Answers 3

up vote 5 down vote accepted

Not a direct answer but I would recommend the pgfplotstable for such tasks. It's quite comprehensive and file operations are quite convenient. It might seem like an overkill for this specific question but as the tasks get more complicated it reveals its true potential.

\documentclass{article}
\usepackage{pgfplotstable,filecontents,booktabs}

% Make csv in question
\begin{filecontents*}{scientists2.csv}
name,surname
Albert,Einstein
Marie,Curie
Thomas,Edison
\end{filecontents*}

\pgfplotstableread[col sep=comma]{scientists2.csv}\mytable

\begin{document}

\begin{center}
\pgfplotstabletypeset[
string type,
columns/name/.style={column name=Name},
columns/surname/.style={column name=Surname},
% Row/column numbering starts from zero
every row 1 column 1/.style={postproc cell content/.style={@cell content=\textbf{##1}}},
every head row/.style={before row={\toprule},after row=\midrule},
every last row/.style={after row={\toprule}},
]\mytable
\end{center}

\end{document} 

enter image description here

share|improve this answer
    
Thanks so much for including such a detailed response -- really helps when just starting out! –  mike Nov 11 '12 at 1:31
    
@mike My pleasure. –  percusse Nov 11 '12 at 1:34
    
Could you include how to align the contents of the cell in this framework (ie, conolum type = {|l}). Thanks, –  mike Nov 11 '12 at 1:46
    
@mike Initially it's c for all columns but you can change it via column type=|l| etc. for the corresponding column. Have a look at the manual, bottom of page 10. –  percusse Nov 11 '12 at 1:52
    
I'm unsuccessful when I put it inside the description: every head row/.style={column type={|l}, before row={\toprule},after row=\midrule}, –  mike Nov 11 '12 at 1:54
show 2 more comments

You can also so this with the datatool package:

enter image description here

Notes:

Code:

\documentclass{article}
\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}%
}%

\begin{document}
\DTLloaddb[keys={Name,Surname}]{myDB}{scientists2.csv}
\bigskip
\begin{tabular}{l l}\toprule
    \textbf{First Name} & \textbf{Last Name}\\\cmidrule{1-2} 
    \DTLforeach*{myDB}{\Name=Name,\Surname=Surname}{%
        \Name & \FormatCell{\Surname} \\
    }%
\end{tabular}
\end{document}
share|improve this answer
add comment

csvsimple version 1.06 released 2012-11-08 does exactly what you want. The code below is your original MWE, the only modification being \textbf{Curie} instead of Curie within the filecontents environment.

enter image description here

 \documentclass{article}
 \usepackage{csvsimple} % requires csvsimple version 1.06 released 2012-11-08

 % Make csv in question
 \begin{filecontents*}{scientists2.csv}
 name,surname
 Albert,Einstein
 Marie,\textbf{Curie}
 Thomas,Edison
 \end{filecontents*}

 \begin{document}

 \begin{center}
 \csvreader[tabular=|l|l|,
     table head=\hline \textbf{ First Name} & \textbf{Last Nmae} \\\hline,
     late after line = \\\hline]%
 {scientists2.csv}{name=\name,surname=\surname}%
 {\name & \surname}%

 \end{center}


 \end{document} 

It is also worth checking out the new "Macro code inside the data" section in the documentation (p.21). The code below has been taken directly from this section.

enter image description here

 \documentclass{scrreprt}
 \usepackage{tikz,csvsimple,filecontents}
 \begin{document} 

  %-- file embedded for simplicity --
 \begin{filecontents*}{macrodata.csv}
 type,description,content
 M,A nice \textbf{formula},         $\displaystyle \int\frac{1}{x} = \ln|x|+c$
 G,A \textcolor{red}{colored} ball, {\tikz \shadedraw [shading=ball] (0,0) circle (.5cm);}
 M,\textbf{Another} formula,        $\displaystyle \lim\limits_{n\to\infty} \frac{1}{n}=0$
 \end{filecontents*}
 %-- end embedded file --

 \csvautotabular{macrodata.csv}


 \csvstyle{my enumerate}{head to column names}


 \begin{enumerate}
 \csvreader[my enumerate]{macrodata.csv}{}{\item \description:\par\content}
 \end{enumerate}


 Now, formulas only:

 \begin{enumerate} 
 \csvreader[my enumerate,filter equal={\type}{M}]{macrodata.csv}{}{%
   \item \description:\qquad\content}
 \end{enumerate}



 \end{document}

"Mega cool", as my kids would say :-).

share|improve this answer
    
Welcome to TeX.SE. You can upload images, using the icon of a picture, and then just remove the !1 and a user with privileges can put that back in for you. –  Peter Grill Nov 11 '12 at 8:31
    
Also, it appears that this will bold all data in the second column? I think the OP wanted to only highlight a single cell, not the entire column. –  Peter Grill Nov 11 '12 at 8:35
    
I have updated my packages and updated your post with the image as generated by v1.06 of cvssimple. Also, I am unable to get your second example to compile: I get Runaway argument?. –  Peter Grill Nov 11 '12 at 8:42
    
Thou art absolutely correct in stating my suggestion boldifies the complete column. If you only want ONE cell entry to be bold, you just boldify that one entry within the filecontents environment while dropping \textbf{...} within the csvreader command. –  Harry Nov 11 '12 at 8:53
    
It turns out that filecontents has an issue if there is a leading space before the \begin,\end. Added image for you of second example. But even with the second case you have the formatting as part of the data. Usually I try to separate content from formatting. –  Peter Grill Nov 11 '12 at 8:53
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.