File Exchange

image thumbnail


version (10 KB) by Kesh Ikuma
LaTeX image object for using LaTeX packges not supported by the builtin LaTeX text interpreter

1 Download

Updated 03 Nov 2015

View License

LaTeX support provided by the MATLAB Graphics engine is limited at a time, especially to add complex equations on a figure. lateximage creates an image object which behaves like a text object with full LaTeX support.
To create a LaTeX equation, simply call lateximage like 'text' function call, e.g.,

h = lateximage(0.5,0.5,'\boldsymbol{\xi} + \boldsymbol{\zeta}') % built-in interpreter cannot handle \boldsymbol

Included lateximage_demo.m illustrates a more elaborate usage.

Main Features:
* Supports many of the standard Text Properties: Position, String, FontSize, FontSmoothing, HorizontalAlignment, VerticalAlignment, Rotation, Color, BackgroundColor, EdgeColor, and LineWidth
* By default, LaTeX string is assumed to be in the equation mode. To create a regular LaTeX text, set EquationOnly option to false
* Auto-scales the image object to maintain the FontSize via actively listening to the axes size and limit PostSet events
* Exposing full LaTeX support with "standalone" document class: fully customizable document class options, LaTeX packages, document preamble, and document body text
* Print-ready. Text will appear correctly both on screen and on paper (or image). Printed text resolution can be increased via OverSamplingFactor option.

Usage Notes:
* Supports both HG2 (r2014b and later) and pre-HG2 Matlab versions
* Right before print/export the figure with lateximage objects, run 'lateximage -printmode on' so that the lateximage objects are properly scaled during print operation.
* This function should only be used if the built-in LaTeX interpreter *cannot* interpret the desired LaTeX string. The built-in interpreter (with a text object) results in a better image output than the image-based solutions, including this submission.
* In R2014b and later, experiment with OverSamplingRate setting for better output, especially for EPS output. >=20 seems to produce better, acceptable results.
* For onscreen usage, keep OverSamplingRate = 1 for the best appearance

* latex and dvipng executable must be available via system path. For Windows systems, simply installing MikTeX would take care of it

* Margin option is not functional yet. dvipng squashes the specified margin in LaTeX file. If anyone knows how to work around this, shoot me a message. The goal of this function is keep external dependency to those in LaTeX distribution only
* Apply OverSamplingRate only when -printmode is on
* <strike>Further investigate a way to listen to the events during print to make scaling more reliable</strike> It appears to be working with a mysterious scaling factor of 2

Cite As

Kesh Ikuma (2020). lateximage (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (4)

Antonio Demarcus

I have problems with matlab R2018 and latex on a mac. I have the following errors, I think due to a configuration issue.

Error using lateximage>renderlatex (line 751)
latex -quiet -aux-directory="/private/var/folders/r7/4p1xw_kx6wl6qpgzxswd_5v40000gn/T"

zsh:1: command not found: latex

Generated .tex file:

Error in lateximage>newobject (line 168)
[A,ALPHA,dims] = renderlatex(opts);

Error in lateximage (line 148)
h = newobject(opts);

Error in lateximage_demo (line 41)


Nice updates Kesh. lateximage() now runs without warnings being generated.

Kesh Ikuma

@Mathew - Glad it worked straight out of the box for you :) Could you post/email me the warning you got? Also, which "undocumented feature" are you referring to? This file has a load of them ;) Examples will come near future.


I like where this is going :-)

Some comments
- This function worked for me without modification. However, it generated warnings...
- You should use verLessThan rather than the undocumented feature function
- Where are the examples to show off your function? Suggest examples where your functions unique functionality are demonstrated. E.g., equation With and without \boldsymbol.


* Removed the nagging warning in R2015b for the use of 'feature('UseHG2')'

* Hopefully this is the final fix for the printmode...

(v1.2.1) * Bug fix: listening to Axes' XLim change

v1.2 - Improved print output scaling

* Added demo script
* Image's Clipping property is set to 'off' by default
* If LaTeX build failed, returns the auto-generated .tex file content
* Pre-HG2 enhancement: listening to figure size change
* Various bug fixes

* Fixed a bug for pre-r2014b
* Edited Description page

MATLAB Release Compatibility
Created with R2015a
Compatible with any release
Platform Compatibility
Windows macOS Linux
Tags Add Tags

Inspired: Hatchfill2