Colouring column values based on a specific condition. How could I do this?












1















I am generating a big-table from a csv file using the latex code as given below.



documentclass{article}
usepackage{booktabs}
usepackage{csvsimple}
usepackage{caption}
usepackage{longtable}

begin{document}

csvautobooklongtable[
table head={%
toprule
bfseries Name &bfseries ID \
midrule},
table foot={\bottomrule}]%
{sample-data.csv}

end{document}


The generated document looks similar to:



enter image description here



Now, I want to make:




  • IDs greater than 6 in bold and red. How could I do this?

  • Also, for IDs less than 6, I want to append a symbol <. So 3 becomes 3 <. How could I do this?


Here is link to the CSV file that I am using to create a table.










share|improve this question









New contributor




Amanda is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





















  • Welcome to TeX.SX! Can you please expand the code snippet that you have posted to a full minimal working example. That is, can you post some csv data too.

    – Andrew
    2 hours ago






  • 1





    @Andrew Have updated.

    – Amanda
    1 hour ago
















1















I am generating a big-table from a csv file using the latex code as given below.



documentclass{article}
usepackage{booktabs}
usepackage{csvsimple}
usepackage{caption}
usepackage{longtable}

begin{document}

csvautobooklongtable[
table head={%
toprule
bfseries Name &bfseries ID \
midrule},
table foot={\bottomrule}]%
{sample-data.csv}

end{document}


The generated document looks similar to:



enter image description here



Now, I want to make:




  • IDs greater than 6 in bold and red. How could I do this?

  • Also, for IDs less than 6, I want to append a symbol <. So 3 becomes 3 <. How could I do this?


Here is link to the CSV file that I am using to create a table.










share|improve this question









New contributor




Amanda is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





















  • Welcome to TeX.SX! Can you please expand the code snippet that you have posted to a full minimal working example. That is, can you post some csv data too.

    – Andrew
    2 hours ago






  • 1





    @Andrew Have updated.

    – Amanda
    1 hour ago














1












1








1








I am generating a big-table from a csv file using the latex code as given below.



documentclass{article}
usepackage{booktabs}
usepackage{csvsimple}
usepackage{caption}
usepackage{longtable}

begin{document}

csvautobooklongtable[
table head={%
toprule
bfseries Name &bfseries ID \
midrule},
table foot={\bottomrule}]%
{sample-data.csv}

end{document}


The generated document looks similar to:



enter image description here



Now, I want to make:




  • IDs greater than 6 in bold and red. How could I do this?

  • Also, for IDs less than 6, I want to append a symbol <. So 3 becomes 3 <. How could I do this?


Here is link to the CSV file that I am using to create a table.










share|improve this question









New contributor




Amanda is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












I am generating a big-table from a csv file using the latex code as given below.



documentclass{article}
usepackage{booktabs}
usepackage{csvsimple}
usepackage{caption}
usepackage{longtable}

begin{document}

csvautobooklongtable[
table head={%
toprule
bfseries Name &bfseries ID \
midrule},
table foot={\bottomrule}]%
{sample-data.csv}

end{document}


The generated document looks similar to:



enter image description here



Now, I want to make:




  • IDs greater than 6 in bold and red. How could I do this?

  • Also, for IDs less than 6, I want to append a symbol <. So 3 becomes 3 <. How could I do this?


Here is link to the CSV file that I am using to create a table.







color longtable conditionals






share|improve this question









New contributor




Amanda is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Amanda is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 1 hour ago







Amanda













New contributor




Amanda is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 3 hours ago









AmandaAmanda

204




204




New contributor




Amanda is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Amanda is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Amanda is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.













  • Welcome to TeX.SX! Can you please expand the code snippet that you have posted to a full minimal working example. That is, can you post some csv data too.

    – Andrew
    2 hours ago






  • 1





    @Andrew Have updated.

    – Amanda
    1 hour ago



















  • Welcome to TeX.SX! Can you please expand the code snippet that you have posted to a full minimal working example. That is, can you post some csv data too.

    – Andrew
    2 hours ago






  • 1





    @Andrew Have updated.

    – Amanda
    1 hour ago

















Welcome to TeX.SX! Can you please expand the code snippet that you have posted to a full minimal working example. That is, can you post some csv data too.

– Andrew
2 hours ago





Welcome to TeX.SX! Can you please expand the code snippet that you have posted to a full minimal working example. That is, can you post some csv data too.

– Andrew
2 hours ago




1




1





@Andrew Have updated.

– Amanda
1 hour ago





@Andrew Have updated.

– Amanda
1 hour ago










2 Answers
2






active

oldest

votes


















2














I struggled to get this to work using csvautobooklongtable as it is fairly restrictive but I think (?) that using csvreader[longtable=ll,...
creates a long table. Using this setup with csvsimple you can create the table



enter image description here



using the code:



documentclass{article}
usepackage{filecontents}
begin{filecontents*}{sample-data.csv}
name, id
Daphne Kub, 1
Karolann Lebsack, 2
Charlotte Parisian, 3
Jairo Mayert, 4
Dr. Adrienne Schulist, 5
Elenora Pacocha, 6
Nikita Mraz, 7
Maya Tremblay, 8
Florine Konopelski, 9
Derrick Volkman, 10
end{filecontents*}

usepackage{xcolor}
usepackage{booktabs}
usepackage{csvsimple}
usepackage{caption}
usepackage{longtable}

newcommandColorID[1]{ifnum#1>6color{red}textbf{#1}else#1 $<$fi}

begin{document}

csvreader[head to column names,longtable=ll,
table head={toprulebfseries Name &bfseries ID \ midrule},
table foot={\bottomrule}
]{sample-data.csv}{}
{name &ColorIDid}

end{document}


The main point is that I head used head to column names to name the "variables" in each column and the macro ColorID is used to make the big numbers bold red and to add < to the small numbers.



(I also added the csv file to the MWE using the filecontents package.)



Edit If you wan to keep your data file intact, and not add the name, id header, then you should remove the head to column names and then use, for example, csvcoli and csvcolii in the table:



documentclass{article}
usepackage{filecontents}
begin{filecontents*}{sample-data.csv}
Daphne Kub, 1
Karolann Lebsack, 2
Charlotte Parisian, 3
Jairo Mayert, 4
Dr. Adrienne Schulist, 5
Elenora Pacocha, 6
Nikita Mraz, 7
Maya Tremblay, 8
Florine Konopelski, 9
Derrick Volkman, 10
end{filecontents*}

usepackage{xcolor}
usepackage{booktabs}
usepackage{csvsimple}
usepackage{caption}
usepackage{longtable}

newcommandColorID[1]{ifnum#1>6color{red}textbf{#1}else#1 $<$fi}

begin{document}

csvreader[longtable=ll,
table head={toprulebfseries Name &bfseries ID \ midrule},
table foot={\bottomrule}
]{sample-data.csv}{}
{csvcoli &ColorIDcsvcolii}

end{document}





share|improve this answer


























  • This gives an error saying Undefined control sequence. Snapshot: ibb.co/9yFFxgx

    – Amanda
    43 mins ago











  • @Amanda Are you compiling my MWE or are you using your data? If you are using your data then you should add name, id to the top of your CSV file.

    – Andrew
    32 mins ago











  • Using mine. Is there a way without it?

    – Amanda
    31 mins ago











  • @Amanda Yes, wait a second and I'll edit.

    – Andrew
    30 mins ago











  • Looks fine. Could you point me to the documentation of csvreader? I could not find the documentation under the ctan.org link you shared. I want to experiment by adding hline after each row. Would want to see some examples.

    – Amanda
    21 mins ago



















1














An approach with readarray and listofitems.



Here, I simply use the readdef macro of readarray to input the file contents into a defmytabledata, while placing a \ at the end of each record of input.



Then, I develop the macro mytable to build the tabular using a token list. I perform a two-tier parsing of mytabledata first separating rows by way of the inserted \, and then looking for the comma in each row. After the readlist*IDlabel{} invocation, IDlabel[3,1] for example will contain the name associated with the 3rd file record, whereas IDlabel[4,2] will contain the id-label associated with the fourth file record. The macro IDlabel is fully expandable, requiring two expansions to get to the tokens that were actually in the file data. Thus, I have provided internal support macros like xxaddtotabtoks{} to take its argument, expand it twice, and add it to the token list.



I set up a foreachitem loop to go through each record and add tokens to the token list. I have to create different logic for the first record, since that record is used to make the header. For the other records, I output to the token list the data, while paying attention to the value of the id-label, If it is less than 6, I add a $<$ and if greater than 6, I make it bold red.



Once the loop is done, I merely need to output the token list inside a tabular.



documentclass{article}
usepackage{listofitems,xcolor,filecontents,readarray,booktabs}
begin{filecontents*}{sample-data.csv}
Name, ID
Thing 1, 3
Thing 2, 5
Joe Smith, 6
Jane Doe, 11
end{filecontents*}
newtokstabtoks
newcommandaddtotabtoks[1]{tabtoksexpandafter{thetabtoks#1}}
newcommandxaddtotabtoks[1]{expandafteraddtotabtoksexpandafter{#1}}
newcommandxxaddtotabtoks[1]{expandafterxaddtotabtoksexpandafter{#1}}
newcommandmytable[2][0]{%
setsepchar{\/,}%
ignoreemptyitems%
tabtoks{}%
readlist*IDlabel{#2}%
foreachitemxinIDlabel{%
ifnumxcnt=1relax
addtotabtoks{toprulebfseries}
xxaddtotabtoks{IDlabel[1,1] & bfseries}
xxaddtotabtoks{IDlabel[1,2] \midrule}%
else
xxaddtotabtoks{IDlabel[xcnt,1] & }%
ifnumIDlabel[xcnt,2]>6relaxaddtotabtoks{bfseriescolor{red}}fi%
xxaddtotabtoks{IDlabel[xcnt,2]}%
ifnumIDlabel[xcnt,2]<6relaxaddtotabtoks{$<$}fi%
addtotabtoks{\}%
fi%
}%
centerline{begin{tabular}{ll}thetabtoksend{tabular}}%
}
begin{document}
readarraysepchar{\}
readdef{sample-data.csv}mytabledata
mytable{mytabledata}
end{document}


enter image description here






share|improve this answer


























  • It will be even more helpful if you could you also add some explanations and links that I could refer to.This all looks too complex to me.

    – Amanda
    1 hour ago











  • Is there not a way to do with csvautobooklongtable?

    – Amanda
    1 hour ago











  • @Amanda I have provided explanation. I don't speak the language of csvautobooklongtable, whereas I am the author of readarray and coauthor of listofitems. Thus, I speak these macros more fluently.

    – Steven B. Segletes
    1 hour ago












Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "85"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});






Amanda is a new contributor. Be nice, and check out our Code of Conduct.










draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f488364%2fcolouring-column-values-based-on-a-specific-condition-how-could-i-do-this%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














I struggled to get this to work using csvautobooklongtable as it is fairly restrictive but I think (?) that using csvreader[longtable=ll,...
creates a long table. Using this setup with csvsimple you can create the table



enter image description here



using the code:



documentclass{article}
usepackage{filecontents}
begin{filecontents*}{sample-data.csv}
name, id
Daphne Kub, 1
Karolann Lebsack, 2
Charlotte Parisian, 3
Jairo Mayert, 4
Dr. Adrienne Schulist, 5
Elenora Pacocha, 6
Nikita Mraz, 7
Maya Tremblay, 8
Florine Konopelski, 9
Derrick Volkman, 10
end{filecontents*}

usepackage{xcolor}
usepackage{booktabs}
usepackage{csvsimple}
usepackage{caption}
usepackage{longtable}

newcommandColorID[1]{ifnum#1>6color{red}textbf{#1}else#1 $<$fi}

begin{document}

csvreader[head to column names,longtable=ll,
table head={toprulebfseries Name &bfseries ID \ midrule},
table foot={\bottomrule}
]{sample-data.csv}{}
{name &ColorIDid}

end{document}


The main point is that I head used head to column names to name the "variables" in each column and the macro ColorID is used to make the big numbers bold red and to add < to the small numbers.



(I also added the csv file to the MWE using the filecontents package.)



Edit If you wan to keep your data file intact, and not add the name, id header, then you should remove the head to column names and then use, for example, csvcoli and csvcolii in the table:



documentclass{article}
usepackage{filecontents}
begin{filecontents*}{sample-data.csv}
Daphne Kub, 1
Karolann Lebsack, 2
Charlotte Parisian, 3
Jairo Mayert, 4
Dr. Adrienne Schulist, 5
Elenora Pacocha, 6
Nikita Mraz, 7
Maya Tremblay, 8
Florine Konopelski, 9
Derrick Volkman, 10
end{filecontents*}

usepackage{xcolor}
usepackage{booktabs}
usepackage{csvsimple}
usepackage{caption}
usepackage{longtable}

newcommandColorID[1]{ifnum#1>6color{red}textbf{#1}else#1 $<$fi}

begin{document}

csvreader[longtable=ll,
table head={toprulebfseries Name &bfseries ID \ midrule},
table foot={\bottomrule}
]{sample-data.csv}{}
{csvcoli &ColorIDcsvcolii}

end{document}





share|improve this answer


























  • This gives an error saying Undefined control sequence. Snapshot: ibb.co/9yFFxgx

    – Amanda
    43 mins ago











  • @Amanda Are you compiling my MWE or are you using your data? If you are using your data then you should add name, id to the top of your CSV file.

    – Andrew
    32 mins ago











  • Using mine. Is there a way without it?

    – Amanda
    31 mins ago











  • @Amanda Yes, wait a second and I'll edit.

    – Andrew
    30 mins ago











  • Looks fine. Could you point me to the documentation of csvreader? I could not find the documentation under the ctan.org link you shared. I want to experiment by adding hline after each row. Would want to see some examples.

    – Amanda
    21 mins ago
















2














I struggled to get this to work using csvautobooklongtable as it is fairly restrictive but I think (?) that using csvreader[longtable=ll,...
creates a long table. Using this setup with csvsimple you can create the table



enter image description here



using the code:



documentclass{article}
usepackage{filecontents}
begin{filecontents*}{sample-data.csv}
name, id
Daphne Kub, 1
Karolann Lebsack, 2
Charlotte Parisian, 3
Jairo Mayert, 4
Dr. Adrienne Schulist, 5
Elenora Pacocha, 6
Nikita Mraz, 7
Maya Tremblay, 8
Florine Konopelski, 9
Derrick Volkman, 10
end{filecontents*}

usepackage{xcolor}
usepackage{booktabs}
usepackage{csvsimple}
usepackage{caption}
usepackage{longtable}

newcommandColorID[1]{ifnum#1>6color{red}textbf{#1}else#1 $<$fi}

begin{document}

csvreader[head to column names,longtable=ll,
table head={toprulebfseries Name &bfseries ID \ midrule},
table foot={\bottomrule}
]{sample-data.csv}{}
{name &ColorIDid}

end{document}


The main point is that I head used head to column names to name the "variables" in each column and the macro ColorID is used to make the big numbers bold red and to add < to the small numbers.



(I also added the csv file to the MWE using the filecontents package.)



Edit If you wan to keep your data file intact, and not add the name, id header, then you should remove the head to column names and then use, for example, csvcoli and csvcolii in the table:



documentclass{article}
usepackage{filecontents}
begin{filecontents*}{sample-data.csv}
Daphne Kub, 1
Karolann Lebsack, 2
Charlotte Parisian, 3
Jairo Mayert, 4
Dr. Adrienne Schulist, 5
Elenora Pacocha, 6
Nikita Mraz, 7
Maya Tremblay, 8
Florine Konopelski, 9
Derrick Volkman, 10
end{filecontents*}

usepackage{xcolor}
usepackage{booktabs}
usepackage{csvsimple}
usepackage{caption}
usepackage{longtable}

newcommandColorID[1]{ifnum#1>6color{red}textbf{#1}else#1 $<$fi}

begin{document}

csvreader[longtable=ll,
table head={toprulebfseries Name &bfseries ID \ midrule},
table foot={\bottomrule}
]{sample-data.csv}{}
{csvcoli &ColorIDcsvcolii}

end{document}





share|improve this answer


























  • This gives an error saying Undefined control sequence. Snapshot: ibb.co/9yFFxgx

    – Amanda
    43 mins ago











  • @Amanda Are you compiling my MWE or are you using your data? If you are using your data then you should add name, id to the top of your CSV file.

    – Andrew
    32 mins ago











  • Using mine. Is there a way without it?

    – Amanda
    31 mins ago











  • @Amanda Yes, wait a second and I'll edit.

    – Andrew
    30 mins ago











  • Looks fine. Could you point me to the documentation of csvreader? I could not find the documentation under the ctan.org link you shared. I want to experiment by adding hline after each row. Would want to see some examples.

    – Amanda
    21 mins ago














2












2








2







I struggled to get this to work using csvautobooklongtable as it is fairly restrictive but I think (?) that using csvreader[longtable=ll,...
creates a long table. Using this setup with csvsimple you can create the table



enter image description here



using the code:



documentclass{article}
usepackage{filecontents}
begin{filecontents*}{sample-data.csv}
name, id
Daphne Kub, 1
Karolann Lebsack, 2
Charlotte Parisian, 3
Jairo Mayert, 4
Dr. Adrienne Schulist, 5
Elenora Pacocha, 6
Nikita Mraz, 7
Maya Tremblay, 8
Florine Konopelski, 9
Derrick Volkman, 10
end{filecontents*}

usepackage{xcolor}
usepackage{booktabs}
usepackage{csvsimple}
usepackage{caption}
usepackage{longtable}

newcommandColorID[1]{ifnum#1>6color{red}textbf{#1}else#1 $<$fi}

begin{document}

csvreader[head to column names,longtable=ll,
table head={toprulebfseries Name &bfseries ID \ midrule},
table foot={\bottomrule}
]{sample-data.csv}{}
{name &ColorIDid}

end{document}


The main point is that I head used head to column names to name the "variables" in each column and the macro ColorID is used to make the big numbers bold red and to add < to the small numbers.



(I also added the csv file to the MWE using the filecontents package.)



Edit If you wan to keep your data file intact, and not add the name, id header, then you should remove the head to column names and then use, for example, csvcoli and csvcolii in the table:



documentclass{article}
usepackage{filecontents}
begin{filecontents*}{sample-data.csv}
Daphne Kub, 1
Karolann Lebsack, 2
Charlotte Parisian, 3
Jairo Mayert, 4
Dr. Adrienne Schulist, 5
Elenora Pacocha, 6
Nikita Mraz, 7
Maya Tremblay, 8
Florine Konopelski, 9
Derrick Volkman, 10
end{filecontents*}

usepackage{xcolor}
usepackage{booktabs}
usepackage{csvsimple}
usepackage{caption}
usepackage{longtable}

newcommandColorID[1]{ifnum#1>6color{red}textbf{#1}else#1 $<$fi}

begin{document}

csvreader[longtable=ll,
table head={toprulebfseries Name &bfseries ID \ midrule},
table foot={\bottomrule}
]{sample-data.csv}{}
{csvcoli &ColorIDcsvcolii}

end{document}





share|improve this answer















I struggled to get this to work using csvautobooklongtable as it is fairly restrictive but I think (?) that using csvreader[longtable=ll,...
creates a long table. Using this setup with csvsimple you can create the table



enter image description here



using the code:



documentclass{article}
usepackage{filecontents}
begin{filecontents*}{sample-data.csv}
name, id
Daphne Kub, 1
Karolann Lebsack, 2
Charlotte Parisian, 3
Jairo Mayert, 4
Dr. Adrienne Schulist, 5
Elenora Pacocha, 6
Nikita Mraz, 7
Maya Tremblay, 8
Florine Konopelski, 9
Derrick Volkman, 10
end{filecontents*}

usepackage{xcolor}
usepackage{booktabs}
usepackage{csvsimple}
usepackage{caption}
usepackage{longtable}

newcommandColorID[1]{ifnum#1>6color{red}textbf{#1}else#1 $<$fi}

begin{document}

csvreader[head to column names,longtable=ll,
table head={toprulebfseries Name &bfseries ID \ midrule},
table foot={\bottomrule}
]{sample-data.csv}{}
{name &ColorIDid}

end{document}


The main point is that I head used head to column names to name the "variables" in each column and the macro ColorID is used to make the big numbers bold red and to add < to the small numbers.



(I also added the csv file to the MWE using the filecontents package.)



Edit If you wan to keep your data file intact, and not add the name, id header, then you should remove the head to column names and then use, for example, csvcoli and csvcolii in the table:



documentclass{article}
usepackage{filecontents}
begin{filecontents*}{sample-data.csv}
Daphne Kub, 1
Karolann Lebsack, 2
Charlotte Parisian, 3
Jairo Mayert, 4
Dr. Adrienne Schulist, 5
Elenora Pacocha, 6
Nikita Mraz, 7
Maya Tremblay, 8
Florine Konopelski, 9
Derrick Volkman, 10
end{filecontents*}

usepackage{xcolor}
usepackage{booktabs}
usepackage{csvsimple}
usepackage{caption}
usepackage{longtable}

newcommandColorID[1]{ifnum#1>6color{red}textbf{#1}else#1 $<$fi}

begin{document}

csvreader[longtable=ll,
table head={toprulebfseries Name &bfseries ID \ midrule},
table foot={\bottomrule}
]{sample-data.csv}{}
{csvcoli &ColorIDcsvcolii}

end{document}






share|improve this answer














share|improve this answer



share|improve this answer








edited 5 mins ago

























answered 55 mins ago









AndrewAndrew

31.7k34583




31.7k34583













  • This gives an error saying Undefined control sequence. Snapshot: ibb.co/9yFFxgx

    – Amanda
    43 mins ago











  • @Amanda Are you compiling my MWE or are you using your data? If you are using your data then you should add name, id to the top of your CSV file.

    – Andrew
    32 mins ago











  • Using mine. Is there a way without it?

    – Amanda
    31 mins ago











  • @Amanda Yes, wait a second and I'll edit.

    – Andrew
    30 mins ago











  • Looks fine. Could you point me to the documentation of csvreader? I could not find the documentation under the ctan.org link you shared. I want to experiment by adding hline after each row. Would want to see some examples.

    – Amanda
    21 mins ago



















  • This gives an error saying Undefined control sequence. Snapshot: ibb.co/9yFFxgx

    – Amanda
    43 mins ago











  • @Amanda Are you compiling my MWE or are you using your data? If you are using your data then you should add name, id to the top of your CSV file.

    – Andrew
    32 mins ago











  • Using mine. Is there a way without it?

    – Amanda
    31 mins ago











  • @Amanda Yes, wait a second and I'll edit.

    – Andrew
    30 mins ago











  • Looks fine. Could you point me to the documentation of csvreader? I could not find the documentation under the ctan.org link you shared. I want to experiment by adding hline after each row. Would want to see some examples.

    – Amanda
    21 mins ago

















This gives an error saying Undefined control sequence. Snapshot: ibb.co/9yFFxgx

– Amanda
43 mins ago





This gives an error saying Undefined control sequence. Snapshot: ibb.co/9yFFxgx

– Amanda
43 mins ago













@Amanda Are you compiling my MWE or are you using your data? If you are using your data then you should add name, id to the top of your CSV file.

– Andrew
32 mins ago





@Amanda Are you compiling my MWE or are you using your data? If you are using your data then you should add name, id to the top of your CSV file.

– Andrew
32 mins ago













Using mine. Is there a way without it?

– Amanda
31 mins ago





Using mine. Is there a way without it?

– Amanda
31 mins ago













@Amanda Yes, wait a second and I'll edit.

– Andrew
30 mins ago





@Amanda Yes, wait a second and I'll edit.

– Andrew
30 mins ago













Looks fine. Could you point me to the documentation of csvreader? I could not find the documentation under the ctan.org link you shared. I want to experiment by adding hline after each row. Would want to see some examples.

– Amanda
21 mins ago





Looks fine. Could you point me to the documentation of csvreader? I could not find the documentation under the ctan.org link you shared. I want to experiment by adding hline after each row. Would want to see some examples.

– Amanda
21 mins ago











1














An approach with readarray and listofitems.



Here, I simply use the readdef macro of readarray to input the file contents into a defmytabledata, while placing a \ at the end of each record of input.



Then, I develop the macro mytable to build the tabular using a token list. I perform a two-tier parsing of mytabledata first separating rows by way of the inserted \, and then looking for the comma in each row. After the readlist*IDlabel{} invocation, IDlabel[3,1] for example will contain the name associated with the 3rd file record, whereas IDlabel[4,2] will contain the id-label associated with the fourth file record. The macro IDlabel is fully expandable, requiring two expansions to get to the tokens that were actually in the file data. Thus, I have provided internal support macros like xxaddtotabtoks{} to take its argument, expand it twice, and add it to the token list.



I set up a foreachitem loop to go through each record and add tokens to the token list. I have to create different logic for the first record, since that record is used to make the header. For the other records, I output to the token list the data, while paying attention to the value of the id-label, If it is less than 6, I add a $<$ and if greater than 6, I make it bold red.



Once the loop is done, I merely need to output the token list inside a tabular.



documentclass{article}
usepackage{listofitems,xcolor,filecontents,readarray,booktabs}
begin{filecontents*}{sample-data.csv}
Name, ID
Thing 1, 3
Thing 2, 5
Joe Smith, 6
Jane Doe, 11
end{filecontents*}
newtokstabtoks
newcommandaddtotabtoks[1]{tabtoksexpandafter{thetabtoks#1}}
newcommandxaddtotabtoks[1]{expandafteraddtotabtoksexpandafter{#1}}
newcommandxxaddtotabtoks[1]{expandafterxaddtotabtoksexpandafter{#1}}
newcommandmytable[2][0]{%
setsepchar{\/,}%
ignoreemptyitems%
tabtoks{}%
readlist*IDlabel{#2}%
foreachitemxinIDlabel{%
ifnumxcnt=1relax
addtotabtoks{toprulebfseries}
xxaddtotabtoks{IDlabel[1,1] & bfseries}
xxaddtotabtoks{IDlabel[1,2] \midrule}%
else
xxaddtotabtoks{IDlabel[xcnt,1] & }%
ifnumIDlabel[xcnt,2]>6relaxaddtotabtoks{bfseriescolor{red}}fi%
xxaddtotabtoks{IDlabel[xcnt,2]}%
ifnumIDlabel[xcnt,2]<6relaxaddtotabtoks{$<$}fi%
addtotabtoks{\}%
fi%
}%
centerline{begin{tabular}{ll}thetabtoksend{tabular}}%
}
begin{document}
readarraysepchar{\}
readdef{sample-data.csv}mytabledata
mytable{mytabledata}
end{document}


enter image description here






share|improve this answer


























  • It will be even more helpful if you could you also add some explanations and links that I could refer to.This all looks too complex to me.

    – Amanda
    1 hour ago











  • Is there not a way to do with csvautobooklongtable?

    – Amanda
    1 hour ago











  • @Amanda I have provided explanation. I don't speak the language of csvautobooklongtable, whereas I am the author of readarray and coauthor of listofitems. Thus, I speak these macros more fluently.

    – Steven B. Segletes
    1 hour ago
















1














An approach with readarray and listofitems.



Here, I simply use the readdef macro of readarray to input the file contents into a defmytabledata, while placing a \ at the end of each record of input.



Then, I develop the macro mytable to build the tabular using a token list. I perform a two-tier parsing of mytabledata first separating rows by way of the inserted \, and then looking for the comma in each row. After the readlist*IDlabel{} invocation, IDlabel[3,1] for example will contain the name associated with the 3rd file record, whereas IDlabel[4,2] will contain the id-label associated with the fourth file record. The macro IDlabel is fully expandable, requiring two expansions to get to the tokens that were actually in the file data. Thus, I have provided internal support macros like xxaddtotabtoks{} to take its argument, expand it twice, and add it to the token list.



I set up a foreachitem loop to go through each record and add tokens to the token list. I have to create different logic for the first record, since that record is used to make the header. For the other records, I output to the token list the data, while paying attention to the value of the id-label, If it is less than 6, I add a $<$ and if greater than 6, I make it bold red.



Once the loop is done, I merely need to output the token list inside a tabular.



documentclass{article}
usepackage{listofitems,xcolor,filecontents,readarray,booktabs}
begin{filecontents*}{sample-data.csv}
Name, ID
Thing 1, 3
Thing 2, 5
Joe Smith, 6
Jane Doe, 11
end{filecontents*}
newtokstabtoks
newcommandaddtotabtoks[1]{tabtoksexpandafter{thetabtoks#1}}
newcommandxaddtotabtoks[1]{expandafteraddtotabtoksexpandafter{#1}}
newcommandxxaddtotabtoks[1]{expandafterxaddtotabtoksexpandafter{#1}}
newcommandmytable[2][0]{%
setsepchar{\/,}%
ignoreemptyitems%
tabtoks{}%
readlist*IDlabel{#2}%
foreachitemxinIDlabel{%
ifnumxcnt=1relax
addtotabtoks{toprulebfseries}
xxaddtotabtoks{IDlabel[1,1] & bfseries}
xxaddtotabtoks{IDlabel[1,2] \midrule}%
else
xxaddtotabtoks{IDlabel[xcnt,1] & }%
ifnumIDlabel[xcnt,2]>6relaxaddtotabtoks{bfseriescolor{red}}fi%
xxaddtotabtoks{IDlabel[xcnt,2]}%
ifnumIDlabel[xcnt,2]<6relaxaddtotabtoks{$<$}fi%
addtotabtoks{\}%
fi%
}%
centerline{begin{tabular}{ll}thetabtoksend{tabular}}%
}
begin{document}
readarraysepchar{\}
readdef{sample-data.csv}mytabledata
mytable{mytabledata}
end{document}


enter image description here






share|improve this answer


























  • It will be even more helpful if you could you also add some explanations and links that I could refer to.This all looks too complex to me.

    – Amanda
    1 hour ago











  • Is there not a way to do with csvautobooklongtable?

    – Amanda
    1 hour ago











  • @Amanda I have provided explanation. I don't speak the language of csvautobooklongtable, whereas I am the author of readarray and coauthor of listofitems. Thus, I speak these macros more fluently.

    – Steven B. Segletes
    1 hour ago














1












1








1







An approach with readarray and listofitems.



Here, I simply use the readdef macro of readarray to input the file contents into a defmytabledata, while placing a \ at the end of each record of input.



Then, I develop the macro mytable to build the tabular using a token list. I perform a two-tier parsing of mytabledata first separating rows by way of the inserted \, and then looking for the comma in each row. After the readlist*IDlabel{} invocation, IDlabel[3,1] for example will contain the name associated with the 3rd file record, whereas IDlabel[4,2] will contain the id-label associated with the fourth file record. The macro IDlabel is fully expandable, requiring two expansions to get to the tokens that were actually in the file data. Thus, I have provided internal support macros like xxaddtotabtoks{} to take its argument, expand it twice, and add it to the token list.



I set up a foreachitem loop to go through each record and add tokens to the token list. I have to create different logic for the first record, since that record is used to make the header. For the other records, I output to the token list the data, while paying attention to the value of the id-label, If it is less than 6, I add a $<$ and if greater than 6, I make it bold red.



Once the loop is done, I merely need to output the token list inside a tabular.



documentclass{article}
usepackage{listofitems,xcolor,filecontents,readarray,booktabs}
begin{filecontents*}{sample-data.csv}
Name, ID
Thing 1, 3
Thing 2, 5
Joe Smith, 6
Jane Doe, 11
end{filecontents*}
newtokstabtoks
newcommandaddtotabtoks[1]{tabtoksexpandafter{thetabtoks#1}}
newcommandxaddtotabtoks[1]{expandafteraddtotabtoksexpandafter{#1}}
newcommandxxaddtotabtoks[1]{expandafterxaddtotabtoksexpandafter{#1}}
newcommandmytable[2][0]{%
setsepchar{\/,}%
ignoreemptyitems%
tabtoks{}%
readlist*IDlabel{#2}%
foreachitemxinIDlabel{%
ifnumxcnt=1relax
addtotabtoks{toprulebfseries}
xxaddtotabtoks{IDlabel[1,1] & bfseries}
xxaddtotabtoks{IDlabel[1,2] \midrule}%
else
xxaddtotabtoks{IDlabel[xcnt,1] & }%
ifnumIDlabel[xcnt,2]>6relaxaddtotabtoks{bfseriescolor{red}}fi%
xxaddtotabtoks{IDlabel[xcnt,2]}%
ifnumIDlabel[xcnt,2]<6relaxaddtotabtoks{$<$}fi%
addtotabtoks{\}%
fi%
}%
centerline{begin{tabular}{ll}thetabtoksend{tabular}}%
}
begin{document}
readarraysepchar{\}
readdef{sample-data.csv}mytabledata
mytable{mytabledata}
end{document}


enter image description here






share|improve this answer















An approach with readarray and listofitems.



Here, I simply use the readdef macro of readarray to input the file contents into a defmytabledata, while placing a \ at the end of each record of input.



Then, I develop the macro mytable to build the tabular using a token list. I perform a two-tier parsing of mytabledata first separating rows by way of the inserted \, and then looking for the comma in each row. After the readlist*IDlabel{} invocation, IDlabel[3,1] for example will contain the name associated with the 3rd file record, whereas IDlabel[4,2] will contain the id-label associated with the fourth file record. The macro IDlabel is fully expandable, requiring two expansions to get to the tokens that were actually in the file data. Thus, I have provided internal support macros like xxaddtotabtoks{} to take its argument, expand it twice, and add it to the token list.



I set up a foreachitem loop to go through each record and add tokens to the token list. I have to create different logic for the first record, since that record is used to make the header. For the other records, I output to the token list the data, while paying attention to the value of the id-label, If it is less than 6, I add a $<$ and if greater than 6, I make it bold red.



Once the loop is done, I merely need to output the token list inside a tabular.



documentclass{article}
usepackage{listofitems,xcolor,filecontents,readarray,booktabs}
begin{filecontents*}{sample-data.csv}
Name, ID
Thing 1, 3
Thing 2, 5
Joe Smith, 6
Jane Doe, 11
end{filecontents*}
newtokstabtoks
newcommandaddtotabtoks[1]{tabtoksexpandafter{thetabtoks#1}}
newcommandxaddtotabtoks[1]{expandafteraddtotabtoksexpandafter{#1}}
newcommandxxaddtotabtoks[1]{expandafterxaddtotabtoksexpandafter{#1}}
newcommandmytable[2][0]{%
setsepchar{\/,}%
ignoreemptyitems%
tabtoks{}%
readlist*IDlabel{#2}%
foreachitemxinIDlabel{%
ifnumxcnt=1relax
addtotabtoks{toprulebfseries}
xxaddtotabtoks{IDlabel[1,1] & bfseries}
xxaddtotabtoks{IDlabel[1,2] \midrule}%
else
xxaddtotabtoks{IDlabel[xcnt,1] & }%
ifnumIDlabel[xcnt,2]>6relaxaddtotabtoks{bfseriescolor{red}}fi%
xxaddtotabtoks{IDlabel[xcnt,2]}%
ifnumIDlabel[xcnt,2]<6relaxaddtotabtoks{$<$}fi%
addtotabtoks{\}%
fi%
}%
centerline{begin{tabular}{ll}thetabtoksend{tabular}}%
}
begin{document}
readarraysepchar{\}
readdef{sample-data.csv}mytabledata
mytable{mytabledata}
end{document}


enter image description here







share|improve this answer














share|improve this answer



share|improve this answer








edited 1 hour ago

























answered 1 hour ago









Steven B. SegletesSteven B. Segletes

164k9209421




164k9209421













  • It will be even more helpful if you could you also add some explanations and links that I could refer to.This all looks too complex to me.

    – Amanda
    1 hour ago











  • Is there not a way to do with csvautobooklongtable?

    – Amanda
    1 hour ago











  • @Amanda I have provided explanation. I don't speak the language of csvautobooklongtable, whereas I am the author of readarray and coauthor of listofitems. Thus, I speak these macros more fluently.

    – Steven B. Segletes
    1 hour ago



















  • It will be even more helpful if you could you also add some explanations and links that I could refer to.This all looks too complex to me.

    – Amanda
    1 hour ago











  • Is there not a way to do with csvautobooklongtable?

    – Amanda
    1 hour ago











  • @Amanda I have provided explanation. I don't speak the language of csvautobooklongtable, whereas I am the author of readarray and coauthor of listofitems. Thus, I speak these macros more fluently.

    – Steven B. Segletes
    1 hour ago

















It will be even more helpful if you could you also add some explanations and links that I could refer to.This all looks too complex to me.

– Amanda
1 hour ago





It will be even more helpful if you could you also add some explanations and links that I could refer to.This all looks too complex to me.

– Amanda
1 hour ago













Is there not a way to do with csvautobooklongtable?

– Amanda
1 hour ago





Is there not a way to do with csvautobooklongtable?

– Amanda
1 hour ago













@Amanda I have provided explanation. I don't speak the language of csvautobooklongtable, whereas I am the author of readarray and coauthor of listofitems. Thus, I speak these macros more fluently.

– Steven B. Segletes
1 hour ago





@Amanda I have provided explanation. I don't speak the language of csvautobooklongtable, whereas I am the author of readarray and coauthor of listofitems. Thus, I speak these macros more fluently.

– Steven B. Segletes
1 hour ago










Amanda is a new contributor. Be nice, and check out our Code of Conduct.










draft saved

draft discarded


















Amanda is a new contributor. Be nice, and check out our Code of Conduct.













Amanda is a new contributor. Be nice, and check out our Code of Conduct.












Amanda is a new contributor. Be nice, and check out our Code of Conduct.
















Thanks for contributing an answer to TeX - LaTeX Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f488364%2fcolouring-column-values-based-on-a-specific-condition-how-could-i-do-this%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

What other Star Trek series did the main TNG cast show up in?

Berlina muro

Berlina aerponto