Draw a box around an equation in MATLAB live editor.

45 vues (au cours des 30 derniers jours)
David Cole
David Cole le 4 Oct 2025 à 14:54
Modifié(e) : Umar le 5 Oct 2025 à 15:57
Here is what I want to do.
Is there a direct way to draw a box around an equation directly from equation editor?
Or, do I need to convert the equation to LaTeX? I tried to use the \boxed command in Latex editor but that did not work either.
Without boxed command:
With boxed command (does not work):
Here is my LaTex code for my equation:
$R\left(A^T \right)=\left\lbrace \left\lbrack \begin{array}{c}
1\\
0\\
3\\
0
\end{array}\right\rbrack ,\left\lbrack \begin{array}{c}
0\\
2\\
0\\
-1
\end{array}\right\rbrack \right\rbrace$
I would prefer to just use equation editor without LaTeX.
Thanks

Réponses (2)

Stephen23
Stephen23 le 5 Oct 2025 à 6:39
Modifié(e) : Stephen23 le 5 Oct 2025 à 6:40
@David Cole: BOXED does not help as MATLAB only supports HBOX and MBOX (which do not draw borders):
As an alternative you could display the equation using the graphics function TEXT (which also allows LaTeX) and use the usual graphics options to create a border:
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', 'EdgeColor','red');
  2 commentaires
David Cole
David Cole le 5 Oct 2025 à 6:51
Déplacé(e) : Stephen23 le 5 Oct 2025 à 6:53
Thank you. I have been trying to get boxed working for a while now. But nothing I can do will make boxed work.
Stephen23
Stephen23 le 5 Oct 2025 à 6:54
Modifié(e) : Stephen23 le 5 Oct 2025 à 6:54
"But nothing I can do will make boxed work."
BOXED is not a supported LaTeX command.
I strongly recommend reading the documentation to know what LaTeX commands are supported:

Connectez-vous pour commenter.


Umar
Umar le 5 Oct 2025 à 5:31
Modifié(e) : Umar il y a environ 20 heures

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!

Catégories

En savoir plus sur Environment and Settings dans Help Center et File Exchange

Produits


Version

R2025a

Community Treasure Hunt

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

Start Hunting!

Translated by