Display A MATLAB Numerical Matrix in the Form of LaTeX Matrix

Feb. 09, 2025 • Updated Aug. 04, 2025

To display a MATLAB numerical matrix in the form of LaTeX matrix, we can create such a self-defined MATLAB function Matrix2LaTeX (which is modified on the basis of the code provided by reference1):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
function result = Matrix2LaTeX(matrix, precision)
% Set precision of numbers
if nargin == 1
    precision = '4';
else
    precision = int2str(precision);
end

% Define the output form of each element in the matrix
outForm4NegativeNumber = ['%0.', precision, 'f & '];
if any(matrix<=0, "all")
    placeHolder = '\\phantom{-}';
    outForm4PositiveNumber = [placeHolder, '%0.', precision, 'f & '];
end

% Generate the LaTeX code of the matrix
[nRow, nColumn] = size(matrix);
out = char(sprintf("\\begin{bmatrix}"));
for i = 1:nRow
    out = [out, char(sprintf('\n\t'))]; %#ok % make a newline
    for j = 1:nColumn
        element = matrix(i, j);
        if element<0
            temp = sprintf(outForm4NegativeNumber, element);
        else
            temp = sprintf(outForm4PositiveNumber, element);
        end
        out = [out, temp]; %#ok
    end

    % Delete the last `&`
    out = out(1:end-3);

    % Add the symbol `\\` at the end of the line
    out = [out, '\\']; %#ok
end
out = [out, char(sprintf("\n\\end{bmatrix}\n"))];

result = out;
disp(result)
end

and we can test it using a simple example:

1
2
3
4
5
6
clc, clear, close all

rng("default")

A = randn(5, 4)
tm1 = Matrix2LaTeX(A, 4);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
A =
    0.5377   -1.3077   -1.3499   -0.2050
    1.8339   -0.4336    3.0349   -0.1241
   -2.2588    0.3426    0.7254    1.4897
    0.8622    3.5784   -0.0631    1.4090
    0.3188    2.7694    0.7147    1.4172

\begin{bmatrix}
	\phantom{-}0.5377 & -1.3077 & -1.3499 & -0.2050\\
	\phantom{-}1.8339 & -0.4336 & \phantom{-}3.0349 & -0.1241\\
	-2.2588 & \phantom{-}0.3426 & \phantom{-}0.7254 & \phantom{-}1.4897\\
	\phantom{-}0.8622 & \phantom{-}3.5784 & -0.0631 & \phantom{-}1.4090\\
	\phantom{-}0.3188 & \phantom{-}2.7694 & \phantom{-}0.7147 & \phantom{-}1.4172\\
\end{bmatrix}

Next, let’s copy the LaTeX code into a LaTeX document, then we’ll have:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
\documentclass{article}
\usepackage{amsmath}

\begin{document}

\begin{equation}
\begin{bmatrix}
	\phantom{-}0.5377 & -1.3077 & -1.3499 & -0.2050\\
	\phantom{-}1.8339 & -0.4336 & \phantom{-}3.0349 & -0.1241\\
	-2.2588 & \phantom{-}0.3426 & \phantom{-}0.7254 & \phantom{-}1.4897\\
	\phantom{-}0.8622 & \phantom{-}3.5784 & -0.0631 & \phantom{-}1.4090\\
	\phantom{-}0.3188 & \phantom{-}2.7694 & \phantom{-}0.7147 & \phantom{-}1.4172\\
\end{bmatrix}
\end{equation}

\end{document}

image-20250804114539988


References

  1. MATLAB N个实用技巧——MATLAB中文论坛精华总结 (第2版), 刘焕进, 李鹏, 王辉, 王海洋 编著, pp. 235-237. ˄