2
$\begingroup$

I am trying to use MMA for some engineering calculations, and the text cell contains both descriptive text and mathematical formulas. The example is as follows:

the resistor R1 = ρ×L/S = 1.8*10/2

I can select this part "1.8*10/2" and execute the "Evaluate in Place" menu function to obtain the calculation result, output will directly replace the selected content. The result is as follows

the resistor R1 = ρ×L/S = 9

But I want to keep the original calculation formula, append "=" and output behind the the selected content; My ideal result is as follows.

the resistor R1 = ρ×L/S = 1.8*10/2 = 9

How can I make it work? Any response will be appreciated.

PS: I believe the people who use mathcad will be interested in this.

Thanks for the guidance from @Domen and @wioiw, the following code works for appending output of evaluation in place. hope to provide some help to those who are interested.

MenuItem["Evaluate in &Place(Output Append)", KernelExecute[NotebookWrite[InputNotebook[],
        StringRiffle @ (Map[StringTrim, #]&) @ (Apply[Identity, #]&) @
            RowBox[{
                If[Head[NotebookRead@InputNotebook[]] === RowBox || Head[NotebookRead@InputNotebook[]] === BoxData, ToString[ToExpression[NotebookRead@InputNotebook[], StandardForm, HoldForm], StandardForm],NotebookRead@InputNotebook[]],
                "=",
                (ToString[#, InputForm]&)@ToExpression@NotebookRead@InputNotebook[]
            }]
    ];],
$\endgroup$
8
  • $\begingroup$ I never use Text cells myself. I do everything in code cell. You could try e=HoldForm[1.8*10/2]; Row[{"the resistor R1 = \[Rho]*L/S = ",e," = ", ReleaseHold[e]}] as an example. But actually the way I do all this is generate Latex from Mathematica and compile to PDF. It takes little more work, but it is the best way to do things :) Mathematica should be just for the computation. The presentation and final report should be left to Latex. Do not use Mathematica like it is Microsoft word. $\endgroup$
    – Nasser
    Commented May 6 at 4:12
  • $\begingroup$ @Nasser, Thank you for your help and prompt. The system in engineering calculations is very complex, with a variety of formulas and many variables. Every inference and calculation require adding symbols, relationship descriptions, etc. Otherwise, it may be difficult for me to read it myself. So textual descriptions are necessary for me. $\endgroup$
    – Yao Li
    Commented May 6 at 4:27
  • $\begingroup$ I understand. As far as I know WRI does not have a product like Mathcad where computation and output automatically presented side by side. So you have to do it "manually". May be someone else can suggest something. For me, I just run the notebook each time I change something, and then compile the Latex and look at the PDF. But may be this workflow is not suitable for everyone. $\endgroup$
    – Nasser
    Commented May 6 at 4:35
  • $\begingroup$ mathematica.stackexchange.com/questions/16165/… $\endgroup$
    – wioiw
    Commented May 6 at 4:46
  • 1
    $\begingroup$ How often do you need to do this? Do you have a whole bunch of similar cells that you want to operate on? Or are you getting string input from a file and you want to operate on strings directly? I ask, because it sounds like you are fine using mouse/keyboard/menu commands, and all you'd need to do is make a copy of the desired expression, paste it to where you want it, and do the evaluate in place there. On the other hand, you tried to make a function to do this, which implies that this needs to be done repeatedly and that the input has a consistent format. $\endgroup$
    – lericr
    Commented May 9 at 17:28

1 Answer 1

2
$\begingroup$

Caution: You'd better back up this file, for unexpected syntax error may cause frontend to break down.

Try appending the below code to $InstallationDirectory/FrontEnd/TextResources/$OperatingSystem/KeyEventTranslations.tr.

This code is only applicable to text cells, i.e. cells with style of "Text", "CodeText", etc.

Item[
    KeyEvent["x", Modifiers -> {Control, Shift}], 
    KernelExecute[NotebookWrite[InputNotebook[], 
        StringRiffle @ (Map[StringTrim, #]&) @ (Apply[Identity, #]&) @ 
            RowBox[{
                NotebookRead@InputNotebook[], 
                "=", 
                (ToString[#, InputForm]&)@ToExpression@NotebookRead@InputNotebook[]
            }]
    ];], 
    MenuEvaluator -> "System"
], 
$\endgroup$
10
  • $\begingroup$ Thank you for your help, but I tried to copy your code into the file ~/Mathematica/14.0/SystemFiles/FrontEnd/TextResources/X/KeyEventTranslations.tr, but pressing "Control+Shift+X" and did not result in any response or output $\endgroup$
    – Yao Li
    Commented May 10 at 10:01
  • $\begingroup$ @YaoLi First of all, you need to confirm that this file is the one that your mma will use. $\endgroup$
    – wioiw
    Commented May 10 at 10:11
  • $\begingroup$ For me, it's not "X" but "Windows" $\endgroup$
    – wioiw
    Commented May 10 at 10:12
  • $\begingroup$ yeah, my Operating System is Ubuntu 22.04, and KeyEventTranslations.tr is only in the directory ~/Mathematica/14.0/SystemFiles/FrontEnd/TextResources/X/ and cannot be found elsewhere $\endgroup$
    – Yao Li
    Commented May 10 at 10:18
  • $\begingroup$ It's quite weird. The code runs well on my machine. $\endgroup$
    – wioiw
    Commented May 10 at 11:08

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.