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
Requirements:
* latex and dvipng executable must be available via system path. For Windows systems, simply installing MikTeX would take care of it
TODOs:
* 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
Kesh Ikuma (2021). lateximage (https://www.mathworks.com/matlabcentral/fileexchange/53474-lateximage), MATLAB Central File Exchange. Retrieved .
Inspired: Hatchfill2
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Create scripts with code, output, and formatted text in a single executable document.
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"
-output-directory="/private/var/folders/r7/4p1xw_kx6wl6qpgzxswd_5v40000gn/T"
"/private/var/folders/r7/4p1xw_kx6wl6qpgzxswd_5v40000gn/T/tp85b54206_968a_4d71_b517_5519031f49f6.tex"
zsh:1: command not found: latex
Generated .tex file:
\documentclass[preview=true,margin=3]{standalone}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{bm}
\begin{document}
$\displaystyle
\boldsymbol\xi$
\end{document}
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)
lateximage(1,10,'\boldsymbol\xi','HorizontalAlignment','left','FontSize',20,'Color',get(h,'Color'),...
Nice updates Kesh. lateximage() now runs without warnings being generated.
@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.