File Exchange

image thumbnail

latex

version 1.0.0.0 (2.48 KB) by

Output a matrix in LaTeX format.

6 Downloads

Updated

View License

This file will output a MATLAB numeric matrix in a format suitable to paste into a LaTeX tabular environment. You can choose whether to individually cast each element in math mode (you wouldn't want to in the AMS bmatrix environment, for example). You can also choose a format specifier independently for each column of the matrix.

I find this useful to convert true matrices, convergence tables, and the like into a document-ready form. It's independent of the Symbolic Toolbox.

Examples:>> latex(toeplitz(1:4))
$1.0000$ & $2.0000$ & $3.0000$ & $4.0000$ \\
$2.0000$ & $1.0000$ & $2.0000$ & $3.0000$ \\
$3.0000$ & $2.0000$ & $1.0000$ & $2.0000$ \\
$4.0000$ & $3.0000$ & $2.0000$ & $1.0000$ \\

>> latex(toeplitz(1:4),'%i','nomath')
1 & 2 & 3 & 4 \\
2 & 1 & 2 & 3 \\
3 & 2 & 1 & 2 \\
4 & 3 & 2 & 1

>> latex(toeplitz(1:4),'%i','%.2f')
$1$ & $2.00$ & $3.00$ & $4.00$ \\
$2$ & $1.00$ & $2.00$ & $3.00$ \\
$3$ & $2.00$ & $1.00$ & $2.00$ \\
$4$ & $3.00$ & $2.00$ & $1.00$

Comments and Ratings (17)

seung lee

very nice

Matthias

Very simple and yet extremely helpful. I suggest using it together with fprintf as you can see in the example below. That way, a raw LaTeX table is created that can easily be used to design LaTeX tables in your desired style and format.

fileID = fopen('testtab.tex', 'w');
fprintf(fileID, '%s', latex(data,'%.2f'));
fclose(fileID);

Dan Golding

This is really useful, thanks!

Oskar Vivero

Oscar

Oscar (view profile)

I use it several times a week, brilliant piece of code. Thank you.

Thnks, handy

Jas Ellis

Great, also deals with NaNs!

Simone Scaringi

WOW!!!! Thanks!

Paul Bendixen

Super tool!
This is really keeping it simple, thank you very much

Craig Robinson

Worked perfectly for what I needed. I like the option of 'nomath' option.

Wolfgang Schwanghart

A very helpful tool. Thanks a lot.

Javad Farzi

great.
I dont know if this code works with ratinal numbers or nut? for example, maybe somone want to generate a table with ratinal numbers to a book or other texts.

Yves Blattmann

Nice, simple and effective.
Thanks to programmer...

c.l. fung

simple and intuitive. good work.

Norbert Marwan

Works well - very nice programmed. Thanks a lot.

MATLAB Release Compatibility
Created with R13
Compatible with any release
Platform Compatibility
Windows macOS Linux
Acknowledgements

Inspired: LaTeX table element wrapper

MATLAB Online Live Editor Challenge

Win cash prizes and have your live script featured on our website

Learn more

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video