5
$\begingroup$

I believe this is a little problem that everybody asking or answering ODE related question in this site suffers.

We know derivative of a variable, say, f, can be inputted as

f'

But once the f' is executed, or we just keystrokes Ctrl+Shift+n to convert it to StandardForm, it'll become

enter image description here

Convert it to InputForm by Ctrl+Shift+i, or copy and paste it to this site or any text editor, what we obtain is

Derivative[1][f]

Not too bad, but still less concise compared to f'. What's the simplest way to convert it back to f'?

Ideally, can we modify the behavior of Convert to InputForm?

f' above is just an example. I'm expecting a solution that handles Derivative[n][f] where n is an integer, of course.

$\endgroup$
2
  • 1
    $\begingroup$ So what do you want to paste for n=5? $\endgroup$
    – Kuba
    Commented Oct 7, 2020 at 7:24
  • 1
    $\begingroup$ @Kuba f''''' :) Well, I know it no longer looks good, but extremely high order derivative doesn't come up often when dealing with ODEs. And I think it won't be too hard to limit the solution to e.g. n<=4 case, if we can figure out one? $\endgroup$
    – xzczd
    Commented Oct 7, 2020 at 7:31

2 Answers 2

4
$\begingroup$

You can use TemplateBox to control how derivatives are copy/pasted:

MakeBoxes[Derivative[n_Integer?(Between[{1,4}])][f_], StandardForm] := With[
    {p=StringRepeat["\[Prime]",n], q=StringRepeat["'",n]},
    TemplateBox[
        {MakeBoxes[f]},
        "Derivative1",
        DisplayFunction->(SuperscriptBox[#1, p, MultilineFunction->None]&),
        InterpretationFunction->(RowBox[{#1, q}]&)
    ]
]

MakeBoxes[Derivative[n__Integer][f_], StandardForm] := TemplateBox[
    {MakeBoxes[f], RowBox[BoxForm`MakeInfixForm[{n}, ",", StandardForm]]},
    "Derivative2",
    DisplayFunction->(SuperscriptBox[#1, RowBox[{"(",#2,")"}],MultilineFunction->None]&),
    InterpretationFunction->(RowBox[{RowBox[{"Derivative", "[", #2,"]"}],"[",#1,"]"}]&)
]
$\endgroup$
1
  • $\begingroup$ Oh you've readily resolved the other little problem i.e. the direct copying of high order derivative and partial derivative in StandardForm :D . $\endgroup$
    – xzczd
    Commented Oct 8, 2020 at 4:00
3
$\begingroup$

One approach is to override Derivative box formatting and use Copy As -> Plain Text

MakeBoxes[Derivative[n_Integer][f_], form_] := RowBox[{ToBoxes@f, StringRepeat["'", n]}]
$\endgroup$
1
  • 3
    $\begingroup$ It turns out to be unnecessary to copy as plain text, Ctrl+Shift+N works, too. $\endgroup$
    – xzczd
    Commented Oct 7, 2020 at 9:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.