1
$\begingroup$

Suppose that I have a Head A that appears pretty often in my code and, in order to make my outputs look nicer, I define

Format[A[x_]]:=Subscript[A,x];

Now if $A_1$ appears in the output and I try to copy and paste it, the result of the paste is Subscript[A,1] rather than A[1].

I would like to find a way such that the data copied to clipboard is not the representation of the expression, but rather its underlying structure. Just like when, for example, I assign it to a variable.

$\endgroup$
2
  • $\begingroup$ Prior to copy and paste, convert the cell (or a duplicate of the cell) to InputForm $\endgroup$
    – Bob Hanlon
    Nov 11 '20 at 17:41
  • $\begingroup$ That doesn't seem to do what I need. The cell becomes Subscript[A,1]. $\endgroup$
    – MannyC
    Nov 11 '20 at 17:59
1
$\begingroup$

I suggest a minor adjustment using this code:

Format[A[x_]] := Interpretation[ 
   Subscript[A, x], A[x]];

which does what you expect with copy/paste.

There are other ways to get fancy with Mathematica which can do a lot more, but this code seems to do what is required.

However, if you want the pasted form to be the "raw" form, you can do that with a slight tweak using Defer[] which was designed for this purpose. Code follows:

Format[A[x_]] := Interpretation[ 
   Subscript[A, x], (Defer@A)[x]];
$\endgroup$
5
  • $\begingroup$ All the answers were equivalent, but I'm accepting yours because of its simplicity. $\endgroup$
    – MannyC
    Nov 12 '20 at 9:06
  • $\begingroup$ Also, the behavior I was going for is slightly different as I wanted to have that the object pasted was the raw form of the expression. This, however, is easily obtained by doing "Copy As > Input Text" (I'm leaving this here for anyone else who might need it.) $\endgroup$
    – MannyC
    Nov 12 '20 at 9:07
  • $\begingroup$ Although I should point out that this tends to give problems when you use those expressions in some functions and then you call ?thatFunction. I am still looking into it. $\endgroup$
    – MannyC
    Nov 12 '20 at 9:34
  • $\begingroup$ @MannyC I have given a tweak which solves your "raw form" problem. $\endgroup$
    – Somos
    Nov 12 '20 at 12:23
  • $\begingroup$ I'd suggest an edit as per @N0va answer. Replace Interpretation by safeInterpretation where safeInterpretation[formatted, raw] := If[BoxForm`UseIcons, Interpretation[formatted, raw], <whatever you want to show in the Information output.>] $\endgroup$
    – MannyC
    Nov 16 '20 at 13:46
1
$\begingroup$

You could use MakeBoxes and create a TemplateBox that controls both display and interpretation:

A /: MakeBoxes[A[x_], StandardForm] := TemplateBox[
    {MakeBoxes[x]},
    "A",
    DisplayFunction -> Function[SubscriptBox["A", #]],
    InterpretationFunction -> Function[RowBox[{"A", "[", #, "]"}]]
]
$\endgroup$
1
$\begingroup$

Another alternative:

ToInterpretationBox[raw_][formatted_]:=With[{box=ToBoxes[formatted]},InterpretationBox[RowBox[{box}],raw]]
A/:MakeBoxes[A[x_],StandardForm]/;BoxForm`UseIcons:=ToInterpretationBox[A[x]][Subscript[A,x]];

which sets up an InterpretationBox for A and this InterpretationBox is copied by default. Using Copy as -> Input Text one can copy the input form. If one really wants the behavior OP described I would recommend the approach presented by Carl Woll in his answer to this question.

As a warning when setting up custom output formats: I would recommend using

MakeBoxes[..., StandartForm] /; BoxForm`UseIcons := ...

related to the discussion in https://mathematica.stackexchange.com/a/231387/ the undocumented condition BoxForm`UseIcons prevents formatting in certain environments especially when using Information, which might otherwise lead to serious problems: the least of which is lost readability in the documentation the worst are serious errors, when one does not exclude Patterns when setting up the formatting.

$\endgroup$

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.