# Mix text and calculated values in table header

I have a simple table:

TableForm[Table[{test}, {m, 1, 5}, {n, 1, 5}],
"2\nFactorInteger[m2]", "3\nFactorInteger[m3]",
"4\nFactorInteger[m4]",
"5\nFactorInteger[m5]"}, {"n=1\nFactorInteger[n1]",
"2\nFactorInteger[n2]", "3\nFactorInteger[n3]",
"4\nFactorInteger[n4]",
"5\nFactorInteger[n5]"}}]


However, I want calculated values for each instance of FactorInteger[#]... How do I do this? Evaluation->Evaluate in place doesn't work, it just creates a blank. And I can't find a way to force StringForm to do it.

Suggestions?

• Why not use Column[{"m=1", FactorInteger[m1]}] instead? – Carl Woll Mar 8 at 17:29
• Hi both. @Carl, the Column trick does what I need. Could you convert it to an answer and I'll tick it? – Richard Burke-Ward Mar 8 at 17:32
• It also looks like you might want to learn about StringTemplate, in case you haven't already! – Carl Lange Mar 8 at 17:37

I don't like using strings when you can use expressions. That is, I like using something like:

Column[{HoldForm[m=1], FactorInteger[20]}]


StringForm["m=1\n1", FactorInteger[20]]


The primary reasons I like expressions better are because including typesetting in a string will not work when the string is exported, and it can lead to hard to understand issues, like italicization and font issues.

Also, I don't like using StringTemplate. An example of why I don't like StringTemplate:

StringTemplate["foo s"][<|"s" -> x^2|>]


"foo Power[x, 2]"

I think you will find StringTemplate very convenient for this task (especially, the ability to make calculations inside <* ...*> delimiters):

TableForm[Table[{test}, {m, 1, 5}, {n, 1, 5}],
{StringTemplate["<* If[#==1,\"m =\", \"\"]*> \n <* FactorInteger[#] *>"] /@
StringTemplate["<* If[#==1,\"n =\", \"\"]*> \n <* FactorInteger[#] *>"] /@

Alternatively, you can use StringRiffle:
TableForm[Table[{test}, {m, 1, 5}, {n, 1, 5}],