7
$\begingroup$

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.

$\endgroup$

1 Answer 1

5
$\begingroup$

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.

$\endgroup$
7
  • $\begingroup$ +1 for working to answer your own question, which BTW is a good one. $\endgroup$
    – Mr.Wizard
    Commented Jun 9, 2012 at 7:17
  • $\begingroup$ @Mr.Wizard, thank you. Now, if I could just manage to do the last part to complete it. $\endgroup$
    – jmlopez
    Commented Jun 9, 2012 at 15:28
  • $\begingroup$ Regarding that (your last line) by chance is "{" ~~ ("c" ..) ~~ "}" -> "" all you're missing? $\endgroup$
    – Mr.Wizard
    Commented Jun 9, 2012 at 15:34
  • $\begingroup$ @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... $\endgroup$
    – jmlopez
    Commented Jun 9, 2012 at 15:40
  • $\begingroup$ 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. $\endgroup$
    – Mr.Wizard
    Commented Jun 9, 2012 at 15:41

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.