7

This question is mainly about exporting to HTML/TeX. For those of you who do not know how to write aligned equations in Mathematica please see this answer. Lets assume that you modified your stylesheet as I mentioned in my answer. In my notebook this is the cell expression I have:

Cell[BoxData[
    FormBox[GridBox[{
    {
    RowBox[{
    RowBox[{"a", "+", "b", "+", "c"}], "\[AlignmentMarker]", "=", "d"}]},
    {
    RowBox[{"c", "\[AlignmentMarker]", "=", 
    RowBox[{"d", "-", "a", "-", "b"}]}]}
}], TraditionalForm]], "DisplayMath"]

Now I want to export this expression to TeX, we can do this with:

cell = Cell[BoxData[
        FormBox[GridBox[{
        {
        RowBox[{
        RowBox[{"a", "+", "b", "+", "c"}], "\[AlignmentMarker]", "=", "d"}]},
        {
        RowBox[{"c", "\[AlignmentMarker]", "=", 
        RowBox[{"d", "-", "a", "-", "b"}]}]}
    }], TraditionalForm]], "DisplayMath"];
Convert`TeX`BoxesToTeX[cell]

The output is:

\begin{array}{c}
 a+b+c=d \\
 c=d-a-b
\end{array}

The function Convert`TeX`BoxesToTeX is undocumented but the documentation does show how to use it under examples. It seems that the function doesn't do anything with the \[AligmentMarker], but I'm sure we could make good use of them.

What we want to do is find any appearances of the \[AligmentMarker] and substitute them with "&". And if we did find some \[AligmentMaker]s then we use StringReplace to change "array" for "aligned". This is the output that I would like to end up with:

\begin{aligned}
 a+b+c&=d \\
 c&=d-a-b
\end{aligned}

Does anyone know how to create a wrapper function that does this? The main problem I'm having is replacing things in cell.

jmlopez
  • 6,470
  • 4
  • 28
  • 48

1 Answers1

5

Using the definition of cell as in the question we can define:

CellToTeX[cell_] := Module[{str},
  str = Convert`TeX`BoxesToTeX@Replace[cell, {"\[AlignmentMarker]" -> "&"}, {-1}];
  StringReplace[str, {"\\&" -> "&", "array" -> "aligned"}]
]

Then when we use it we obtain:

CellToTeX[cell]
\begin{aligned}{c}
 a+b+c&=d \\
 c&=d-a-b
\end{aligned}

I'm still not so sure of how to define a rule to replace {c} or possibly many {cccc} for an empty string.

jmlopez
  • 6,470
  • 4
  • 28
  • 48
  • +1 for working to answer your own question, which BTW is a good one. – Mr.Wizard Jun 09 '12 at 07:17
  • @Mr.Wizard, thank you. Now, if I could just manage to do the last part to complete it. – jmlopez Jun 09 '12 at 15:28
  • Regarding that (your last line) by chance is "{" ~~ ("c" ..) ~~ "}" -> "" all you're missing? – Mr.Wizard Jun 09 '12 at 15:34
  • @Mr.Wizard, you just answered the question. Could you write it as an answer and maybe explain a little about how you came up with it? I saw those type of rules but I would have never thought about using a rule like that... – jmlopez Jun 09 '12 at 15:40
  • You know it's funny- you're doing things with LaTeX I don't comprehend; it didn't occur to me that a simple replacement like this was holding you up. – Mr.Wizard Jun 09 '12 at 15:41
  • I have to leave. I'd be happy to write an introduction to Mathematica string replacements, but not right now. Sorry I didn't give you a better answer, sooner! – Mr.Wizard Jun 09 '12 at 15:44
  • @Mr.Wizard, no problem, I can wait. I'll probably need to add more string replacements rules in the future and I'll be referring to this question. – jmlopez Jun 09 '12 at 15:54