# Producing cleaner Mathematica output

I asked this in chat and it turns out it would make for a good question make a posting for. I am writing up a handout type notebook and I'd like to have some clean output for presentation's sake.

Say I have multiple functions I wish to evaluate in one cell. How can I force the output to a more condensed space/format such as one line?

What Is Happening

Input:

a = 1 + 1
b = 1 + 2


Output:

2

3


What I Have Been Doing

I have been using the List[] command.

List[a = 1 + 1,
b = 1 + 2]


But this gives me some 'extraneous' curly braces.

{2, 3}


What I Want

Output:

2, 3


Other

The fix should keep the formatting of output functions (should not reduce functions to one-line input style syntax).

• As a more general question, what about more general separators like new lines instead of just commas, also? – rcollyer Jan 31 '12 at 16:33
• Why would you want such a non standard format? You can not copy it into any other function, you can not apply to it, you can not do anything with it? – user21 Jan 31 '12 at 16:53
• As I stated, I am writing up a notebook for use as a 'handout'. I am looking for very clean output that saves space (aka paper). I'm not sure what you mean by "you can not do anything with it" regarding the output as I've been able to copy/paste any result I need into a new equation later on if I need to. – LordStryker Jan 31 '12 at 17:05
• The output M- generates is M- input, your output is not, since 2,3 is not valid syntax. You might get it done with a format rule, perhaps. – user21 Jan 31 '12 at 17:16

Try using semi-colons to suppress normal output and then use Print to print what you want exactly:

a = 1+1;
b = 1+2;
Print[a,", ",b];


which gives:

2, 3


Here is an approach using CellEvaluationFunction:

Use the Options Inspector and set CellEvaluationFunction to:

(Map[ToExpression, Row[Apply[Riffle[#, "\", \""]& , #]]]& )


This is best combined with style sheets to define a style that automatically uses this function. (Ask if you need help setting this up.)

To use this more simply, or simply to try it out, evaluate this to create a new Input cell, then type your input into that cell:

CellPrint[
Cell[BoxData[""], "Input",
CellEvaluationFunction ->
(ToExpression /@ Row[Riffle[#, "\", \""] & @@ #] &)]]


The result should look like this:

• Very nice. This was exactly my interpretation of the OP's problem and all the other answers have merely provided half-workarounds for the example in the question. I took the same approach and spent a while fiddling with CellEvaluationFunction and low level cell boxes before I gave up. This is what I had hoped to do, but couldn't. – rm -rf Feb 1 '12 at 1:50
• I would use CellEvaluationFunction -> (ToExpression[##, Row[{##}, ","]&]&) which I think is a bit cleaner. – Carl Woll Jun 4 '19 at 17:08

I highly doubt something that answers your question is useful in any way, since your plan is to strip output of syntactically relevant information, but here we go:

Mathematica provides a few commands that are applied to input/output automatically:

## $Post $Post is automatically applied to all expressions before their output is printed. This alters the data, e.g. setting $Post = N will generate numerical data only. {1 + 1, 1 + 2} (* Print lists in column form *)$Post = If[Head[#] === List, Column[#]] &;

{1 + 1, 1 + 2}
(* You can't add 1 to a column :-( *)
% + 1

(* Reset the variable to default *)
$Post =. {1 + 1, 1 + 2}  ## $PrePrint

$PrePrint is similar to $Post, only that it does not affect output but only the printed version, i.e. the visualization. Setting this to N will preserve the normal Mathematica workflow in the back end, only the display is different.

{1 + 1, 1 + 2}

(* Print lists in column form *)
$PrePrint = If[Head[#] === List, Column[#]] &; {1 + 1, 1 + 2} (* Only printing is affected, you can still do normal arithmetic with the output *) % + 1 (* Reset the variable to default *)$PrePrint =.
{1 + 1, 1 + 2}


## $Pre $Pre is applied after the input has been put in. Make sure make sure to include a Hold somewhere, otherwise it's pretty much the same as \$Post (see remarks in the documentation).

You could use Format

Format[myInput[a___]] := DisplayForm@RowBox[Riffle[{a}, ","]]


Then

myInput[a = 1 + 1, b = 1 + 2]

(*
==> 2, 3
*)


More complicated input:

myInput[D[Cos[Log[x]^2], x], Integrate[Sin[1/x], x], Integrate[Log[Cos[x]], x]]


myInput[D[Cos[Log[x]^2], x], Integrate[Sin[1/x], x], Integrate[Log[Cos[x]], x]]


ExportString[{{a = 1 + 1, b = 1 + 2}}, "CSV"]

I'm not sure if this will help, but you can try StringForm along with CellPrint and Print. I've been messing around with something similar and I've almost got it working. To duplicate the output that you want, you can do this:
a = 1 + 1;
CellPrint[Print[StringForm[", ", a, b]]]