I would like to create a PasteButton that will paste the following expression into an existing Input Cell and display in the Input Cell with the 6 place precision while keeping the full underlying machine precision. From my efforts I suspect that this is not possible because the only way to obtain the shortened display is to paste an entire Output Cell..
expr = N[x/3 + 2 y/3]
giving
0.333333 x + 0.666667 y
The following is just the simplest approach, which does not work. I haven’t found any other approach that works either.
PasteButton["1/3", expr]
The resulting button pastes the following where the shorter form would be preferable.
0.3333333333333333` x+0.6666666666666666` y
Does anyone know a way to specify a PasteButton that will paste the 6 place Output display into an Input Cell?
This will do what you want. It still has the full resolution – evaluate the result and you can check that all the digits are still there by doing //FullForm.
Thanks Neil. That does paste into an Input Cell with the desired print precision but unfortunately I was deficient in my example. Suppose we have an expression with 2D Boxes such as:
expr = N[x^2/3 + 2 y^2/3]
Then we lose the regular StandardForm.
(Also the real numbers would come from a longer calculation and we couldn’t just revert back to the rational expression.)
The problem with this solution is that it prints an entirely new Input Cell and won’t insert into an existing Input Cell.
But it gives me a suggestion. Perhaps one can insert a dummy expression into the existing cell, copy the entire cell and Replace the dummy expression and then rewrite the Cell in place. I don’t exactly know how to do this yet but will see if I can do it.
Can you initially create the Input cell into which the code is pasted with the Cell option NumberMarks -> False? Or can you set the option after the pasting?
Or maybe you can do something with ButtonBoxOptionsButtonFunction. See CurrentValue[{StyleDefinitions, "Paste"}] for a hint.
Yes! Both of those solutions worked. An ExpressionCell in a longer expression seems to be something like an InlineCell in a Text Cell. In my StyleSheet I use a slight color on Input Cells and no color on Output Cells. When Neil’s Input Cell is evaluated I obtain an Output Cell but the ExpressionCell has the Input coloring. Michael’s formulation solves that minor problem.
A nice collaboration between two experts! Many thanks.
Thanks Carl. That has one minor drawback. It pastes the expression in literally and not as a syntactic unit. Of course, the user could always type a set of parentheses before pasting but might often forget. If it was pasted after a ‘z’ in an existing Input Cell, the ‘z’ would only be associated with the first term in the expression. So I went to a box structure as follows.