2
$\begingroup$

I have a simple table:

TableForm[Table[{test}, {m, 1, 5}, {n, 1, 5}], 
   TableHeadings -> {{"m=1\nFactorInteger[m1]", 
  "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?

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

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

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

enter image description here

instead of something like:

StringForm["m=1\n`1`", FactorInteger[20]]

enter image description here

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]"

So, my suggestion is to use expressions as your table headings.

$\endgroup$
2
$\begingroup$

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}], 
 TableHeadings -> 
   {StringTemplate["<* If[#==1,\"m =\", \"\"]*> ``\n <* FactorInteger[#] *>"] /@
     Range[5], 
   StringTemplate["<* If[#==1,\"n =\", \"\"]*> ``\n <* FactorInteger[#] *>"] /@
     Range[5]}]

enter image description here

Alternatively, you can use StringRiffle:

TableForm[Table[{test}, {m, 1, 5}, {n, 1, 5}], 
 TableHeadings -> 
  {StringRiffle[{#, FactorInteger@#}, {If[# == 1, "m = ", ""], "\n", ""}]&/@Range[5], 
  StringRiffle[{#, FactorInteger@#}, { If[# == 1, "n = ", ""], "\n", ""}]&/@Range[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.