5
$\begingroup$

Happy Easter!

How can I change the background color of one specific row in TableForm? Or can I even do it ?

for instance:

TableForm[{{a, b}, {c, d}, {e, f}}, 
 TableHeadings -> {{"r1", "r2", "r3"}, {"c1", "c2"}}]

How can I make the background color of r2 to be orange ?

Thanks!

$\endgroup$
  • $\begingroup$ TableForm[{{a, b}, Item[#, Background -> Red] & /@ {c, d}, {e, f}}, TableHeadings -> {{"r1", "r2", "r3"}, {"c1", "c2"}}]? $\endgroup$ – kglr Apr 1 '18 at 8:20
  • $\begingroup$ @kglr thank you very much. This works. However, I have a very long list and It would be tedious to replace every single second row item with your code. Is there also another way ? $\endgroup$ – james Apr 1 '18 at 8:30
  • $\begingroup$ You can do something like: list = {{a, b}, {c, d}, {e, f}}; list2 = MapAt[Item[#, Background -> Red] &, list, {{2, All}}]; TableForm[list2, TableHeadings -> {{"r1", "r2", "r3"}, {"c1", "c2"}}] or like list3 = list; list3[[2]] = Item[#, Background -> Red] & /@ list3[[2]]; TableForm[list3, TableHeadings -> {{"r1", "r2", "r3"}, {"c1", "c2"}}] $\endgroup$ – kglr Apr 1 '18 at 8:36
  • $\begingroup$ @kglr that works ! Thank you very much. Can you please add it as answer, such that I can accept it. Thanks. $\endgroup$ – james Apr 1 '18 at 10:02
4
$\begingroup$
list = {{a, b}, {c, d}, {e, f}}; 
list2 = MapAt[Item[#, Background -> Red] &, list, {{2, All}}]; 
TableForm[list2, TableHeadings -> {{"r1", "r2", "r3"}, {"c1", "c2"}}] 

or

list3 = list; 
list3[[2]] = Item[#, Background -> Red] & /@ list3[[2]]; 
TableForm[list3, TableHeadings -> {{"r1", "r2", "r3"}, {"c1", "c2"}}]

enter image description here

$\endgroup$
  • $\begingroup$ Oddly, formatting provided by Item is ignored when it is in the TableHeadings values. $\endgroup$ – Alan Apr 1 '18 at 18:36

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.