# NotebookWrite in EvaluationBox forces Input CellStyle

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

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


(Click)

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, 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

## 2 Answers

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.

• 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 7: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
• 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

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.

• It has the problem described in 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
• Ok, instead of CurrentValue you can use this answer – Kuba Mar 28 '15 at 8:55