Draw a box around an equation in MATLAB live editor.




0 Comments
Answers (2)
Hi @David Cole,
I apologize for the delayed response. I did not pay attention to my comments.
The \boxed command does NOT work in MATLAB's equation editor.
As Stephen23 correctly pointed out in the thread, MATLAB only supports a limited subset of LaTeX commands. The \boxed command, which comes from the AMS math package in full LaTeX, is not among the supported commands. Official MathWorks documentation confirms \boxed is NOT supported.
According to the <https://www.mathworks.com/help/matlab/matlab_prog/insert-equations.html Official Documentation > under the "Math Constructs" section, the complete list of supported LaTeX math commands includes: frac, sqrt, bmod, pmod, widehat, widetilde, bra, ket, braket, stackrel, overset, underset, binom, choose, pmatrix, matrix, begin{array}, begin{cases}, left, middle, right, limits, and nolimits — \boxedis completely absent from this list.
Additionally, under "Text Styling," only \hbox and \mbox are supported for box commands, and as Stephen23 noted, these do not draw borders around content.
Practical Solutions:
Since MATLAB's equation editor doesn't support \boxed, here are three alternative approaches:
Option 1: Use the text() function with graphics borders (Stephen23's approach)
figure; axh = axes('Visible','off'); text(axh, 0.5, 0.5, '$R\left(A^T \right)=\left[\begin{array}{cc} 1 & 0 \\ 0 & 2 \\ 3 & 0 \\ 0 & -1 \end{array}\right]$', ... 'Interpreter', 'latex', ... 'FontSize', 14, ... 'HorizontalAlignment', 'center', ... 'EdgeColor', 'red', ... 'LineWidth', 2, ... 'Margin', 5);
Option 2: Create the boxed equation in an external LaTeX editor
Use a LaTeX editor (Overleaf, TeXShop, or any online LaTeX editor) where \boxed works properly:
\documentclass{standalone} \usepackage{amsmath} \begin{document} \boxed{ R\left(A^T \right)=\begin{bmatrix} 1 & 0 \\ 0 & 2 \\ 3 & 0 \\ 0 & -1 \end{bmatrix} } \end{document}
Export as PNG or PDF, then import into MATLAB:
% Import the image img = imread('boxed_equation.png');
% Display in a figure figure; imshow(img); axis off;
% Alternatively, insert into Live Editor using Insert > Image from the menu
Option 3: Use annotation rectangles
figure; % Create the equation with text txt = text(0.5, 0.5, '$R\left(A^T \right)=\left[\begin{array}{cc} 1 & 0 \\ 0 & 2 \\ 3 & 0 \\ 0 & -1 \end{array}\right]$', ... 'Interpreter', 'latex', ... 'FontSize', 14, ... 'HorizontalAlignment', 'center', ... 'Units', 'normalized');
% Get the extent of the text extent = get(txt, 'Extent');
% Add padding padding = 0.02;
% Create annotation rectangle around the text annotation('rectangle', ... [extent(1)-padding, extent(2)-padding, extent(3)+2*padding, extent(4)+2*padding], ... 'EdgeColor', 'red', ... 'LineWidth', 2);
axis off;
There is no direct way to box equations within MATLAB's equation editor itself. This limitation is by design, as MATLAB doesn't support the full LaTeX package ecosystem. However, the three options above should provide you with practical workarounds to achieve the desired result.
I hope this clarifies the situation and provides you with workable solutions!
0 Comments
See Also
Categories
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!