How to force TeXForm not to use "array" environment for a square array?
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.
Another possibility which seems to work is
lis={{1,2},{3,4}}
TeXForm[InputForm@lis]
Which renders as
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
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
while when we use TeXForm
, we get the desired non-MatrixForm
output:
{{1, 2}, {3, 4}} //TeXForm
$\{\{1,2\},\{3,4\}\}$