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


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.

  • 1
    $\begingroup$ 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. $\endgroup$ – m_goldberg Mar 27 '15 at 20:32
  • $\begingroup$ @m_goldberg Sorry and thank you. I should be more focused on English, not only the issue. $\endgroup$ – Kuba Mar 27 '15 at 20:33
  • $\begingroup$ 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. $\endgroup$ – m_goldberg Mar 27 '15 at 22:02
  • $\begingroup$ @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? $\endgroup$ – Kuba Mar 27 '15 at 22:07
  • $\begingroup$ 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. $\endgroup$ – m_goldberg Mar 27 '15 at 22:10

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.

| improve this answer | |
  • $\begingroup$ 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? $\endgroup$ – Kuba Mar 28 '15 at 7:49
  • $\begingroup$ 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. $\endgroup$ – ihojnicki Mar 28 '15 at 13:01
  • 1
    $\begingroup$ You've got the point but those things are related, and Possible Issues section or See also seem like appriopriate places. $\endgroup$ – Kuba Mar 28 '15 at 15:00

I have a possible work around here:

Cell[#, "Output"] &@BoxData@ToBoxes@DateString[]]; 
SelectionMove[EvaluationNotebook[], Previous, Cell]; 
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.

| improve this answer | |
  • $\begingroup$ It has the problem described in mathematica.stackexchange.com/q/24777/5478 $\endgroup$ – Kuba Mar 27 '15 at 21:42
  • $\begingroup$ @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... $\endgroup$ – sacratus Mar 27 '15 at 21:47
  • $\begingroup$ @Kuba reported, but no workaround at the moment. hope they will fix it! $\endgroup$ – sacratus Mar 27 '15 at 22:24
  • $\begingroup$ Yes i reported CurrentValue with CellStyle and used the example from the documentation to show the problem. $\endgroup$ – sacratus Mar 27 '15 at 23:10
  • 1
    $\begingroup$ Ok, instead of CurrentValue you can use this answer $\endgroup$ – Kuba Mar 28 '15 at 8:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.