# Use Format while still being able to copy-paste the underlying representation

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.

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

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]];
• All the answers were equivalent, but I'm accepting yours because of its simplicity. Nov 12 '20 at 9:06
• 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.) Nov 12 '20 at 9:07
• 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. Nov 12 '20 at 9:34
• @MannyC I have given a tweak which solves your "raw form" problem. Nov 12 '20 at 12:23
• I'd suggest an edit as per @N0va answer. Replace Interpretation by safeInterpretation where safeInterpretation[formatted, raw] := If[BoxFormUseIcons, Interpretation[formatted, raw], <whatever you want to show in the Information output.>] Nov 16 '20 at 13:46

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", "[", #, "]"}]]
]

Another alternative:

ToInterpretationBox[raw_][formatted_]:=With[{box=ToBoxes[formatted]},InterpretationBox[RowBox[{box}],raw]]
A/:MakeBoxes[A[x_],StandardForm]/;BoxFormUseIcons:=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] /; BoxFormUseIcons := ...

related to the discussion in https://mathematica.stackexchange.com/a/231387/ the undocumented condition BoxFormUseIcons 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.