10
$\begingroup$

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).

$\endgroup$
4
  • $\begingroup$ As a more general question, what about more general separators like new lines instead of just commas, also? $\endgroup$
    – rcollyer
    Jan 31, 2012 at 16:33
  • $\begingroup$ 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? $\endgroup$
    – user21
    Jan 31, 2012 at 16:53
  • 1
    $\begingroup$ 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. $\endgroup$ Jan 31, 2012 at 17:05
  • $\begingroup$ 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. $\endgroup$
    – user21
    Jan 31, 2012 at 17:16

6 Answers 6

5
$\begingroup$

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
$\endgroup$
12
$\begingroup$

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:

Mathematica graphics

$\endgroup$
2
  • 1
    $\begingroup$ 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. $\endgroup$
    – rm -rf
    Feb 1, 2012 at 1:50
  • 2
    $\begingroup$ I would use CellEvaluationFunction -> (ToExpression[##, Row[{##}, ","]&]&) which I think is a bit cleaner. $\endgroup$
    – Carl Woll
    Jun 4, 2019 at 17:08
11
$\begingroup$

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}

Output

$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}

Output

$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).

$\endgroup$
7
$\begingroup$

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

Mathematica graphics

TraditionalForm:

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

Mathematica graphics

$\endgroup$
4
$\begingroup$

How about:

ExportString[{{a = 1 + 1, b = 1 + 2}}, "CSV"]
$\endgroup$
1
  • $\begingroup$ This is perfect for the supplied example and thank you. However, for more complex situations it seems to turn everything into 'input syntax' losing all of the function formatting. Perhaps I should edit my post to reflect this. $\endgroup$ Jan 31, 2012 at 17:00
4
$\begingroup$

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;
b = 1 + 2;
CellPrint[Print[StringForm["``, ``", a, b]]]

Maybe too specific for your purpose?

$\endgroup$

Your Answer

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

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