# Apply conditional styling to the rows of a table

This is a follow-on from a similar question here. If I already have a table of data, say

table1 = {{0, 0, 0, 0, 0, 0},
{0, 1, 2, 3, 4, 5},
{0, 2, 4, 6, 8, 10},
{0, 3, 6, 9, 12, 15},
{0, 4, 8, 12, 16, 20},
{0, 5, 10, 15, 20, 25},
{0, 6, 12, 18, 24, 30},
{0, 7, 14, 21, 28, 35},
{0, 8, 16, 24, 32, 40},
{0, 9, 18, 27, 36, 45},
{0, 10, 20, 30, 40, 50},
{0, 11, 22, 33, 44, 55},
{0, 12, 24, 36, 48, 60},
{0, 13, 26, 39, 52, 65},
{0, 14, 28, 42, 56, 70},
{0, 15, 30, 45, 60, 75}}


and I want to present this in TableForm with the rows whose row numbers are prime (i.e., rows 2,3,5...) in bold, how do I do this by applying functions to table1? Perhaps some combination of If, Length, Range and PrimeQ?

Obviously in the above case the table is small enough to do it manually, but I want to be able to do this with any arbitrary pre-existing table of data.

Does something like this do what you want?

table1 /. (row : {a_?NumberQ, b_?PrimeQ, c___?NumberQ} :>Map[Highlighted@Style[#, Bold] &, row]) // TableForm


Hi Richard. Not sure I fully understand what you're trying to achieve, so the following suggestions are a bit of a guess.

table1[[All, 2]] += 1;
table1 /. (row : {a_?NumberQ, b_?PrimeQ, c___?NumberQ} :>
Map[Highlighted@Style[#, Bold] &, row]) // TableForm


All I've done here is modify your table1 to add a 'shift' value of 1 to all values in the second column. If you want to leave table1 as-is, you could try the following.

table1 /. (row : {a_?NumberQ, b_?NumberQ, c___?NumberQ} :>
Map[Highlighted@Style[#, Bold] &, row] /;
PrimeQ[b + 1]) // TableForm


This code imposes a condition such that the rewrite rule is invoked only if the value of the number in the second column when added to 1 is Prime.

• One additional request - should have made this clearer... What if there's an offset? Like if the first row is actually row 0 instead of row 1? Which bit do I change? – Richard Burke-Ward Mar 28 at 11:39