0
$\begingroup$

In the following table

table = Table[x*n, {x, 0, 20}, 
   {n, 0, 5}]; 
TableForm[table, TableHeadings -> 
 {{"x=0", "1", "2", "3", "4", 
     "5", "6", "7", "8", "9", "10", 
     "11", "12", "13", "14", "15", 
     "16", "17", "18", "19", "20"}, 
   {"n=0", "1", "2", "3", "4", 
     "5"}}]

I want to create two versions:

  1. With the headings in bold for each row where x is prime,
  2. With the whole row in bold (including its heading) for each row where x is prime.

How do I accomplish these?

I want a computed result rather than simply formatting rows or headers manually, since I may want to change the size of the table later. I'm after a global solution...

$\endgroup$
  • $\begingroup$ As a starting point: Table[Style[x*n, If[PrimeQ[x], Bold, ## &[]]], {x, 0, 20}, {n, 0, 5}] $\endgroup$ – J. M. is away Mar 27 at 16:48
2
$\begingroup$

First version:

xlabels = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20"};
table = Table[x*n, {x, 0, 20}, {n, 0, 5}];
TableForm[table, 
 TableHeadings -> {Style[#, {Bold,Plain}[[PrimeQ[ToExpression[#]] /. {True -> 1, False -> 2}]]] & /@ xlabels, {"n=0", "1", "2", "3", "4", "5"}}]

enter image description here

Second version:

table2 = Table[Style[x*n, {Bold, Plain}[[PrimeQ[ToExpression[x]] /. {True -> 1, False -> 2}]]], {x, 0, 20}, {n, 0, 5}];
TableForm[table2, 
 TableHeadings -> {Style[#, {Bold, Plain}[[PrimeQ[ToExpression[#]] /. {True -> 1, False -> 2}]]] & /@ xlabels, {"n=0", "1", "2", "3", "4", "5"}}]

enter image description here

$\endgroup$

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.