8
$\begingroup$

The default behavior of Mathematica,

transforms a jagged array

TeXForm[{{1, 2}, {3}}]

to

\{\{1,2\},\{3\}\}

but

a square array

TeXForm[{{1, 2}, {3, 4}}]

to

 \left(
\begin{array}{cc}
 1 & 2 \\
 3 & 4 \\
\end{array}
\right)

Question

Is it possible to produce

\{\{1,2\},\{3,4\}\}

for TeXForm[{{1, 2}, {3, 4}}]?

$\endgroup$
0

3 Answers 3

7
$\begingroup$

Try

TeXForm[HoldForm /@ {{1, 2}, {3, 4}}]

\{\{1,2\},\{3,4\}\}

This basically prevents TeXForm from recognizing the expression as a matrix.

Using HoldForm to prevent or force certain interpretations is also mentioned in the MaTeX documentation.

$\endgroup$
6
$\begingroup$

Another possibility which seems to work is

lis={{1,2},{3,4}}
TeXForm[InputForm@lis]

Mathematica graphics

Which renders as

Mathematica graphics

$\endgroup$
1
  • 1
    $\begingroup$ Hmmm... The output of TeXForm is not easy to predict. $\endgroup$
    – D G
    Commented Jul 26, 2018 at 13:20
3
$\begingroup$

Briefly, TeXForm works by formatting the input using TraditionalForm, and then converting that format to TeXForm. Specifically, it creates TraditionalForm boxes from the input, and then converts those boxes to TeXForm. TraditionalForm uses MatrixForm to format matrices, and the TeXForm for MatrixForm boxes uses the array environment. So, if you want to avoid the array environment, you will need to either avoid MatrixForm, or modify the way TeXForm operates on MatrixForm boxes.

The other answers avoided MatrixForm by inserting a wrapper into the expression such that TraditionalForm no longer tries to use MatrixForm. However, this means you need to modify your input. I will present an alternate approach where you don't need to modify your input. It will be convenient to make use of my Initial function, which I reproduce below:

Initial /: Verbatim[TagSetDelayed][Initial[sym_], lhs_, rhs_] := With[
    {
    new=Block[{sym},
        TagSetDelayed[sym,lhs,rhs];
        First @ Language`ExtendedDefinition[sym]
    ],
    protect=Unprotect[sym]
    },

    sym;
    Quiet@MakeBoxes[sym[],TraditionalForm];

    Unprotect[sym];
    Replace[
        new,
        Rule[values_,n:Except[{}]] :> (
            values[sym] = DeleteDuplicates @ Join[n, values[sym]]
        ),
        {2}
    ];
    Protect@protect;
]

The Initial wrapper is used to force the new definition to be used before the old definitions. In our case, we want to add the following FormatValues for TraditionalForm:

Initial[TraditionalForm] /: 
    MakeBoxes[x_List /; TrueQ@$TeX, TraditionalForm] := MakeBoxes[x, StandardForm]

Now, TraditionalForm formatting of matrices will depend on the value of $TeX:

$TeX = False;
{{1, 2}, {3, 4}} //TraditionalForm

$TeX = True;
{{1, 2}, {3, 4}} //TraditionalForm

enter image description here

This means that TeXForm will be similarly affected:

$TeX = False;
{{1, 2}, {3, 4}} //TeXForm

$\left( \begin{array}{cc} 1 & 2 \\ 3 & 4 \\ \end{array} \right)$

$TeX = True;
{{1, 2}, {3, 4}} //TeXForm

$\{\{1,2\},\{3,4\}\}$

Finally, it would be convenient if TeXForm automatically sets $TeX to True, which we can achieve by again using the Initial wrapper:

$TeX =.;
Initial[Convert`TeX`ExpressionToTeX] /: 
    Convert`TeX`ExpressionToTeX[e__] /; !TrueQ@$TeX := Block[
        {$TeX = True},
        Convert`TeX`ExpressionToTeX[e]
    ]

Now, when we use TraditionalForm, we get the usual MatrixForm output:

{{1, 2}, {3, 4}} //TraditionalForm

enter image description here

while when we use TeXForm, we get the desired non-MatrixForm output:

{{1, 2}, {3, 4}} //TeXForm

$\{\{1,2\},\{3,4\}\}$

$\endgroup$

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.