TransWikia.com

LyX-like LaTeX in Mathematica Text Cells

Mathematica Asked by sferics on December 20, 2020

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?

One Answer

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.

Answered by asd1dsa on December 20, 2020

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