How to force TeXForm not to use "array" environment for a square array?

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}}]?

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

• Hmmm... The output of TeXForm is not easy to predict. Jul 26 '18 at 13:20

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 @ LanguageExtendedDefinition[sym]
],
protect=Unprotect[sym]
},

sym;

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[ConvertTeXExpressionToTeX] /:
ConvertTeXExpressionToTeX[e__] /; !TrueQ@$TeX := Block[ {$TeX = True},
ConvertTeXExpressionToTeX[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\}\}$