1
$\begingroup$

I want the simplest way of exporting matrices, and vector from mathematica to Latex.

Usually, I display my matrices in MatrixForm and copy as Latex to paste in my latex document. However it seems it doesn't always work, take the following code:

vv = {1, 2};
vv // MatrixForm

If from the display you do "copy as -> Latex", it returns you:

\{1,2\}

Instead of (for example):

\begin{pmatrix} 1 \\ 2 \end{pmatrix}

Which would match the mathematica display. If I input a matrix however (thus having more than 1 column), it works as it should.

How to make it really returns me something that will look like a column vector in latex ?

$\endgroup$
  • $\begingroup$ In version 12, I only get array, not pmatrix. $\endgroup$ – Henrik Schumacher Jan 16 at 13:10
  • $\begingroup$ @HenrikSchumacher I don't necesserally want a pmatrix and indeed it is not what it returns me for a "real" matrix (not a column vector). But I want a proper array to represent the column vector which he doesn't give me. $\endgroup$ – StarBucK Jan 16 at 13:45
3
$\begingroup$

What about

export[v_?VectorQ] := export@Partition[vv, 1];
export[A_?MatrixQ] := CopyToClipboard@TeXForm@A;

and then

vv // export

?

A way to enforce pmatrix could be

export[v_?VectorQ] := export@Partition[vv, 1];
export[A_?MatrixQ] := CopyToClipboard@StringJoin[
    "\\begin{pmatrix}\n",
    ExportString[
     Join[
      Map[TeXForm, A, {2}],
      Append[ConstantArray["\\\\", {Length[A] - 1, 1}], {""}],
      2],
     "Table",
     "FieldSeparators" -> " & "
     ],
    "\n\\end{pmatrix}"
    ];

Btw., MatrixForm is super superfluous here.

| improve this answer | |
$\endgroup$
  • $\begingroup$ Thank you for your answer. So actually the "real" answer inside is that I have to do Partition[vv, 1] to tell mathematica to write it as a matrix (even if it is a column vector). So that when I export in latex he knows he has to write a column vector using array. Would you confirm ? $\endgroup$ – StarBucK Jan 16 at 13:47
  • 2
    $\begingroup$ Mathematica just does not distinguish between "column vectors" and "row vectors". But you can transform an $n$-vector v into a $n \times 1$-matrix with Partition[v,1]. Btw., I strongly believe that Mathematica is a "she". But who knows...? $\endgroup$ – Henrik Schumacher Jan 16 at 13:52
  • $\begingroup$ Ok thanks. Well actually I'm french so I sometime replace it by he or she as we don't have impersonal pronouns ^^ $\endgroup$ – StarBucK Jan 16 at 14:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.