0
$\begingroup$

I am writing an interactive document and have an idea to make in-line buttons that on demand would create and bring up separate notebooks containing some explanations or a remindings. One example of the code of such a button is as follows:

Button["Show it",
 CreateDocument[
  Column[{
    Spacer[30],
    TextCell["some explanatory text:" , "Text", FontSize -> 20],
    Spacer[10],
    TextCell[
      Row[{Spacer[50], Style["Φ=", 24, Italic], 
        Style[HoldForm[(ϕ*τ^2*ν^3*ω*θ[
            1 - ν*ω])/(τ^2*ν^2 + 
            2*δ*ν*τ + ω^2)^2], Italic, 30]}], 
      TextAlignment -> Center] // TraditionalForm
     }, Alignment -> Left],
      WindowTitle -> "An expression", WindowSize -> {400, 300}], 
     ImageSize -> {70, 18}, Background -> Brown, 
     BaseStyle -> {"GenericButton", Bold}] 

Upon its evaluation a button is created. This button one can copy-pasted into a necessary part of the text, say, into an in-line cell. It works, in principle. It works irreproachably, if there is only a text or a text and any graphics inside. With the formula it is more complex. If the button contains a formula such as the one in the code above, and if I press the button for the first (second and so on ) time I get the document open with the formula that looks as follows:

If you see here no picture, try yourself, what I explained in words.

And that is what I intended to. Now comes my "however". However, if I save/close the main document (the one containing the button code and the button itself, then open it once more and press the button, I get this:

If you see here no picture, try yourself, what I explained in words.

As you see there is a lot of unwanted patantheses appeared. The question is how can I get rid of these parasitic parantheses?

Small addition: I have forgotten to write that there is an evident work around. The expression in question may be saved as an image and in that form passed into the button statement. Then the code is like this

Button["Show it",
 CreateDocument[
  Column[{
    Spacer[30],
    TextCell["some explanatory text:" , "Text", FontSize -> 20],
    Spacer[10],
    ExpressionCell[
     Row[{Spacer[50], 
       Show["Paste here the image of a formula", ImageSize -> 250]}]]
     }, Alignment -> Left],
  WindowTitle -> "An expression", WindowSize -> {400, 300}], 
 ImageSize -> {70, 18}, Background -> Brown, 
 BaseStyle -> {"GenericButton", Bold}]

This is the way, of course, but I am not fond of it. The reason is that my document contains dozens of such buttons. If each of them will contain an image or two, the document size will rise too much.

To summarize: after the very helpful answers made by bil s and bobknight I see two ways of coping with the problem. I first outline the problem below and then discuss the solutions.

The problem consists in making a button to show a window with a formula as described. The difficulty is that the variables met in the formula may take part in calculations in the document and may have already been assigned to something. Even more, this assignment may be needed in some place of the document after the place where the button is used.

I initially tried to cope with it by applying HoldForm. This is not the right way, however, since it is the HoldForm statement that is responsible for extra parentheses that show up when one closes the document, then opens it and then presses the button. I also tried to use the scoping constructs like Module, but with no success, as I explained in discussion with bil s (see comments).

In the present form neither of the answers deal with this problem. However, on the basis of these answers I think I understand now how to deal with it. I see two ways.

First, it is possible to clear the variables met in the formula. Then one should take care that the necessary variables should be defined before each calculations, or each calculation should use a scoping construct. Like this the bil s solutions works as follows:

 Button["Show it", 
 Clear[Φ, τ, ϕ, ν, ω, θ, \
δ]; 
 CreateDocument[
  Column[{Spacer[30], 
    TextCell["some explanatory text:", "Text", FontSize -> 20], 
    Spacer[10], 
    TextCell[
      Row[{Spacer[50], Style["Φ=", Italic, 30], 
        Style[(ϕ τ^2 ν^3 ω θ (1 - ν \
ω))/(τ^2 ν^2 + 
           2 δ ν τ + ω^2)^2, Italic, 30]}], 
      TextAlignment -> Center] // TraditionalForm}, 
   Alignment -> Left], WindowTitle -> "An expression", 
  WindowSize -> {400, 300}], ImageSize -> {70, 18}, 
 Background -> Brown, BaseStyle -> {"GenericButton", Bold}]

Second, one may use the bobknight's solution, but write the formula as a string:

   Button["Show it", CreateDocument[
  Notebook[{
    Cell["some explanatory text:", "Text", FontSize -> 20], Cell[""],

    Cell[BoxData[
      ToBoxes[Row[{TraditionalForm[
          Style["Φ", 30, Italic]], 
         TraditionalForm[Style[" = ", 30, Italic]], 
         TraditionalForm[
          Style["\!\(\*FractionBox[\(ϕ\\\ \*SuperscriptBox[\(\
τ\), \(2\)]\\\ \*SuperscriptBox[\(ν\), \(3\)]\\\ ω\\\ \
θ \((1 - ν\\\ ω)\)\), \
SuperscriptBox[\((\*SuperscriptBox[\(τ\), \(2\)]\\\ \
\*SuperscriptBox[\(ν\), \(2\)] + 2\\\ δ\\\ ν\\\ τ \
+ \*SuperscriptBox[\(ω\), \(2\)])\), \(2\)]]\)", Italic, 
           30]]}]]], TextAlignment -> Center, 
     ShowStringCharacters -> False]

               },

   WindowTitle -> "An expression", WindowSize -> {500, 300}, 
   ShowCellBracket -> False
              ]
                                                           ],

 ImageSize -> Automatic, Background -> Brown, 
 BaseStyle -> {"GenericButton", Bold}]

I like this solution better, since it does not interact with the definitions of the parameters.

$\endgroup$
3
  • 1
    $\begingroup$ Alexei, if this is you, you might want to merge the account you posted this with, with the other account. Please reply to this or flag your post if that's what you want. $\endgroup$
    – Verbeia
    Jul 6, 2013 at 12:48
  • $\begingroup$ Yes, that's me. I loaded from another place and just started to ask, and only then made a loh-in. I have no idea, how to merge it. $\endgroup$ Jul 6, 2013 at 14:01
  • $\begingroup$ @Alexei follow this instruction as far as you are able. Let me know if it works (may take a few days). If it does not I'll contact someone who can help. $\endgroup$
    – Mr.Wizard
    Aug 6, 2013 at 9:49

2 Answers 2

1
$\begingroup$

Remove the extra HoldForm and the extra parentheses go away. This one works for me:

Button["Show it", CreateDocument[Column[{Spacer[30], 
     TextCell["some explanatory text:", "Text", FontSize -> 20], 
     Spacer[10], TextCell[
     Row[{Spacer[50], Style["\[CapitalPhi]=", 24, Italic], 
     Style[(\[Phi] \[Tau]^2 \[Nu]^3 \[Omega] \[Theta][
         1 - \[Nu] \[Omega]])/(\[Tau]^2 \[Nu]^2 + 
         2 \[Delta] \[Nu] \[Tau] + \[Omega]^2)^2, Italic, 30]}], 
     TextAlignment -> Center] // TraditionalForm}, 
     Alignment -> Left], WindowTitle -> "An expression", 
     WindowSize -> {400, 300}], ImageSize -> {70, 18}, 
     Background -> Brown, BaseStyle -> {"GenericButton", Bold}]
$\endgroup$
5
  • $\begingroup$ No, that does not work. If I remove HoldForm the paramtheses are there from the very first button click. $\endgroup$ Jul 6, 2013 at 14:04
  • $\begingroup$ See code. It does not have parentheses before or after restart (except for the ones you've explicitly put there). $\endgroup$
    – bill s
    Jul 6, 2013 at 15:07
  • $\begingroup$ s yes, I tried it. It works as a stand-alone. Not in the document, however, since in the document a number of calculations is being done. If there is no HoldForm the parameters may take values assigned somewhere in the previous parts of the document (and in my case some, indeed, took). $\endgroup$ Jul 6, 2013 at 16:22
  • $\begingroup$ How about this: place your button in a module and localize all the display variables. This way you can safely remove the HoldForm and get rid of the extra parentheses (as per the answer) without interference from other code. Localizing variables within functions is also a good idea in general. $\endgroup$
    – bill s
    Jul 7, 2013 at 7:22
  • $\begingroup$ @bill s. This idea already came to me, but it does not work either. Here is what I see as the result instead of the expected formula: ([Nu]$4721^3 [Tau]$4721^2 [Omega]$4721 [Phi]$4721 [Theta]$4721(1-[Nu]$4721 [Omega]$4721))/(2 [Delta]$4721 [Nu]$4721 [Tau]$4721+[Nu]$4721^2 [Tau]$4721^2+[Omega]$4721^2)^2 $\endgroup$ Jul 8, 2013 at 6:51
1
$\begingroup$

This could be an alternative solution. Using Notebook[] to wrap all cells so you can add some further options and manage the whole document and single cells as well.

Button["Show it", CreateDocument[
  Notebook[{
    Cell["some explanatory text:", "Text", FontSize -> 20],
    Cell[""],
    Cell[BoxData[
      ToBoxes[Row[{TraditionalForm[Style[Φ, 30, Italic]], 
         TraditionalForm[Style[" = ", 30, Italic]], 
         TraditionalForm[
          Style[(ϕ*τ^2*ν^3*ω*θ[
               1 - ν*ω])/(τ^2*ν^2 + 
               2*δ*ν*τ + ω^2)^2, Italic, 
           30]]}]]], TextAlignment -> Center, 
     ShowStringCharacters -> False]}, WindowTitle -> "An expression",
   WindowSize -> {500, 300}, ShowCellBracket -> False]], 
 ImageSize -> Automatic, Background -> Brown, 
 BaseStyle -> {"GenericButton", Bold}]

Note that I used TraditionalForm as wrapper of the Style statement and not to the whole Row. Furthermore, the WindowsSize seems to influence the TraditionalForm. Indeed, with the original size you wrote, the cell was written in a non traditional form, so I changed WindowSize.

Finally, consider the following alternative, where I used just one TraditionalForm wrapper and inside of it I put the whole formula. This can be done when variables are not defined and the == (Equal) will not answer True or False. So, this means in some cases this could be not the right solution.

Button["Show it", CreateDocument[
  Notebook[{
    Cell["some explanatory text:", "Text", FontSize -> 20],
    Cell[""],
    Cell[BoxData[
      ToBoxes[TraditionalForm[
        Style[Φ == (ϕ*τ^2*ν^3*ω*\
θ[1 - ν*ω])/(τ^2*ν^2 + 
              2*δ*ν*τ + ω^2)^2, Italic, 30]]]],
      TextAlignment -> Center, ShowStringCharacters -> False]}, 
   WindowTitle -> "An expression",
   WindowSize -> {500, 300}, ShowCellBracket -> False]], 
 ImageSize -> Automatic, Background -> Brown, 
 BaseStyle -> {"GenericButton", Bold}]

In both cases I get the following:

enter image description here

And, of course, even after saving/closing/opening the notebook, the button's result is alwasy the same. Hope this is helpful.

$\endgroup$
2
  • $\begingroup$ Thank you. The second solutions is shorter, and I can include a Clear statement in front of the CreateDocument statement to clean up the definitions of parameters, if any. Could you please comment, what job BoxData/ToBoxes do here? I see that the result is right, but do not understand, how it works. $\endgroup$ Aug 6, 2013 at 7:43
  • $\begingroup$ In addition to the previous comment. There is still the problem to cope with the variables that eventually have been defined somewhere in calculations before the button. I see two ways to cope with it. The first I described in the previous comment. It is not really satisfactory, since it requires a lot of extra attention to check all calculations. The second is to define the formula as a string. In this case it does not interfere with the definitions. $\endgroup$ Aug 6, 2013 at 9:00

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.