2

I want to programmatically create a "Code" style cell with commands separated by ; and a new line (from some list). Say for example I have a list {set[a,1],set[b,2],...} I would like to create the cell.

a=1;
b=2;
...

This can almost be done by following the answer to a similar question here, and replacing "Input" with "Code". I find that CompoundExpression@@{a,b,...} does show as a;b;..., as I would like, but I don't know how to add the appropriate newlines.

xzczd
  • 65,995
  • 9
  • 163
  • 468
Kvothe
  • 4,419
  • 9
  • 28

1 Answers1

3

How about this?:

format[l_List] := 
  ToBoxes@# /. {"set", "[", RowBox[{a_, ",", b_}], "]"} :> {a, "=", b, ";", "\n"} & /@ 
       l // RowBox // BoxData // Cell[#, "Code"] & // CellPrint;

format@{set[a, 1], set[b, 2], set[c, 2]}

xzczd
  • 65,995
  • 9
  • 163
  • 468