image thumbnail


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


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 (2022). lateximage (, MATLAB Central File Exchange. Retrieved .

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

Inspired: Hatchfill2

Community Treasure Hunt

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

Start Hunting!