Draw a box around an equation in MATLAB live editor.

조회 수: 35 (최근 30일)
David Cole
David Cole 2025년 10월 4일 14:54
편집: Umar 대략 20시간 전
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

답변 (2개)

Stephen23
Stephen23 2025년 10월 5일 6:39
편집: Stephen23 2025년 10월 5일 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
David Cole
David Cole 대략 1시간 전
이동: Stephen23 대략 1시간 전
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 대략 1시간 전
편집: Stephen23 대략 1시간 전
"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:

댓글을 달려면 로그인하십시오.


Umar
Umar 2025년 10월 5일 5:31
편집: Umar 대략 20시간 전

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!

카테고리

Help CenterFile Exchange에서 Environment and Settings에 대해 자세히 알아보기

제품


릴리스

R2025a

Community Treasure Hunt

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

Start Hunting!

Translated by