8
$\begingroup$

If I have a notebook and I am just typing out ideas, I would like to be able to write math just as TeX style formatting for display and not as input to Mathematica.

I have found that I can use CTRL-4 to enter TeX style input, but the problem is that I cannot enter multiline equations or aligned environments like below...

\begin{aligned}
f(x) &= \int_0^1 x^2 dx \\
&= \frac{(1)^3}{3} - \frac{(0)^3}{3} \\
\end{aligned}

Is it possible to enter math like this for display in Mathematica?

$\endgroup$
3
  • $\begingroup$ align is a math environment. So you should not be using "$$" at all around it. Then it works. But the generated cell is not sized well and the math is hard to read. There might be a way around this issue of cell size. I do not know now. This TeX input is new and still has bugs in it. $\endgroup$
    – Nasser
    Commented Feb 16, 2022 at 6:51
  • $\begingroup$ @Nasser, I originally tried to write math on this website with $$, that is a typo and not what I was trying to write in Mathematica. I cannot even get the align environment to work in Mathematica. Is there a way to makr CTRL-4 do a multiline input? $\endgroup$
    – Joff
    Commented Feb 16, 2022 at 6:58
  • $\begingroup$ Do others agree it's a bug? $\endgroup$
    – Michael E2
    Commented Apr 3, 2022 at 4:03

4 Answers 4

5
$\begingroup$

align is a math environment. So you should not be using "$$" at all around it. Then it works. But the generated cell is not sized well and the math is hard to read.

From help on Insert ▶ Inline TeX Input it says

inline math–mode

Which for me means it will not work with align as this is not inline math.

enter image description here

Also do not add \\ at the end of the last line in align. This will result in uneeded extra space at the end which does not look good.

As a work around, and since this is just for display, why not use MaTeX?

MaTeX["
\\begin{aligned}
f(x) &= \\int_0^1 x^2 dx \\\\
&= \\frac{(1)^3}{3} - \\frac{(0)^3}{3}
\\end{aligned}
", Magnification -> 2]

Which gives

Mathematica graphics

$\endgroup$
2
  • $\begingroup$ How do I get MaTeX to work? It doesn't seem to be a function on my plain Mathematica install $\endgroup$
    – Joff
    Commented Feb 16, 2022 at 7:02
  • $\begingroup$ @Joff Oh, Matex is an extra package that you need to install. see MateX Added link in the answer also. $\endgroup$
    – Nasser
    Commented Feb 16, 2022 at 7:03
5
$\begingroup$

Caveat: While playing with this trying to get an exact replica of TeX, I occasionally crashed the front end. (Ouch.) Probably because I made a typo in editing the cell expression. I think the code below is safe, but I felt I should warn people. I can't prove that I'm right. It's mainly to demonstrate how the bug could be fixed.

It seems to be a bug, which should be reported to WRI. There's a missing BoxData and possibly a missing FormBox. (The resulting boxes are more elaborate than needed, but the elaboration might be the result of a straightforward programming approach -- that is, each item in the alignment grid gets its own Cell[].) Anyway, the following hack of internals fixes aligned equations with the setting of $fixEquationAlignment below, and it may break other alignments (slightly). One might be able to figure out a setting for $fixEquationAlignment in other cases, but if you mix alignments in one TeX cell, then probably you have to go in and edit the cell expression directly.

(* How to unset the damage done below *)
System`Convert`TeXImportDump`TeXImport[args__] /; ! 
   TrueQ[$pleaseFixAlignment] =.

(*this fixes aligned equations*)
$fixEquationAlignment = {"Columns" -> {Right, {Left}},
 "Rows" -> {{Baseline}}};
DownValues[System`Convert`TeXImportDump`TeXImport] = 
  Join[{HoldPattern[
      System`Convert`TeXImportDump`TeXImport[args__] /; ! 
        TrueQ[$pleaseFixAlignment]] :> 
     Block[{$pleaseFixAlignment = True}, 
      System`Convert`TeXImportDump`TeXImport[args] /.
        Cell[RowBox[data__], rest___] :> 
         Cell[BoxData@FormBox[RowBox[data], TraditionalForm], rest] /.
       g_GridBox :> 
        Append[g, GridBoxAlignment -> $fixEquationAlignment]]}, 
   DownValues[System`Convert`TeXImportDump`TeXImport]];

Note that it does not really do TeX: Mathematica translates TeX into its own boxes. It does a pretty good job, but there are differences.


Update: Version 2

This produces both a simpler box structure and better spacing for equations:

(*How to unset the damage done below*)
System`Convert`TeXImportDump`TeXImport[args__] /; !TrueQ[$pleaseFixAlignment] =.

(*this fixes aligned equations*)
$fixEquationAlignment = {"Columns" -> {Right, {Left}}, "Rows" -> {{Baseline}}};
DownValues[System`Convert`TeXImportDump`TeXImport] = 
  Join[{HoldPattern[
      System`Convert`TeXImportDump`TeXImport[args__] /; ! 
        TrueQ[$pleaseFixAlignment]] :> 
     Block[{$pleaseFixAlignment = True}, 
      System`Convert`TeXImportDump`TeXImport[args] /.
        Cell[TextData[{" ", Cell[b_, ___], ___}], ___] :> b /.
       g_GridBox :> 
        Join[g, 
         GridBox[GridBoxAlignment -> $fixEquationAlignment, 
          GridBoxSpacings -> {"Columns" -> {Offset[
               0.27999999999999997`], {Offset[0.]}, 
              Offset[0.27999999999999997`]}, 
            "Rows" -> {Offset[0.2], {Offset[0.4]}, Offset[0.2]}}]]]}, 
   DownValues[System`Convert`TeXImportDump`TeXImport]];

Or one can postprocess a cell by executing this below the cell with TeX (uses $fixEquationAlignment):

NotebookWrite[
 PreviousCell[],
 NotebookRead@PreviousCell[] /.
   Cell[TextData[{" ", Cell[b_, ___], ___}], ___] :> b /.
  g_GridBox :> 
   Join[g, 
    GridBox[GridBoxAlignment -> $fixEquationAlignment, 
     GridBoxSpacings -> {"Columns" -> {Offset[
          0.27999999999999997`], {Offset[0.]}, 
         Offset[0.27999999999999997`]}, 
       "Rows" -> {Offset[0.2], {Offset[0.4]}, Offset[0.2]}}]]
 ]
$\endgroup$
4
$\begingroup$

Here's a hack to get control-4 to use MaTeX (see http://szhorvat.net/pelican/latex-typesetting-in-mathematica.html and MaTeXInstall[]) to format TeX in inline cells:

(* How to unset the damage done below *)
InputAssistant`TeXStringToBoxes // Unprotect;
InputAssistant`TeXStringToBoxes[s_String] /; TrueQ@$useMaTeXQ =.;
InputAssistant`TeXStringToBoxes // Protect;

(* use MaTeX to format inline TeX *)
Needs@"MaTeX`";
$useMaTeXMag = 1;
$useMaTeXBaselineShift = 0;
InputAssistant`TeXStringToBoxes // Unprotect;
InputAssistant`TeXStringToBoxes[s_String] /; TrueQ@$useMaTeXQ :=
  AdjustmentBox[
   ToBoxes@MaTeX[s, Magnification -> $useMaTeXMag],
   BoxBaselineShift -> $useMaTeXBaselineShift];
InputAssistant`TeXStringToBoxes // Protect;

$useMaTeXQ = True;

The parameters are included because first trial was x^2, and the result did not align well with text in a Text cell. So I made some adjustments:

$useMaTeXMag = 1.44; (* x^2 and x have the ~same size in a Text cell *)
$useMaTeXBaselineShift = -0.12;  (* aligns x^2 with x in a Text cell *)

You may wish to test some parameters and the MaTeX options such as ContentPadding before fixing a style for a document. There's no way to encode Mathematica parameter assignments in the inline-TeX input mechanism that I've discovered. Of course, it might make the most sense to encode the magnification in TeX itself. This could be done with the option "Preamble" and a $useMaTeXPreamble variable, so that you wouldn't have to type it in every instance of TeX input.

I wonder if the Inline TeX Input assistant can be cloned, to create an independent Inline MaTeX assistant or to call any other external service and insert boxes in a cell. (Obviously, one cannot have control-4 do more than one thing, but one could use a palette.) The InputAssistant` context seems to deal only with TeX, but the name suggests the functionality could be expanded.

Update: Newlines

You can't type a newline (return) directly, since it cause the FE to call the display function (TeX/MaTeX). You can type \[NewLine], the character code \.0a, or type out the TeX, copy it and paste it in. I pasted the OP's TeX code and didn't think of trying to type it until @ChrisK commented.

$\endgroup$
7
  • $\begingroup$ Wow! How do you enter multiple lines as in your first example? When I hit ctrl-4, then \begin {aligned}, the first return key sends it to evaluate, resulting in $Failed. $\endgroup$
    – Chris K
    Commented Apr 4, 2022 at 1:40
  • 1
    $\begingroup$ @ChrisK I pasted the OP's input. I didn't realized entering it would be a problem. :) However, you can type \[NewLine] and it works. -- Just had a thought and [thought] it worked too: Shift-return enters a newline. NOPE. Shift-return enters \[LineSeparator] and fails. Have to enter \[NewLine] I guess. $\endgroup$
    – Michael E2
    Commented Apr 4, 2022 at 2:49
  • $\begingroup$ This probably isn't the place to ask, but is there some way to just get a LaTeX cell, hit shift-return, and it gets typeset? Something like and ExternalLanguage cell. $\endgroup$
    – Chris K
    Commented Apr 4, 2022 at 20:15
  • 1
    $\begingroup$ @ChrisK Something like this?: CellPrint[TextCell["edit me", "Text", Evaluatable -> True, CellEvaluationFunction -> (NotebookWrite[EvaluationCell[], Cell[BoxData[MakeBoxes[#, StandardForm] &@MaTeX[#]], "Output"]] &)]]. The Cell options could be set in stylesheet. The style of the output need not be "Output". For text, you might want an inline cell: CellPrint[TextCell["edit me", "Text", Evaluatable -> True, CellEvaluationFunction -> (NotebookWrite[EvaluationCell[], Cell[TextData[Cell[BoxData[MakeBoxes[#, StandardForm] &@MaTeX[#]], "InlineFormula"]], "Text"]] &)]] $\endgroup$
    – Michael E2
    Commented Apr 4, 2022 at 20:52
  • $\begingroup$ That's pretty good, but not quite what I had in mind. I'll ask a proper question some time when the need becomes pressing. thanks! $\endgroup$
    – Chris K
    Commented Apr 4, 2022 at 21:27
2
$\begingroup$

Inline $\TeX$ Input works well:

enter image description here

I don't know if $\LaTeX$ environments can be used for multiline equations in a Notebook, but a somewhat improvised solution is to use spaces in $\LaTeX$ math mode.

$\endgroup$
2
  • $\begingroup$ I would hardly call that working well. It requires manually spacing all of the elements to get them aligned $\endgroup$
    – Joff
    Commented Feb 17, 2022 at 2:21
  • 1
    $\begingroup$ That's why I mentioned that it's kind of improvised. MaTeX is a better solution because you can even load packages with the preamble option. $\endgroup$ Commented Feb 18, 2022 at 4:50

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.