Handling expression differently for display vs. calculation?
Clash Royale CLAN TAG#URR8PPP
up vote
3
down vote
favorite
I have a function I wrap a lot of my numbers with to factor them and display them out a certain way. Usually I do this at the last step of a calculation, but sometimes it would be nice if I could designate that a number should be displayed one way, but handled another way for computation.
For example, suppose I wanted prime numbers in some list to be printed out bold. I might do that with:
nums = If[PrimeQ@#, Style[#,Bold], #]& /@ nums
The problem is that if I then want to, say, take all the elements in that list that are under 100, any code that hits Style
instead of a number will freak out.
Is there something like a wrapper that designates an expression to be handled as some value for subsequent evaluation, but print out a certain way when it comes to display preparation? I know you could achieve this by messing with $PrePrint
, but it seems like there might be a better way.
evaluation output-formatting
New contributor
add a comment |Â
up vote
3
down vote
favorite
I have a function I wrap a lot of my numbers with to factor them and display them out a certain way. Usually I do this at the last step of a calculation, but sometimes it would be nice if I could designate that a number should be displayed one way, but handled another way for computation.
For example, suppose I wanted prime numbers in some list to be printed out bold. I might do that with:
nums = If[PrimeQ@#, Style[#,Bold], #]& /@ nums
The problem is that if I then want to, say, take all the elements in that list that are under 100, any code that hits Style
instead of a number will freak out.
Is there something like a wrapper that designates an expression to be handled as some value for subsequent evaluation, but print out a certain way when it comes to display preparation? I know you could achieve this by messing with $PrePrint
, but it seems like there might be a better way.
evaluation output-formatting
New contributor
Have a look at Interpretation
â Jason B.
9 hours ago
@JasonB. That doesn't work if you put the Interpretation wrapped numbers in a variable. Trynums = Interpretation[If[PrimeQ@#, Style[#, Red], #], #] & /@ Range[15]
and then2*nums
.
â Sjoerd C. de Vries
9 hours ago
add a comment |Â
up vote
3
down vote
favorite
up vote
3
down vote
favorite
I have a function I wrap a lot of my numbers with to factor them and display them out a certain way. Usually I do this at the last step of a calculation, but sometimes it would be nice if I could designate that a number should be displayed one way, but handled another way for computation.
For example, suppose I wanted prime numbers in some list to be printed out bold. I might do that with:
nums = If[PrimeQ@#, Style[#,Bold], #]& /@ nums
The problem is that if I then want to, say, take all the elements in that list that are under 100, any code that hits Style
instead of a number will freak out.
Is there something like a wrapper that designates an expression to be handled as some value for subsequent evaluation, but print out a certain way when it comes to display preparation? I know you could achieve this by messing with $PrePrint
, but it seems like there might be a better way.
evaluation output-formatting
New contributor
I have a function I wrap a lot of my numbers with to factor them and display them out a certain way. Usually I do this at the last step of a calculation, but sometimes it would be nice if I could designate that a number should be displayed one way, but handled another way for computation.
For example, suppose I wanted prime numbers in some list to be printed out bold. I might do that with:
nums = If[PrimeQ@#, Style[#,Bold], #]& /@ nums
The problem is that if I then want to, say, take all the elements in that list that are under 100, any code that hits Style
instead of a number will freak out.
Is there something like a wrapper that designates an expression to be handled as some value for subsequent evaluation, but print out a certain way when it comes to display preparation? I know you could achieve this by messing with $PrePrint
, but it seems like there might be a better way.
evaluation output-formatting
evaluation output-formatting
New contributor
New contributor
New contributor
asked 9 hours ago
Trev
161
161
New contributor
New contributor
Have a look at Interpretation
â Jason B.
9 hours ago
@JasonB. That doesn't work if you put the Interpretation wrapped numbers in a variable. Trynums = Interpretation[If[PrimeQ@#, Style[#, Red], #], #] & /@ Range[15]
and then2*nums
.
â Sjoerd C. de Vries
9 hours ago
add a comment |Â
Have a look at Interpretation
â Jason B.
9 hours ago
@JasonB. That doesn't work if you put the Interpretation wrapped numbers in a variable. Trynums = Interpretation[If[PrimeQ@#, Style[#, Red], #], #] & /@ Range[15]
and then2*nums
.
â Sjoerd C. de Vries
9 hours ago
Have a look at Interpretation
â Jason B.
9 hours ago
Have a look at Interpretation
â Jason B.
9 hours ago
@JasonB. That doesn't work if you put the Interpretation wrapped numbers in a variable. Try
nums = Interpretation[If[PrimeQ@#, Style[#, Red], #], #] & /@ Range[15]
and then 2*nums
.â Sjoerd C. de Vries
9 hours ago
@JasonB. That doesn't work if you put the Interpretation wrapped numbers in a variable. Try
nums = Interpretation[If[PrimeQ@#, Style[#, Red], #], #] & /@ Range[15]
and then 2*nums
.â Sjoerd C. de Vries
9 hours ago
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
3
down vote
Using $PrePrint
is fairly easy.
Define a helper function to use with $PrePrint
to deconflict multiple uses of Slot (#)
boldPrime = If[PrimeQ[#], Style[#, Bold], #] &;
$PrePrint = If[Head[#] === List, boldPrime /@ #, #] &;
boldPrime
is mapped onto lists
nums = Range[25]
nums
can be used subsequently
nums^(1/2)
RandomInteger[0, 100, 20]
It is not mapped onto other forms of expressions
3 x^5
add a comment |Â
up vote
3
down vote
I have tried to do this before and found it ultimately to be a complex problem. Certainly there are easier ways to accomplish specific tasks, such as what Bob Hanlon showed, but that doesn't actually provide the functionality requested.
I believe you will need to choose either a "whitelist" or "blacklist" approach to how functions handle your wrapper. For example we could use UpValues (created with TagSetDelayed
) to tell all functions besides If
, List
, and MakeBoxes
to strip the wrapper. (These specific functions only because I need them for the example to function.)
Foundation:
MakeBoxes[hiddenStyle[x_, sty__], _] := ToBoxes @ Style[x, sty]
p1 = Except[If | List | MakeBoxes];
hiddenStyle /: (h : p1)[a___, hiddenStyle[x_, __], b___] := h[a, x, b]
Usage:
nums = If[PrimeQ@#, hiddenStyle[#, Red], #] & /@ Range[15]
2*nums
Here you see that your style is applied (by way of the MakeBoxes
definition), but arbitrary functions outside of our p1
pattern see the wrapper hiddenStyle
as transparent. This is only a minimal working example and is not intended to be robust.
The other approach is to overload each function that you want to use to see the wrapper as transparent. For example we could write:
ClearAll[hiddenStyle]
p2 = Plus | Times | Power;
MakeBoxes[hiddenStyle[x_, sty__], _] := ToBoxes @ Style[x, sty]
hiddenStyle /: (h : p2)[a___, hiddenStyle[x_, __], b___] := h[a, x, b]
nums = If[PrimeQ@#, hiddenStyle[#, Red], #] & /@ Range[15]
2*nums
foo /@ nums
With more work it is possible to create more complex behaviors such as passing on styling to output, but they often come at a significant cost of performance, e.g. interfering with Listable
behavior.
If you describe your application in more detail I can try to provide additional examples.
add a comment |Â
up vote
0
down vote
Why not create a form wrapper to output primes in bold? For example:
MakeBoxes[myForm[expr_],StandardForm] ^:= Internal`InheritedBlock[MakeBoxes,
MakeBoxes[i_Integer,StandardForm] := If[PrimeQ[i], StyleBox[ToString[i],Red], ToString[i]];
MakeBoxes[expr]
]
Unprotect[$OutputForms];
$OutputForms = DeleteDuplicates[Append[$OutputForms, myForm]];
Protect[$OutputForms];
(I used red instead of bold so that the difference is a bit more noticeable). Then:
foo /@ Range[10] //myForm
Because myForm
is a member of $OutputForms
, the myForm
wrapper gets stripped before getting stored in Out
:
%//InputForm
foo[1], foo[2], foo[3], foo[4], foo[5], foo[6], foo[7], foo[8], foo[9], foo[10]
Copy/paste of will also strip the myForm
wrapper.
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
Using $PrePrint
is fairly easy.
Define a helper function to use with $PrePrint
to deconflict multiple uses of Slot (#)
boldPrime = If[PrimeQ[#], Style[#, Bold], #] &;
$PrePrint = If[Head[#] === List, boldPrime /@ #, #] &;
boldPrime
is mapped onto lists
nums = Range[25]
nums
can be used subsequently
nums^(1/2)
RandomInteger[0, 100, 20]
It is not mapped onto other forms of expressions
3 x^5
add a comment |Â
up vote
3
down vote
Using $PrePrint
is fairly easy.
Define a helper function to use with $PrePrint
to deconflict multiple uses of Slot (#)
boldPrime = If[PrimeQ[#], Style[#, Bold], #] &;
$PrePrint = If[Head[#] === List, boldPrime /@ #, #] &;
boldPrime
is mapped onto lists
nums = Range[25]
nums
can be used subsequently
nums^(1/2)
RandomInteger[0, 100, 20]
It is not mapped onto other forms of expressions
3 x^5
add a comment |Â
up vote
3
down vote
up vote
3
down vote
Using $PrePrint
is fairly easy.
Define a helper function to use with $PrePrint
to deconflict multiple uses of Slot (#)
boldPrime = If[PrimeQ[#], Style[#, Bold], #] &;
$PrePrint = If[Head[#] === List, boldPrime /@ #, #] &;
boldPrime
is mapped onto lists
nums = Range[25]
nums
can be used subsequently
nums^(1/2)
RandomInteger[0, 100, 20]
It is not mapped onto other forms of expressions
3 x^5
Using $PrePrint
is fairly easy.
Define a helper function to use with $PrePrint
to deconflict multiple uses of Slot (#)
boldPrime = If[PrimeQ[#], Style[#, Bold], #] &;
$PrePrint = If[Head[#] === List, boldPrime /@ #, #] &;
boldPrime
is mapped onto lists
nums = Range[25]
nums
can be used subsequently
nums^(1/2)
RandomInteger[0, 100, 20]
It is not mapped onto other forms of expressions
3 x^5
answered 9 hours ago
Bob Hanlon
56k23589
56k23589
add a comment |Â
add a comment |Â
up vote
3
down vote
I have tried to do this before and found it ultimately to be a complex problem. Certainly there are easier ways to accomplish specific tasks, such as what Bob Hanlon showed, but that doesn't actually provide the functionality requested.
I believe you will need to choose either a "whitelist" or "blacklist" approach to how functions handle your wrapper. For example we could use UpValues (created with TagSetDelayed
) to tell all functions besides If
, List
, and MakeBoxes
to strip the wrapper. (These specific functions only because I need them for the example to function.)
Foundation:
MakeBoxes[hiddenStyle[x_, sty__], _] := ToBoxes @ Style[x, sty]
p1 = Except[If | List | MakeBoxes];
hiddenStyle /: (h : p1)[a___, hiddenStyle[x_, __], b___] := h[a, x, b]
Usage:
nums = If[PrimeQ@#, hiddenStyle[#, Red], #] & /@ Range[15]
2*nums
Here you see that your style is applied (by way of the MakeBoxes
definition), but arbitrary functions outside of our p1
pattern see the wrapper hiddenStyle
as transparent. This is only a minimal working example and is not intended to be robust.
The other approach is to overload each function that you want to use to see the wrapper as transparent. For example we could write:
ClearAll[hiddenStyle]
p2 = Plus | Times | Power;
MakeBoxes[hiddenStyle[x_, sty__], _] := ToBoxes @ Style[x, sty]
hiddenStyle /: (h : p2)[a___, hiddenStyle[x_, __], b___] := h[a, x, b]
nums = If[PrimeQ@#, hiddenStyle[#, Red], #] & /@ Range[15]
2*nums
foo /@ nums
With more work it is possible to create more complex behaviors such as passing on styling to output, but they often come at a significant cost of performance, e.g. interfering with Listable
behavior.
If you describe your application in more detail I can try to provide additional examples.
add a comment |Â
up vote
3
down vote
I have tried to do this before and found it ultimately to be a complex problem. Certainly there are easier ways to accomplish specific tasks, such as what Bob Hanlon showed, but that doesn't actually provide the functionality requested.
I believe you will need to choose either a "whitelist" or "blacklist" approach to how functions handle your wrapper. For example we could use UpValues (created with TagSetDelayed
) to tell all functions besides If
, List
, and MakeBoxes
to strip the wrapper. (These specific functions only because I need them for the example to function.)
Foundation:
MakeBoxes[hiddenStyle[x_, sty__], _] := ToBoxes @ Style[x, sty]
p1 = Except[If | List | MakeBoxes];
hiddenStyle /: (h : p1)[a___, hiddenStyle[x_, __], b___] := h[a, x, b]
Usage:
nums = If[PrimeQ@#, hiddenStyle[#, Red], #] & /@ Range[15]
2*nums
Here you see that your style is applied (by way of the MakeBoxes
definition), but arbitrary functions outside of our p1
pattern see the wrapper hiddenStyle
as transparent. This is only a minimal working example and is not intended to be robust.
The other approach is to overload each function that you want to use to see the wrapper as transparent. For example we could write:
ClearAll[hiddenStyle]
p2 = Plus | Times | Power;
MakeBoxes[hiddenStyle[x_, sty__], _] := ToBoxes @ Style[x, sty]
hiddenStyle /: (h : p2)[a___, hiddenStyle[x_, __], b___] := h[a, x, b]
nums = If[PrimeQ@#, hiddenStyle[#, Red], #] & /@ Range[15]
2*nums
foo /@ nums
With more work it is possible to create more complex behaviors such as passing on styling to output, but they often come at a significant cost of performance, e.g. interfering with Listable
behavior.
If you describe your application in more detail I can try to provide additional examples.
add a comment |Â
up vote
3
down vote
up vote
3
down vote
I have tried to do this before and found it ultimately to be a complex problem. Certainly there are easier ways to accomplish specific tasks, such as what Bob Hanlon showed, but that doesn't actually provide the functionality requested.
I believe you will need to choose either a "whitelist" or "blacklist" approach to how functions handle your wrapper. For example we could use UpValues (created with TagSetDelayed
) to tell all functions besides If
, List
, and MakeBoxes
to strip the wrapper. (These specific functions only because I need them for the example to function.)
Foundation:
MakeBoxes[hiddenStyle[x_, sty__], _] := ToBoxes @ Style[x, sty]
p1 = Except[If | List | MakeBoxes];
hiddenStyle /: (h : p1)[a___, hiddenStyle[x_, __], b___] := h[a, x, b]
Usage:
nums = If[PrimeQ@#, hiddenStyle[#, Red], #] & /@ Range[15]
2*nums
Here you see that your style is applied (by way of the MakeBoxes
definition), but arbitrary functions outside of our p1
pattern see the wrapper hiddenStyle
as transparent. This is only a minimal working example and is not intended to be robust.
The other approach is to overload each function that you want to use to see the wrapper as transparent. For example we could write:
ClearAll[hiddenStyle]
p2 = Plus | Times | Power;
MakeBoxes[hiddenStyle[x_, sty__], _] := ToBoxes @ Style[x, sty]
hiddenStyle /: (h : p2)[a___, hiddenStyle[x_, __], b___] := h[a, x, b]
nums = If[PrimeQ@#, hiddenStyle[#, Red], #] & /@ Range[15]
2*nums
foo /@ nums
With more work it is possible to create more complex behaviors such as passing on styling to output, but they often come at a significant cost of performance, e.g. interfering with Listable
behavior.
If you describe your application in more detail I can try to provide additional examples.
I have tried to do this before and found it ultimately to be a complex problem. Certainly there are easier ways to accomplish specific tasks, such as what Bob Hanlon showed, but that doesn't actually provide the functionality requested.
I believe you will need to choose either a "whitelist" or "blacklist" approach to how functions handle your wrapper. For example we could use UpValues (created with TagSetDelayed
) to tell all functions besides If
, List
, and MakeBoxes
to strip the wrapper. (These specific functions only because I need them for the example to function.)
Foundation:
MakeBoxes[hiddenStyle[x_, sty__], _] := ToBoxes @ Style[x, sty]
p1 = Except[If | List | MakeBoxes];
hiddenStyle /: (h : p1)[a___, hiddenStyle[x_, __], b___] := h[a, x, b]
Usage:
nums = If[PrimeQ@#, hiddenStyle[#, Red], #] & /@ Range[15]
2*nums
Here you see that your style is applied (by way of the MakeBoxes
definition), but arbitrary functions outside of our p1
pattern see the wrapper hiddenStyle
as transparent. This is only a minimal working example and is not intended to be robust.
The other approach is to overload each function that you want to use to see the wrapper as transparent. For example we could write:
ClearAll[hiddenStyle]
p2 = Plus | Times | Power;
MakeBoxes[hiddenStyle[x_, sty__], _] := ToBoxes @ Style[x, sty]
hiddenStyle /: (h : p2)[a___, hiddenStyle[x_, __], b___] := h[a, x, b]
nums = If[PrimeQ@#, hiddenStyle[#, Red], #] & /@ Range[15]
2*nums
foo /@ nums
With more work it is possible to create more complex behaviors such as passing on styling to output, but they often come at a significant cost of performance, e.g. interfering with Listable
behavior.
If you describe your application in more detail I can try to provide additional examples.
answered 8 hours ago
Mr.Wizardâ¦
228k294651019
228k294651019
add a comment |Â
add a comment |Â
up vote
0
down vote
Why not create a form wrapper to output primes in bold? For example:
MakeBoxes[myForm[expr_],StandardForm] ^:= Internal`InheritedBlock[MakeBoxes,
MakeBoxes[i_Integer,StandardForm] := If[PrimeQ[i], StyleBox[ToString[i],Red], ToString[i]];
MakeBoxes[expr]
]
Unprotect[$OutputForms];
$OutputForms = DeleteDuplicates[Append[$OutputForms, myForm]];
Protect[$OutputForms];
(I used red instead of bold so that the difference is a bit more noticeable). Then:
foo /@ Range[10] //myForm
Because myForm
is a member of $OutputForms
, the myForm
wrapper gets stripped before getting stored in Out
:
%//InputForm
foo[1], foo[2], foo[3], foo[4], foo[5], foo[6], foo[7], foo[8], foo[9], foo[10]
Copy/paste of will also strip the myForm
wrapper.
add a comment |Â
up vote
0
down vote
Why not create a form wrapper to output primes in bold? For example:
MakeBoxes[myForm[expr_],StandardForm] ^:= Internal`InheritedBlock[MakeBoxes,
MakeBoxes[i_Integer,StandardForm] := If[PrimeQ[i], StyleBox[ToString[i],Red], ToString[i]];
MakeBoxes[expr]
]
Unprotect[$OutputForms];
$OutputForms = DeleteDuplicates[Append[$OutputForms, myForm]];
Protect[$OutputForms];
(I used red instead of bold so that the difference is a bit more noticeable). Then:
foo /@ Range[10] //myForm
Because myForm
is a member of $OutputForms
, the myForm
wrapper gets stripped before getting stored in Out
:
%//InputForm
foo[1], foo[2], foo[3], foo[4], foo[5], foo[6], foo[7], foo[8], foo[9], foo[10]
Copy/paste of will also strip the myForm
wrapper.
add a comment |Â
up vote
0
down vote
up vote
0
down vote
Why not create a form wrapper to output primes in bold? For example:
MakeBoxes[myForm[expr_],StandardForm] ^:= Internal`InheritedBlock[MakeBoxes,
MakeBoxes[i_Integer,StandardForm] := If[PrimeQ[i], StyleBox[ToString[i],Red], ToString[i]];
MakeBoxes[expr]
]
Unprotect[$OutputForms];
$OutputForms = DeleteDuplicates[Append[$OutputForms, myForm]];
Protect[$OutputForms];
(I used red instead of bold so that the difference is a bit more noticeable). Then:
foo /@ Range[10] //myForm
Because myForm
is a member of $OutputForms
, the myForm
wrapper gets stripped before getting stored in Out
:
%//InputForm
foo[1], foo[2], foo[3], foo[4], foo[5], foo[6], foo[7], foo[8], foo[9], foo[10]
Copy/paste of will also strip the myForm
wrapper.
Why not create a form wrapper to output primes in bold? For example:
MakeBoxes[myForm[expr_],StandardForm] ^:= Internal`InheritedBlock[MakeBoxes,
MakeBoxes[i_Integer,StandardForm] := If[PrimeQ[i], StyleBox[ToString[i],Red], ToString[i]];
MakeBoxes[expr]
]
Unprotect[$OutputForms];
$OutputForms = DeleteDuplicates[Append[$OutputForms, myForm]];
Protect[$OutputForms];
(I used red instead of bold so that the difference is a bit more noticeable). Then:
foo /@ Range[10] //myForm
Because myForm
is a member of $OutputForms
, the myForm
wrapper gets stripped before getting stored in Out
:
%//InputForm
foo[1], foo[2], foo[3], foo[4], foo[5], foo[6], foo[7], foo[8], foo[9], foo[10]
Copy/paste of will also strip the myForm
wrapper.
answered 2 hours ago
Carl Woll
60.3k279155
60.3k279155
add a comment |Â
add a comment |Â
Trev is a new contributor. Be nice, and check out our Code of Conduct.
Trev is a new contributor. Be nice, and check out our Code of Conduct.
Trev is a new contributor. Be nice, and check out our Code of Conduct.
Trev 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
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f183487%2fhandling-expression-differently-for-display-vs-calculation%23new-answer', 'question_page');
);
Post as a guest
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
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
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
Have a look at Interpretation
â Jason B.
9 hours ago
@JasonB. That doesn't work if you put the Interpretation wrapped numbers in a variable. Try
nums = Interpretation[If[PrimeQ@#, Style[#, Red], #], #] & /@ Range[15]
and then2*nums
.â Sjoerd C. de Vries
9 hours ago