1

When I try to color a row inside xstring it created unwanted blank row, I need longtable for large data, so how do I proceed to filter out?

Here is my MWE:

 \documentclass[a4paper,12pt]{article}
 \usepackage{longtable,filecontents,datatool,xstring}
 \usepackage[table]{xcolor}

 \begin{filecontents*}{data.csv}
 Sl.No., Name, Class, Gender
 1, Mr. David , X, M
 2, Miss Kache , XII,F
 3, Mr. Stephan, XII,M
 4, Mr. Prakash, XII,M
 5, Mr. Digvijay, XII,M
 \end{filecontents*}
 \DTLloaddb{mydata}{data.csv}

 \begin{document}

 \begin{longtable}{|l|l|l|l|}
 \hline
 Sl.No. & Name & Class & Gender\\ \hline
 \DTLforeach*{mydata}{\sl=Sl.No.,\name=Name,\c=Class,\g=Gender}{

 \IfStrEq{\g}{M}{\\\rowcolor{red!20} \sl & \name & \c & \g \\ \hline}{}

 }


 \end{longtable}

 \end{document}

enter image description here

  • \rowcolor like \multicolumn has to be the first thing (after expansion) in the row, you can not use IfStrEq{ – David Carlisle May 16 '17 at 20:47
  • Is there any alternative to \rowcolor so that i can use IfStrEq and color a row @DavidCarlisle – Biki Teron May 16 '17 at 20:49
2

You seem to have misplaced the \hlines and the \\s. Here is a cleaned up version

 \documentclass[a4paper,12pt]{article}
 \usepackage{longtable,filecontents,datatool,xstring}
 \usepackage[table]{xcolor}

 \begin{filecontents*}{data.csv}
 Sl.No., Name, Class, Gender
 1, Mr. David , X, M
 2, Miss Kache , XII,F
 3, Mr. Stephan, XII,M
 4, Mr. Prakash, XII,M
 5, Mr. Digvijay, XII,M
 \end{filecontents*}
 \DTLloaddb{mydata}{data.csv}

 \begin{document}

 \begin{longtable}{|l|l|l|l|}
 \hline
 Sl.No. & Name & Class & Gender  \DTLforeach*{mydata}{\sl=Sl.No.,\name=Name,\c=Class,\g=Gender}{%
 \IfStrEq{\g}{M}{\\\hline\rowcolor{red!20} \sl & \name & \c & \g }{}
 }\\\hline


 \end{longtable}

 \end{document}

enter image description here

On the other hand I suggest a removal of the vertical separator, and the 1st, 3rd and 4th columns centered. In this example I also included the other line, which is filtered out in your example (just for completeness sake, since how not to render it is shown above)

 \documentclass[a4paper,12pt]{article}
 \usepackage{longtable,filecontents,datatool,xstring}
 \usepackage[table]{xcolor}

 \begin{filecontents*}{data.csv}
 Sl.No., Name, Class, Gender
 1, Mr. David , X, M
 2, Miss Kache , XII,F
 3, Mr. Stephan, XII,M
 4, Mr. Prakash, XII,M
 5, Mr. Digvijay, XII,M
 \end{filecontents*}
 \DTLloaddb{mydata}{data.csv}

 \begin{document}

 \begin{longtable}{clcc}
 \hline
 Sl.No. & Name & Class & Gender  \DTLforeach*{mydata}{\sl=Sl.No.,\name=Name,\c=Class,\g=Gender}{%
 \IfStrEq{\g}{M}{\\\hline\rowcolor{red!20} \sl & \name & \c & \g }{\\\hline\rowcolor{blue!20} \sl & \name & \c & \g}
 }\\\hline


 \end{longtable}

 \end{document}

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.