jkuczm / MathematicaCellsToTeX

Convert Mathematica cells to TeX, retaining formatting.
Other
57 stars 10 forks source link

Problems with integers inside boxes #29

Open vsht opened 4 years ago

vsht commented 4 years ago

The following code cannot be processed by CellToTeX

CellToTeX[
 Cell[BoxData[
   FormBox[FractionBox["1", 
     TemplateBox[{"\"(\"", 
       SuperscriptBox[
        FormBox[FormBox[StyleBox["k", Bold, StripOnInput -> False], 
          TraditionalForm], TraditionalForm], 2], "\"-\"", 
       RowBox[{"\[ImaginaryI]", " ", "\"\[Eta]\""}], "\")\""}, "Row", 
      DisplayFunction -> (RowBox[{#, "\[InvisibleSpace]", #2, 
           "\[InvisibleSpace]", #3, "\[InvisibleSpace]", #4, 
           "\[InvisibleSpace]", #5}] &), 
      InterpretationFunction -> (RowBox[{"Row", "[", 
           RowBox[{"{", 
             RowBox[{#, ",", #2, ",", #3, ",", #4, ",", #5}], "}"}], 
           "]"}] &)]], TraditionalForm]], "Output"]]

yet if I change 2 in the SuperscriptBox to "2" it works fine

CellToTeX[
 Cell[BoxData[
   FormBox[FractionBox["1", 
     TemplateBox[{"\"(\"", 
       SuperscriptBox[
        FormBox[FormBox[StyleBox["k", Bold, StripOnInput -> False], 
          TraditionalForm], TraditionalForm], "2"], "\"-\"", 
       RowBox[{"\[ImaginaryI]", " ", "\"\[Eta]\""}], "\")\""}, "Row", 
      DisplayFunction -> (RowBox[{#, "\[InvisibleSpace]", #2, 
           "\[InvisibleSpace]", #3, "\[InvisibleSpace]", #4, 
           "\[InvisibleSpace]", #5}] &), 
      InterpretationFunction -> (RowBox[{"Row", "[", 
           RowBox[{"{", 
             RowBox[{#, ",", #2, ",", #3, ",", #4, ",", #5}], "}"}], 
           "]"}] &)]], TraditionalForm]], "Output"]]

producing

\begin{mmaCell}{Output}
  \mmaFrac{1}{(\mmaSup{k}{2}-i \(\eta\))}
\end{mmaCell}

The bold styling of k is lost, but this is a lesser issue.

The input is a produced by some custom typesetting rules used in FeynCalc so this is probably a not very common issue. Still, I'm wondering if there are any fundamental reasons why every non-box inside a box must be a string for CellToTeX to work properly?

jkuczm commented 4 years ago

As far as I remember I considered boxes produced by MakeBoxes to be the only valid ones, and MakeBoxes gives boxes with strings. But e.g. MakeExpression is more lenient and accepts boxes with "raw" symbols or numbers, so maybe current behavior of CellToTeX is to strict.

Until this is changed in package, a workaround is to add relevant pattern to $basicBoxes:

PrependTo[$ContextPath, "CellsToTeX`Configuration`"];
AppendTo[$basicBoxes, _Integer]

As to StyleBox, handling it in full generality is difficult on the TeX side. By default StyleBoxes are simply ignored. For simple case, as above, you could add additional rule to handle it, e.g. with \textbf, on the TeX side:

PrependTo[$linearBoxesToTeX, StyleBox[b_, Bold, ___] :> "\\textbf{" <> makeString@b <> "}"]

After above two changes CellToTeX on original cell should give:

\begin{mmaCell}{Output}
  \mmaFrac{1}{(\mmaSup{\textbf{k}}{2}-i \(\eta\))}
\end{mmaCell}
vsht commented 4 years ago

Thanks, this workaround works nicely!

vsht commented 4 years ago

Just as a reminder for my future self. This

PrependTo[$linearBoxesToTeX, 
 StyleBox[b_, ___, FontWeight -> Bold, ___] :> 
  "\\textbf{" <> makeString@b <> "}"]

is also needed to get some bold symbols converted properly.