Handling expression differently for display vs. calculation?

The name of the pictureThe name of the pictureThe name of the pictureClash 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.










share|improve this question







New contributor




Trev is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.



















  • Have a look at Interpretation
    – Jason B.
    6 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
    5 hours ago














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.










share|improve this question







New contributor




Trev is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.



















  • Have a look at Interpretation
    – Jason B.
    6 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
    5 hours ago












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.










share|improve this question







New contributor




Trev is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











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






share|improve this question







New contributor




Trev is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question







New contributor




Trev is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question






New contributor




Trev is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 6 hours ago









Trev

161




161




New contributor




Trev is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Trev is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Trev is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











  • Have a look at Interpretation
    – Jason B.
    6 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
    5 hours ago
















  • Have a look at Interpretation
    – Jason B.
    6 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
    5 hours ago















Have a look at Interpretation
– Jason B.
6 hours ago




Have a look at Interpretation
– Jason B.
6 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
5 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
5 hours ago










2 Answers
2






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]


enter image description here



nums can be used subsequently



nums^(1/2)


enter image description here



RandomInteger[0, 100, 20]


enter image description here



It is not mapped onto other forms of expressions



3 x^5


enter image description here






share|improve this answer



























    up vote
    2
    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


    enter image description here



    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


    enter image description here



    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.






    share|improve this answer




















      Your Answer




      StackExchange.ifUsing("editor", function ()
      return StackExchange.using("mathjaxEditing", function ()
      StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
      StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
      );
      );
      , "mathjax-editing");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "387"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      convertImagesToLinks: false,
      noModals: false,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );






      Trev is a new contributor. Be nice, and check out our Code of Conduct.









       

      draft saved


      draft discarded


















      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






























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      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]


      enter image description here



      nums can be used subsequently



      nums^(1/2)


      enter image description here



      RandomInteger[0, 100, 20]


      enter image description here



      It is not mapped onto other forms of expressions



      3 x^5


      enter image description here






      share|improve this answer
























        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]


        enter image description here



        nums can be used subsequently



        nums^(1/2)


        enter image description here



        RandomInteger[0, 100, 20]


        enter image description here



        It is not mapped onto other forms of expressions



        3 x^5


        enter image description here






        share|improve this answer






















          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]


          enter image description here



          nums can be used subsequently



          nums^(1/2)


          enter image description here



          RandomInteger[0, 100, 20]


          enter image description here



          It is not mapped onto other forms of expressions



          3 x^5


          enter image description here






          share|improve this answer












          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]


          enter image description here



          nums can be used subsequently



          nums^(1/2)


          enter image description here



          RandomInteger[0, 100, 20]


          enter image description here



          It is not mapped onto other forms of expressions



          3 x^5


          enter image description here







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 5 hours ago









          Bob Hanlon

          56k23589




          56k23589




















              up vote
              2
              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


              enter image description here



              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


              enter image description here



              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.






              share|improve this answer
























                up vote
                2
                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


                enter image description here



                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


                enter image description here



                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.






                share|improve this answer






















                  up vote
                  2
                  down vote










                  up vote
                  2
                  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


                  enter image description here



                  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


                  enter image description here



                  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.






                  share|improve this answer












                  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


                  enter image description here



                  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


                  enter image description here



                  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.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 4 hours ago









                  Mr.Wizard♦

                  228k294651019




                  228k294651019




















                      Trev is a new contributor. Be nice, and check out our Code of Conduct.









                       

                      draft saved


                      draft discarded


















                      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.













                       


                      draft saved


                      draft discarded














                      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













































































                      Comments

                      Popular posts from this blog

                      What does second last employer means? [closed]

                      Installing NextGIS Connect into QGIS 3?

                      One-line joke