4

Perhaps I am doing something wrong, but to me, writing Matrices and tables in LaTeX code is rather onerous. Would it be possible to produce an n x n matrix using a format such as Python or Matlab:

A = [1 2 3; 4 5 6; 7 8 9] 

Which should produce a square matrix with elements 1-9.

I understand tables and some matrix options would need to be specified in order for this to work, but that seems doable.

5
  • 6
    you could of course write a parser for that but if you replace space by & and ; by \\ you have the latex syntax so one extra character per row, is hardly "onerous" is it? \begin{pmatrix}1&2&3\\4&5&6\\7&8&9\end{pmatrix} Commented Jul 5, 2021 at 20:49
  • You make a compelling argument Commented Jul 5, 2021 at 21:02
  • Would PythonTeX be a reasonable choice? Commented Jul 5, 2021 at 21:10
  • tex.stackexchange.com/questions/302213/… Commented Jul 5, 2021 at 21:37
  • 1
    You can create a script in MATLAB that from a matrix create the LaTeX syntax for the table and then save the output as a text file with extension .tex and then use \input{<file>.tex} to import in LaTeX. I have made very long tables in this way with MATHEMATICA. Commented Jul 5, 2021 at 21:49

3 Answers 3

10

nicematrix is so cute...

\documentclass[12pt]{article}

\usepackage{nicematrix}

\begin{document}

$\begin{bNiceMatrix}[light-syntax]
    1 2 3; 4 5 6; 7 8 9
\end{bNiceMatrix}$

\end{document}

If you want, you can even create a dedicated macro.

\documentclass[12pt]{article}

\usepackage{nicematrix}

\newcommand\matlabmat[1]{%
    \begin{bNiceMatrix}[light-syntax]
        #1
    \end{bNiceMatrix}%
}

\begin{document}

$\matlabmat{1 2 3; 4 5 6; 7 8 9}$

$\matlabmat{1 ; 2 ; 3 ; 4 ; 5 ; 6}$

\end{document}

This gives:

enter image description here

1

Here's the link to proposed package nicematrix:

https://ctan.org/pkg/nicematrix?lang=en

1
  • It is in TeX Live. Commented Jul 6, 2021 at 18:06
1

If you are an AUCTeX/Emacs user, just active the embedded calc mode the point (cursor) on this expression (C-x * e)

\[A = [1 2 3; 4 5 6; 7 8 9]\] 

instantanly the expression is converted in

\[A = \begin{pmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9 \end{pmatrix} \]

This operation is reversible: still in calc embedded mode the command d N does it.

Unfortunately it do not seem possible to chose the matrix environment (bmatrix for example), you have to change it `manually'.

To do so you can configure the LaTeX langage mode 2 to display the output in two dimensional mode. C-u 2 d T do the trick. It can be

% [calc-mode: language: (latex 2)]
\[
A = \begin{pmatrix}
    1 & 2 & 3 \\ 
    4 & 5 & 6 \\ 
    7 & 8 & 9
    \end{pmatrix}
\] 

Don't forget to return back to latex mode (C-x * e).

To change the pmatrix into bmatrix (or whatever matrix environment you want) Just C-u C-c C-e as usual. It may be easier to do a simple find and replace.

Note that calc can perform a large number of matrix operations for numeric or symbolic values, the ability to compose in LaTeX syntax is a very basic although excellent functionality.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.