Would like input and output printed on same line, w/o needing extra syntax
You can fix up Partial Solution 1 with your additional requirements with
$PrePrint = (TraditionalForm@HoldForm[In[line] = #] /. line -> $Line /. DownValues[In] /.
{HoldPattern[a_ = a_] :> a,
HoldPattern[a_ = HoldForm[a_]] :> a,
HoldPattern[(c : (a_ = b_)) = b_] :> c,
HoldPattern[(a_ = b_) = c_] :> HoldForm[a = b = c]
}
) &;
$Post = (Replace[#, Null -> HoldForm[In[line]] /. line -> $Line /. DownValues[In]]) &;
The first three additional replacement rules in $PrePrint
address redundancy in the output, the fourth replacement rule removes the unwanted parentheses, and the $Post
command makes it so there is always output, even if there normally would be none.
Now you have
Integrate[x^2, x]
$\int x^2 \, dx=\frac{x^3}{3}$
int = Integrate[x^2, x]
$\text{int}=\int x^2 \, dx=\frac{x^3}{3}$
int/x
$\frac{\text{int}}{x}=\frac{x^2}{3}$
%/x
$\frac{\%}{x}=\frac{x}{3}$
a = 2
$a=2$
g[x_] := Sin[x]
$g(x\_):=\sin(x)$
g[Pi]
$g(\pi)=0$
Note that (a = b) = c
has a different meaning than a = b = c
-- the latter first assigns c
to b
, and then assigns b
to a
, while the former first assigns b
to a
, and then c
to a
. That's why the parentheses are put there in the first place. But that should only be an issue for you if you try to copy and paste and then re-evaluate the input.
Some explanations of the code:
Mathematica stores all of the previously entered input in the function In
, as values for In[1]
, In[2]
, etc. You can get a list of all of these values as a list of replacement rules with Downvalues[In]
. Try evaluating Downvalues[In]
by itself (after doing some other calculations) to see. So Downvalues[In]
already has the correct syntax to be used in a replacement operation.
$PrePrint
and $Post
are similar -- they both apply a function to every output expression. The main difference between them is that $Post
is applied before the value of the output is assigned to the Out
variable, and $Preprint
is applied after. You can see this in action with (in a freshly started kernel):
$PrePrint = f
f[f]
a
f[a]
$Post = g
f[g[g]]
a
f[g[a]]
$PrePrint =.
g[Null]
$Post =.
DownValues[Out]
{HoldPattern[%1] :> f, HoldPattern[%2] :> a, HoldPattern[%3] :> g[g], HoldPattern[%4] :> g[a], HoldPattern[%5] :> g[Null], HoldPattern[%6] :> Null}
Pondering on that sequence of input and output should help you understand $PrePrint
and $Post
. Note that $Post
is applied before $PrePrint
, and the effects of $Post
show up in the downvalues for Out
, while the effects of $PrePrint
do not.
This solution does most of what your are looking for, producing stylized output much like what @Simon Rochester's answer shows. Additionally, this writes to a new notebook window:
$Pre=.;
If[Notebooks["Output"]=={}, nb= CreateDocument[ Null, {
WindowSize->Medium, WindowTitle->"Output", WindowMargins->Automatic,
StyleDefinitions-> FrontEnd`FileName[{"Report"},"StandardReport.nb",CharacterEncoding->"UTF-8"]
}] ];
Clear[fPrint];
SetAttributes[fPrint, HoldAll];
fPrint[expr_]:= Block[ { exprPrint, exprHold=HoldForm[expr] },
NotebookWrite[nb, SelectionMove[nb, Next, Cell]
; CellGroupData[
{
Cell[BoxData[ToBoxes[HoldForm[expr], StandardForm]], "Input"],
(
exprPrint = If[
MatchQ[exprHold, HoldForm[Set[x_,y_]]/;AtomQ[y]]
, exprHold
, exprHold == expr ]
; Cell[BoxData[ToBoxes[exprPrint, TraditionalForm]], "Output", Editable->True]
)
}
, Open], AutoScroll-> False]
; expr
] // Quiet;
$Pre = fPrint;