2
$\begingroup$

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.

$\endgroup$

1 Answer 1

3
$\begingroup$

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]}
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.