TransWikia.com

Convert Derivative[1][f] to f'

Mathematica Asked on June 27, 2021

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.

2 Answers

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,"]"}]&)
]

Correct answer by Carl Woll on June 27, 2021

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]}]

Answered by swish on June 27, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP