9
$\begingroup$

Lately I've been learning more about the Wolfram Language, and I've become particularly enamored with the stated goals of the Mathematica notebook interface. I've used Mathematica exclusively as a "super calculator" since about 2011, but I think the idea of a living publication, that includes text, figures, AND computation in a single document, could be a total paradigm shift.

However, I find that this goal for the notebook interface is completely undercut by the inability to properly typeset mathematics in a Mathematica notebook. I am familiar with Mathematica's "math typesetting" achieved via the CTRL key, but this is woefully inadequate in comparison to LaTeX. In my search for how to typeset LaTeX inside Mathematica Text cells, the closest solution I've come across uses the wonderful Mathematica package MaTeX, as described in this StackExchange answer: How to use $\LaTeX$ in Mathematica text. However, I find this procedure (generating the TeX with a MaTeX command, and then dragging that into the Text cell) very awkward.

Is there a way to create a custom keyboard command (e.g. "CMD + m") that would activate MaTeX typesetting within a Text cell? I'm imagining something like LyX, where you press the keyboard command and are instantly typing TeX code that will be compiled and displayed after you press the command again or press the right-key. Here is a short gif showing what I mean in LyX: https://thumbs.gfycat.com/GiftedHatefulCaterpillar-small.gif

I've contacted via email the creator of MaTeX, Szabolcs Horvát, and he seems optimistic about this being possible, but lacks the time and Front End development experience to implement this. Does anyone here know if/how this may be achieved with Mathematica?

$\endgroup$
3
$\begingroup$

Step 1: Set a hot key for the convertion from Wolfram Language codes to MaTeX graphics.

This have been discussed for many times on MSE. (See KeyEventsTranslation.tr and FrontEndEventActions)

(* Add this to your KeyEventsTranslation.tr in your $UserBaseDirectory *)
Item[KeyEvent["F4"(*, Modifiers -> {Control, Shift}*)], KernelExecute[
    MaTeXUtilities`Action["ToMaTeX"] (* We will define it later *)
], MenuEvaluator -> Automatic]

Step 2: Define what to do when the key F4 is pressed.

Execute following code to define the behavior of F4. Add these codes to your init.m if you don't want to execute them manually for each session of Mathematica.

BeginPackage["MaTeXUtilities`", {"MaTeX`"}]
Begin["`Private`"]

ClearAll@"`*"

MaTeXUtilities`Action["ToMaTeX"] := With[{selected = NotebookRead@EvaluationNotebook[]},
    Switch[selected, 
        _Cell,
            NotebookWrite[
                EvaluationNotebook[],
                MaTeXUtilities`MaTeXTemplate@First@selected,
                All
            ],
        _?System`Convert`TeXFormDump`BoxQ,
            NotebookWrite[
                EvaluationNotebook[],
                MaTeXUtilities`MaTeXTemplate@selected,
                All
            ]
    ]
]

MaTeXUtilities`MaTeXTemplate[inputBox_] := TemplateBox[
    {inputBox},
    "MaTeXTemplate"
    , InterpretationFunction -> Function@inputBox
    , DisplayFunction -> Function@Evaluate@ToBoxes[MaTeX`MaTeX@Convert`TeX`BoxesToTeX@inputBox, StandardForm]
]

End[]
EndPackage[]

There isn't an Action["FromMaTeX"] implemented because you can use the Convert To: InputForm(Ctrl+Shift+N) menu command for that.

Step 3: Restart Mathematica and try it.

Select some codes/formulae and press the hot key.

It may cost some time for loading MaTeX` and producing a new (not cached) Graphics.

$\endgroup$
3
  • 1
    $\begingroup$ This sounds interesting, but I'm not sure where to find the proper KeyEventsTranslation.tr and init.m to modify (even after following the link and googling). I tried some things, didn't work. ¯\_(ツ)_/¯ Could you add a little extra detail for those of us who don't dig into $UserBaseDirectory too often? $\endgroup$ – Chris K Nov 22 '20 at 15:37
  • $\begingroup$ @ChrisK The tr file is created as $UserBaseDirectory\SystemFiles\FrontEnd\TextResources\Windows\KeyEventTranslations.tr for me (the path may not exist so you should create it). The path should be modified to match your OS. The init.m file is in $UserBaseDirectory\Kernel\init.m $\endgroup$ – asd1dsa Nov 22 '20 at 18:05
  • 1
    $\begingroup$ @ChrisK The required structure of $UserBaseDirectory\SystemFiles\FrontEnd is similar to $InstallationDirectory\SystemFiles\FrontEnd. You can dig into the latter path and see how the .tr(TextResources) files are arranged. $\endgroup$ – asd1dsa Nov 22 '20 at 18:09

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.