HoldForm: Display exactly as entered?
up vote
7
down vote
favorite
Sometimes, Mathematica's internal representation of expressions can give somewhat surprising results. Specifically, I noticed this when I tried to preserve products of fractions for display.
E.g. the following example as in "HoldForm does not Hold Form for fractions sometimes":
i.e. it isn't easily possible to preserve the output fractions as they are written. This is actually surprising though, because Mathematica internally preserves the "product of two fractions" form:
In[1]:= HoldForm[1/x 1/y] // InputForm
Out[1]= HoldForm[(1/x)*(1/y)]
In[2]:= HoldForm[1/x 1/y] // FullForm
Out[2]= HoldForm[Times[Times[1,Power[x,-1]],Times[1,Power[y,-1]]]]
Is there some way to get Mathematica to display such forms as actual product of fractions in any of the formatted output forms, without manually adding holdform constructs?
evaluation output-formatting hold display
New contributor
add a comment |
up vote
7
down vote
favorite
Sometimes, Mathematica's internal representation of expressions can give somewhat surprising results. Specifically, I noticed this when I tried to preserve products of fractions for display.
E.g. the following example as in "HoldForm does not Hold Form for fractions sometimes":
i.e. it isn't easily possible to preserve the output fractions as they are written. This is actually surprising though, because Mathematica internally preserves the "product of two fractions" form:
In[1]:= HoldForm[1/x 1/y] // InputForm
Out[1]= HoldForm[(1/x)*(1/y)]
In[2]:= HoldForm[1/x 1/y] // FullForm
Out[2]= HoldForm[Times[Times[1,Power[x,-1]],Times[1,Power[y,-1]]]]
Is there some way to get Mathematica to display such forms as actual product of fractions in any of the formatted output forms, without manually adding holdform constructs?
evaluation output-formatting hold display
New contributor
1
As to the final question, why not just convert input cell to the output once you are done with the input that should not be modified anyway?
– Kuba♦
Nov 15 at 11:33
Strongly related: (1), (2).
– Alexey Popkov
Nov 15 at 11:35
add a comment |
up vote
7
down vote
favorite
up vote
7
down vote
favorite
Sometimes, Mathematica's internal representation of expressions can give somewhat surprising results. Specifically, I noticed this when I tried to preserve products of fractions for display.
E.g. the following example as in "HoldForm does not Hold Form for fractions sometimes":
i.e. it isn't easily possible to preserve the output fractions as they are written. This is actually surprising though, because Mathematica internally preserves the "product of two fractions" form:
In[1]:= HoldForm[1/x 1/y] // InputForm
Out[1]= HoldForm[(1/x)*(1/y)]
In[2]:= HoldForm[1/x 1/y] // FullForm
Out[2]= HoldForm[Times[Times[1,Power[x,-1]],Times[1,Power[y,-1]]]]
Is there some way to get Mathematica to display such forms as actual product of fractions in any of the formatted output forms, without manually adding holdform constructs?
evaluation output-formatting hold display
New contributor
Sometimes, Mathematica's internal representation of expressions can give somewhat surprising results. Specifically, I noticed this when I tried to preserve products of fractions for display.
E.g. the following example as in "HoldForm does not Hold Form for fractions sometimes":
i.e. it isn't easily possible to preserve the output fractions as they are written. This is actually surprising though, because Mathematica internally preserves the "product of two fractions" form:
In[1]:= HoldForm[1/x 1/y] // InputForm
Out[1]= HoldForm[(1/x)*(1/y)]
In[2]:= HoldForm[1/x 1/y] // FullForm
Out[2]= HoldForm[Times[Times[1,Power[x,-1]],Times[1,Power[y,-1]]]]
Is there some way to get Mathematica to display such forms as actual product of fractions in any of the formatted output forms, without manually adding holdform constructs?
evaluation output-formatting hold display
evaluation output-formatting hold display
New contributor
New contributor
edited Nov 15 at 11:32
Alexey Popkov
38k4104260
38k4104260
New contributor
asked Nov 15 at 11:02
kdb
1413
1413
New contributor
New contributor
1
As to the final question, why not just convert input cell to the output once you are done with the input that should not be modified anyway?
– Kuba♦
Nov 15 at 11:33
Strongly related: (1), (2).
– Alexey Popkov
Nov 15 at 11:35
add a comment |
1
As to the final question, why not just convert input cell to the output once you are done with the input that should not be modified anyway?
– Kuba♦
Nov 15 at 11:33
Strongly related: (1), (2).
– Alexey Popkov
Nov 15 at 11:35
1
1
As to the final question, why not just convert input cell to the output once you are done with the input that should not be modified anyway?
– Kuba♦
Nov 15 at 11:33
As to the final question, why not just convert input cell to the output once you are done with the input that should not be modified anyway?
– Kuba♦
Nov 15 at 11:33
Strongly related: (1), (2).
– Alexey Popkov
Nov 15 at 11:35
Strongly related: (1), (2).
– Alexey Popkov
Nov 15 at 11:35
add a comment |
3 Answers
3
active
oldest
votes
up vote
3
down vote
How about:
MakeBoxes[Times[a_, b_], StandardForm] := RowBox[{MakeBoxes@a, MakeBoxes@b}]
1/x 1/y 1/z
Btw, one could argue that1/x
should be kept in that exact form too, that is no fraction box in input, no fraction box in output.
– Kuba♦
Nov 15 at 11:17
@kuba Oh I forgot about that, now I've turned toMakeBoxes
in all places, thanks for pointing out. As to "no fraction box in input, no fraction box in output" part, I think OP's goal is just obtaining an output consistent with theFullForm
of the expression, so I'd like to stop here. (I admit that's a harder and interesting question though. Perhaps a new question like "Can I make the display of expression changeless at all?" should be asked? )
– xzczd
Nov 15 at 11:28
Useful for my usecase, as it controls exactly what is displayed how. I suppose nothing more catch-all than this is doable. @xzczd I'd also prefer if it were possible to keep "1/x" vs the 2-dimensional form separate, but I don't think that this distinction is even kept in the internal representation.
– kdb
Nov 15 at 15:06
@kdb Yeah, if $1/x$ and $frac{1}{x}$ needs to be distincted, I'm afraid something as shown in Kuba's answer is necessary. I hope I'm wrong.
– xzczd
Nov 15 at 15:37
add a comment |
up vote
3
down vote
IMO, the only way to be sure is to interfere as soon as possible:
$PreRead = Function[boxes
, boxes /.
RowBox[{"RawInput", "[", hf_, "]"}] :>
RowBox[{"RawBoxes", "[", ToBoxes[hf], "]"}]
];
add a comment |
up vote
3
down vote
Another possibility is to use InString
:
1/x 1/y
ToExpression[InString[-1], StandardForm, RawBoxes]
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
How about:
MakeBoxes[Times[a_, b_], StandardForm] := RowBox[{MakeBoxes@a, MakeBoxes@b}]
1/x 1/y 1/z
Btw, one could argue that1/x
should be kept in that exact form too, that is no fraction box in input, no fraction box in output.
– Kuba♦
Nov 15 at 11:17
@kuba Oh I forgot about that, now I've turned toMakeBoxes
in all places, thanks for pointing out. As to "no fraction box in input, no fraction box in output" part, I think OP's goal is just obtaining an output consistent with theFullForm
of the expression, so I'd like to stop here. (I admit that's a harder and interesting question though. Perhaps a new question like "Can I make the display of expression changeless at all?" should be asked? )
– xzczd
Nov 15 at 11:28
Useful for my usecase, as it controls exactly what is displayed how. I suppose nothing more catch-all than this is doable. @xzczd I'd also prefer if it were possible to keep "1/x" vs the 2-dimensional form separate, but I don't think that this distinction is even kept in the internal representation.
– kdb
Nov 15 at 15:06
@kdb Yeah, if $1/x$ and $frac{1}{x}$ needs to be distincted, I'm afraid something as shown in Kuba's answer is necessary. I hope I'm wrong.
– xzczd
Nov 15 at 15:37
add a comment |
up vote
3
down vote
How about:
MakeBoxes[Times[a_, b_], StandardForm] := RowBox[{MakeBoxes@a, MakeBoxes@b}]
1/x 1/y 1/z
Btw, one could argue that1/x
should be kept in that exact form too, that is no fraction box in input, no fraction box in output.
– Kuba♦
Nov 15 at 11:17
@kuba Oh I forgot about that, now I've turned toMakeBoxes
in all places, thanks for pointing out. As to "no fraction box in input, no fraction box in output" part, I think OP's goal is just obtaining an output consistent with theFullForm
of the expression, so I'd like to stop here. (I admit that's a harder and interesting question though. Perhaps a new question like "Can I make the display of expression changeless at all?" should be asked? )
– xzczd
Nov 15 at 11:28
Useful for my usecase, as it controls exactly what is displayed how. I suppose nothing more catch-all than this is doable. @xzczd I'd also prefer if it were possible to keep "1/x" vs the 2-dimensional form separate, but I don't think that this distinction is even kept in the internal representation.
– kdb
Nov 15 at 15:06
@kdb Yeah, if $1/x$ and $frac{1}{x}$ needs to be distincted, I'm afraid something as shown in Kuba's answer is necessary. I hope I'm wrong.
– xzczd
Nov 15 at 15:37
add a comment |
up vote
3
down vote
up vote
3
down vote
How about:
MakeBoxes[Times[a_, b_], StandardForm] := RowBox[{MakeBoxes@a, MakeBoxes@b}]
1/x 1/y 1/z
How about:
MakeBoxes[Times[a_, b_], StandardForm] := RowBox[{MakeBoxes@a, MakeBoxes@b}]
1/x 1/y 1/z
edited Nov 15 at 11:21
answered Nov 15 at 11:13
xzczd
25.5k468243
25.5k468243
Btw, one could argue that1/x
should be kept in that exact form too, that is no fraction box in input, no fraction box in output.
– Kuba♦
Nov 15 at 11:17
@kuba Oh I forgot about that, now I've turned toMakeBoxes
in all places, thanks for pointing out. As to "no fraction box in input, no fraction box in output" part, I think OP's goal is just obtaining an output consistent with theFullForm
of the expression, so I'd like to stop here. (I admit that's a harder and interesting question though. Perhaps a new question like "Can I make the display of expression changeless at all?" should be asked? )
– xzczd
Nov 15 at 11:28
Useful for my usecase, as it controls exactly what is displayed how. I suppose nothing more catch-all than this is doable. @xzczd I'd also prefer if it were possible to keep "1/x" vs the 2-dimensional form separate, but I don't think that this distinction is even kept in the internal representation.
– kdb
Nov 15 at 15:06
@kdb Yeah, if $1/x$ and $frac{1}{x}$ needs to be distincted, I'm afraid something as shown in Kuba's answer is necessary. I hope I'm wrong.
– xzczd
Nov 15 at 15:37
add a comment |
Btw, one could argue that1/x
should be kept in that exact form too, that is no fraction box in input, no fraction box in output.
– Kuba♦
Nov 15 at 11:17
@kuba Oh I forgot about that, now I've turned toMakeBoxes
in all places, thanks for pointing out. As to "no fraction box in input, no fraction box in output" part, I think OP's goal is just obtaining an output consistent with theFullForm
of the expression, so I'd like to stop here. (I admit that's a harder and interesting question though. Perhaps a new question like "Can I make the display of expression changeless at all?" should be asked? )
– xzczd
Nov 15 at 11:28
Useful for my usecase, as it controls exactly what is displayed how. I suppose nothing more catch-all than this is doable. @xzczd I'd also prefer if it were possible to keep "1/x" vs the 2-dimensional form separate, but I don't think that this distinction is even kept in the internal representation.
– kdb
Nov 15 at 15:06
@kdb Yeah, if $1/x$ and $frac{1}{x}$ needs to be distincted, I'm afraid something as shown in Kuba's answer is necessary. I hope I'm wrong.
– xzczd
Nov 15 at 15:37
Btw, one could argue that
1/x
should be kept in that exact form too, that is no fraction box in input, no fraction box in output.– Kuba♦
Nov 15 at 11:17
Btw, one could argue that
1/x
should be kept in that exact form too, that is no fraction box in input, no fraction box in output.– Kuba♦
Nov 15 at 11:17
@kuba Oh I forgot about that, now I've turned to
MakeBoxes
in all places, thanks for pointing out. As to "no fraction box in input, no fraction box in output" part, I think OP's goal is just obtaining an output consistent with the FullForm
of the expression, so I'd like to stop here. (I admit that's a harder and interesting question though. Perhaps a new question like "Can I make the display of expression changeless at all?" should be asked? )– xzczd
Nov 15 at 11:28
@kuba Oh I forgot about that, now I've turned to
MakeBoxes
in all places, thanks for pointing out. As to "no fraction box in input, no fraction box in output" part, I think OP's goal is just obtaining an output consistent with the FullForm
of the expression, so I'd like to stop here. (I admit that's a harder and interesting question though. Perhaps a new question like "Can I make the display of expression changeless at all?" should be asked? )– xzczd
Nov 15 at 11:28
Useful for my usecase, as it controls exactly what is displayed how. I suppose nothing more catch-all than this is doable. @xzczd I'd also prefer if it were possible to keep "1/x" vs the 2-dimensional form separate, but I don't think that this distinction is even kept in the internal representation.
– kdb
Nov 15 at 15:06
Useful for my usecase, as it controls exactly what is displayed how. I suppose nothing more catch-all than this is doable. @xzczd I'd also prefer if it were possible to keep "1/x" vs the 2-dimensional form separate, but I don't think that this distinction is even kept in the internal representation.
– kdb
Nov 15 at 15:06
@kdb Yeah, if $1/x$ and $frac{1}{x}$ needs to be distincted, I'm afraid something as shown in Kuba's answer is necessary. I hope I'm wrong.
– xzczd
Nov 15 at 15:37
@kdb Yeah, if $1/x$ and $frac{1}{x}$ needs to be distincted, I'm afraid something as shown in Kuba's answer is necessary. I hope I'm wrong.
– xzczd
Nov 15 at 15:37
add a comment |
up vote
3
down vote
IMO, the only way to be sure is to interfere as soon as possible:
$PreRead = Function[boxes
, boxes /.
RowBox[{"RawInput", "[", hf_, "]"}] :>
RowBox[{"RawBoxes", "[", ToBoxes[hf], "]"}]
];
add a comment |
up vote
3
down vote
IMO, the only way to be sure is to interfere as soon as possible:
$PreRead = Function[boxes
, boxes /.
RowBox[{"RawInput", "[", hf_, "]"}] :>
RowBox[{"RawBoxes", "[", ToBoxes[hf], "]"}]
];
add a comment |
up vote
3
down vote
up vote
3
down vote
IMO, the only way to be sure is to interfere as soon as possible:
$PreRead = Function[boxes
, boxes /.
RowBox[{"RawInput", "[", hf_, "]"}] :>
RowBox[{"RawBoxes", "[", ToBoxes[hf], "]"}]
];
IMO, the only way to be sure is to interfere as soon as possible:
$PreRead = Function[boxes
, boxes /.
RowBox[{"RawInput", "[", hf_, "]"}] :>
RowBox[{"RawBoxes", "[", ToBoxes[hf], "]"}]
];
edited Nov 15 at 11:48
answered Nov 15 at 11:42
Kuba♦
102k12199509
102k12199509
add a comment |
add a comment |
up vote
3
down vote
Another possibility is to use InString
:
1/x 1/y
ToExpression[InString[-1], StandardForm, RawBoxes]
add a comment |
up vote
3
down vote
Another possibility is to use InString
:
1/x 1/y
ToExpression[InString[-1], StandardForm, RawBoxes]
add a comment |
up vote
3
down vote
up vote
3
down vote
Another possibility is to use InString
:
1/x 1/y
ToExpression[InString[-1], StandardForm, RawBoxes]
Another possibility is to use InString
:
1/x 1/y
ToExpression[InString[-1], StandardForm, RawBoxes]
answered Nov 15 at 13:20
Alexey Popkov
38k4104260
38k4104260
add a comment |
add a comment |
kdb is a new contributor. Be nice, and check out our Code of Conduct.
kdb is a new contributor. Be nice, and check out our Code of Conduct.
kdb is a new contributor. Be nice, and check out our Code of Conduct.
kdb is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f186025%2fholdform-display-exactly-as-entered%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
As to the final question, why not just convert input cell to the output once you are done with the input that should not be modified anyway?
– Kuba♦
Nov 15 at 11:33
Strongly related: (1), (2).
– Alexey Popkov
Nov 15 at 11:35