MathematicaCellsToTeX icon indicating copy to clipboard operation
MathematicaCellsToTeX copied to clipboard

No wrapping lines

Open paubanon opened this issue 1 year ago • 1 comments

After performing an export. The Latex file doesn't wrap the lines, nor present some output correctly. Screenshot 2024-09-20 at 14 03 00 Screenshot 2024-09-20 at 14 02 43

paubanon avatar Sep 20 '24 12:09 paubanon

Sorry for late reply.

For automatic line breaking one can try using breaklines option from listings package, see https://github.com/jkuczm/mmacells/issues/22. But this will not work in the same way as automatic line breaking in Mathematica FrontEnd. Unfortunately, currently to get nicely formatted code one needs to manually format it either in Mathematica, or in exported TeX code.

As to matrix form. Proper general conversion of Mathematica GridBox requires tricky combination of verbatim and tabular-like environments on the TeX side, which is not yet implemented in mmacells. For cases like above you could add something like following configuration:

PrependTo[$ContextPath, "CellsToTeX`Configuration`"];

SetOptions[mmaCellProcessor, "Indentation" -> ""];

$boxesToFormattedTeX = Join[{
  RowBox[{l:"("|"[", "\[NoBreak]", gridBox_GridBox, "\[NoBreak]", r:")"|"]"}] :>
    $commandCharsToTeX[[1, 1]] <> "(" <> $commandCharsToTeX[[1, 1]] <> "left" <> l <>  makeString@gridBox <> $commandCharsToTeX[[1, 1]] <> "right" <> r <> $commandCharsToTeX[[1, 1]] <> ")",
  GridBox[grid:{___List}, ___] :>
    $commandCharsToTeX[[1, 1]] <> "mmaGrid" <> $commandCharsToTeX[[2, 1]] <> StringRepeat["c", Max[Length /@ grid]] <> $commandCharsToTeX[[3, 1]]  <> $commandCharsToTeX[[2, 1]] <> "\n" <>
      StringJoin@Riffle[Riffle[makeString /@ #, "\&"]& /@ grid, $commandCharsToTeX[[1, 1]] <> "\\\n"] <> "\n" <> $commandCharsToTeX[[3, 1]]
 }, $boxesToFormattedTeX];

After adding above code to Mathematica, presented output cell will be converted to following TeX code:

\begin{mmaCell}[form=MatrixForm]{Output}
\(\left(\mmaGrid{cccc}{
\mmaSqrt{1-\mmaFrac{2 G M}{r}}\&0\&0\&0\\
0\&\mmaSqrt{\mmaFrac{1}{1-\mmaFrac{2 G M}{r}}}\&0\&0\\
0\&0\&r\&0\\
0\&0\&0\&r Sin[\(\theta\)]
}\right)\)
\end{mmaCell}

With following definition added to the TeX file:

\newcommand{\mmaGrid}[2]{{%
  \renewcommand{\&}{&}%
  \begin{tabular}{#1}%
    #2%
  \end{tabular}%
}}

\mmaSet{morelst={morefvcmdparams=\mmaGrid 1}}

It'll compile to pdf looking like this: pdf with MatrixForm output

jkuczm avatar Oct 10 '24 19:10 jkuczm