3

There is a promissing new function EvaluationBox[] and a nice last example in documentation section for it:

Button["date", NotebookWrite[EvaluationBox[], ToBoxes @ DateString[]]]

enter image description here

(Click)

enter image description here

The problem is that this string is in an "Input" cell now! Why was it converted?

Quite a problem because it is not easy to change cell style.


I was trying:

Button["date",  NotebookWrite[EvaluationBox[], 
   Cell[#, "Output"] & @ BoxData @ ToBoxes @ DateString[]]]

but the problem remains. On the other hand we can see that it is not a problem of NotebookWrite itself but of EvaluationBox[] because EvaluationCell replaced by the code above gives correct output:

Button["date", NotebookWrite[EvaluationCell[], 
  Cell[#, "Output"] & @ BoxData @ ToBoxes @ DateString[]]]

Any thougts? Quick work arounds?

I've introduced one, ugly but working here: Getting the Box of a Button . I was trying to improve it with EvluationBox[] but the I've faced this issue.

Kuba
  • 136,707
  • 13
  • 279
  • 740
  • 1
    Kuba, I have edited "walkaround" in your posts to "work around" several times in the past, but I think you should make the edit this time to help you learn the correct English idiom. – m_goldberg Mar 27 '15 at 20:32
  • @m_goldberg Sorry and thank you. I should be more focused on English, not only the issue. – Kuba Mar 27 '15 at 20:33
  • I don't see how this can be fixed by a work around. EvaluationBox is going to create a cell no matter what, and you don't want a cell. If you did, sacratus' answer would serve you. But because you want a box structure to insert into a possibly much larger expression, I think EvaluationBox is simply inappropriate for the purpose you have in mind. Therefore, I would not call what you have encountered a bug. – m_goldberg Mar 27 '15 at 22:02
  • @m_goldberg Box is supposed to be part of Cell so I don't see how one can expect that replacing EvaluationBox may influence whole cell. Or have I missed something? – Kuba Mar 27 '15 at 22:07
  • It looks to me that you have missed that any use of EvaluationBox has the side effect of deleting the output cell containing the button and then creating a new cell in your notebook. – m_goldberg Mar 27 '15 at 22:10
  • @m_goldberg I'd have missed this if it was stated somewhere. Now, it makes not sense :( "EvaluationBox[] returns a BoxObject corresponding to the box structure in which this function is being evaluated." – Kuba Mar 27 '15 at 22:13
  • The documentation for EvaluationBox is very poor and looks to be hastily written. I think you have a good case for complaining to WRI about the documentation. – m_goldberg Mar 27 '15 at 22:15
  • @m_goldberg on my way! :) – Kuba Mar 27 '15 at 22:16

2 Answers2

5

Using NotebookWrite in this manner is really no different from manually modifying the content of an Output cell.

The FrontEnd converts the cell to Input, since it anticipates the user would be interested in evaluating it afterwards.

What style is used is determined by DefaultDuplicateCellStyle.

Kuba
  • 136,707
  • 13
  • 279
  • 740
ihojnicki
  • 3,026
  • 1
  • 16
  • 11
  • If it is so, ok, I can undestand motivation. However lack of description in NotebookWrite page or no link around to DefaultDuplicateCellStyle even in Low Level Notebook Programming guide are enough to defend my angry attitude. What do you think? – Kuba Mar 28 '15 at 07:49
  • I am not certain I would expect the behavior of the editor to be documented in a function page. It would kinda imply that the behavior is specific to calling the function, which is obviously not true. However, it is completely reasonable suggestion to document this behavior in a more discoverable fashion. – ihojnicki Mar 28 '15 at 13:01
  • 1
    You've got the point but those things are related, and Possible Issues section or See also seem like appriopriate places. – Kuba Mar 28 '15 at 15:00
1

I have a possible work around here:

Button["date", 
NotebookWrite[EvaluationCell[], 
Cell[#, "Output"] &@BoxData@ToBoxes@DateString[]]; 
SelectionMove[EvaluationNotebook[], Previous, Cell]; 
CurrentValue[Cells[NotebookSelection[EvaluationNotebook[]]][[1]], 
StyleNames] = "Subsection"]

unforunately i could not test EvaluationBox[] because i have only Mathematica 9 available here. I used EvaluationCell instead and changed the style of the cell to "Subsection" for demonstation. Use "Output" instead if it works for EvaluationBox.

sacratus
  • 1,556
  • 10
  • 18
  • It has the problem described in http://mathematica.stackexchange.com/q/24777/5478 – Kuba Mar 27 '15 at 21:42
  • @Kuba Strange, can one make a bugreport on this? The idea with CurrentValue is from the documentation of Cells! Ugly that it messes up the CellStyles. I'll have another look on this... – sacratus Mar 27 '15 at 21:47
  • @Kuba reported, but no workaround at the moment. hope they will fix it! – sacratus Mar 27 '15 at 22:24
  • Yes i reported CurrentValue with CellStyle and used the example from the documentation to show the problem. – sacratus Mar 27 '15 at 23:10
  • Great, keep us informed :) – Kuba Mar 27 '15 at 23:13
  • 1
    Ok, instead of CurrentValue you can use this answer – Kuba Mar 28 '15 at 08:55
  • @Kuba i got an answer from WRI, they say its not a bug: ("In this case, Mathematica is behaving as designed. In particular, the StyleNames item does not apply to the cells you are looking at. Further, StyleNames is an undocumented symbol, meaning that it's usage can change without warning. In this particular case, however, if you simply ask for the current value of StyleNames for a given cell, you get back an empty list. Please see the attached notebook for details.") – sacratus Mar 30 '15 at 21:27