Group Abstract Group Abstract

Message Boards Message Boards

1
|
10.2K Views
|
11 Replies
|
8 Total Likes
View groups...
Share
Share this post:

[?] Pasting numerical expressions into Input Cells?

Posted 8 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.

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

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.

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

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
POSTED BY: Neil Singer

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
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard