A simple yet flexible way to access Matlab results from LaTeX documents. Includes an example file.
Updated 24 Jul 2006

View License

USAGE: latexcmd(filename, var1, var2, ...)

Creates a LaTeX file with commands named as the corresponding Matlab variables. Include the resulting LaTeX file into your LaTeX document (by an \include{filename}) to access the variable values. Works on scalars, polynomials, numeric arrays, cell arrays, and structures. Also works with most types: numbers (real and complex), NaN, Inf, strings, syms (from the Symbolic Toolbox), function handles, and inlines (although functions look flat without the Symbolic TB).

If you find any errors, please let me know (peder at axensten dot se)!

INSPIRATION: latexcmd has the functionality of the similar m-files latex, matrix2latex, MAT2TEX, latex_trick, mat2textable, but works on more variable and number types, and has a more flexible number format, when required. It can also save as many variables as you want in one file, as opposed to one file per variable in some older related m-files.

filename must be a string. If prefixed by '>' (i.e. '>filename'), the results will be appended to previous file contents. If empty, results will be printed on the display.
var may be any of the following:
+ Arrays must be max 2-dimensional.
+ Cell arrays may not be nested (contain structures or arrays).
+ Structures generates a header with field names, followed by hline, followed by actual values.
+ Character strings:
- '\myname': The name of the next expression.
- '@myprefix': The prefix of the following names.
- '%myformat': Defines a new active number format (e.g. '%12.5E'). Specials: '%r': fractions format of numbers (using rat). '%R': fractions format of numbers (using rat) with integer part. '%ns': power of ten suffix (e.g., k, M, etc.) (n is number of digits). '%nS': 'scientific notation', exponent multiple of 3. '%u & %g & %5.2f%%' formats array columns individually.
- '%stat': Statistics of the following array (all of it, not column-wise): length, min, max, range, mean, median, std, cov, and all of it collectively (see latexcmd_example.m). If the next expression is non-numeric, '%stat' will be ignored.
- '%poly': Outputs the following array as a polynomial of 'x'.
- '%poly:myvar': Outputs the following array as a polynomial of myvar.
- Other strings will be output as they are.

EXAMPLE (See 'help latexcmd_example' for more detailed examples.):
--- In Matlab m-file ---------------------------------------------------
result= [1 2 3 4 5 6];
resultmean= mean(result);
latexcmd(results, result, resultmean);
--- In your LaTeX file -------------------------------------------------
[and somewhere later]
The mean of the measurements is \resultmean, with individual results:
\begin{tabular}{r@{, }r@{, }r@{, }r@{, }r@{, and }r}\result\end{tabular}

The following LaTeX commands are defined (you may redefine them for other formatting): \integer{int}, \float{int}{decimal}, \integerE{int}{exp}, \floatE{int}{decimal}{exp}, \fraction{nom}{denom}, \numberdelimiter, \imsymb, \textmu, \NaN, \statAll{}{}{}{}{}{}{}{}.

Version 1.0, 2006-03-20.
Version 1.1, 2006-03-29:
- Added the '%stat' option to output various statistical info on an array.
- Added '%poly'/'%poly:name' options to output an array as a polynomial.
- Individual column formatting didn't actually work (sorry!), now it does.
- Fixed a crash when filename was an empty string.
- Removed stuff that mlint complained about.
Version 1.2, 2006-04-27:
- Better (?) help text (no code changes).
Version 1.3, 2006-05-07:
- Prefix (with \) certain characters in char variables: $&%#_{}~^.
Version 1.4, 2006-05-23:
- Fixed a bug appearing when latexcmd was called from a command window.
Version 1.5, 2006-06-06:
- Fixed bugs in latexcmd_example.m.
- Objects of type inline are now treated the same as function_handle objects.
Version 1.6, 2006-06-07:
- Additional type of formatting for real scalars using power of ten suffix, (M is 1e6 etc.). See '%^n', above.
- Fixed a pretty rare bug with %-prefixed strings.
Version 1.7, 2006-06-14:
- Fixed bug with one-dim. structures (thanks Ted Rosenbaum for reporting it).
Version 1.8, 2006-07-20:
- Added support for Fixed-Point Toolbox fi objects (class
- '%r' and '%R': fractions format of numbers, using rat().
- '%ns': scientific notation (exponent multiple of 3) with suffix.
- '%nS': scientific notation (exponent multiple of 3).

COPYRIGHT (C) Peder Axensten (peder at axensten dot se), 2006.

Cite As

Peder Axensten (2024). latexcmd (, MATLAB Central File Exchange. Retrieved .

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

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
Version Published Release Notes

- Added support for Fixed-Point Toolbox fi objects (class
- '%r' and '%R': fractions format of numbers, using rat().
- '%ns': scientific notation with suffix.
- '%nS': scientific notation.