Message Boards Message Boards

[?] Pasting numerical expressions into Input Cells?

Posted 6 years ago

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?

11 Replies

You can just use a Style wrapper instead of Interpretation + ExpressionCell:

PasteButton["1/3", Style[expr, StripOnInput -> True, NumberMarks -> False]]
POSTED BY: Carl Woll

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.

expr = N[x/3 + 2 y/3]
PasteButton["1/3", 
 RawBoxes@StyleBox[RowBox[{"(", ToBoxes[expr], ")"}], 
   NumberMarks -> False]]

When this is pasted into an Input Cell that contains an initial 'z' we now obtain:

z (0.333333 x + 0.666667 y)

This maintains full machine precision in the notebook, but not in the posting. It is now also editable.

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.

David,

You can slightly modify Michael's solution to do what you want including adding to the expression.

PasteButton["1/3", 
 ExpressionCell[expr, "Input", NumberMarks -> False]]

Regards

oops -- I just realized that this loses the extra precision that you wanted...

POSTED BY: Neil Singer

forget my last comment -- It does retain the digits -- I just mistyped the fullform...

I think this does what you want

PasteButton["1/3", 
 ExpressionCell[expr, "Input", NumberMarks -> False]]

Am I correct?

POSTED BY: Neil Singer

Hi Neil,

This seems closer:

PasteButton["1/3", Interpretation[ExpressionCell[expr, "Input", NumberMarks -> False], expr]]

But the contents of the embedded cell still cannot be edited (in either this or your solution).

POSTED BY: Michael Rogers

I can do it with Button but not PasteButton. Maybe this will help:

Button["1/3", CellPrint@ExpressionCell[expr, "Input", NumberMarks -> False]]
POSTED BY: Michael Rogers

Thanks Michael.

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.

David,

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.

POSTED BY: Michael Rogers

David,

Use

PasteButton["1/3", expr // OutputForm]

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.

Regards,

Neil

POSTED BY: Neil Singer

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.)

Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract