![列表:如何注释 Matlab 列表(Matlab 代码)(花括号、箭头、文本)](https://linux22.com/image/320075/%E5%88%97%E8%A1%A8%EF%BC%9A%E5%A6%82%E4%BD%95%E6%B3%A8%E9%87%8A%20Matlab%20%E5%88%97%E8%A1%A8%EF%BC%88Matlab%20%E4%BB%A3%E7%A0%81%EF%BC%89%EF%BC%88%E8%8A%B1%E6%8B%AC%E5%8F%B7%E3%80%81%E7%AE%AD%E5%A4%B4%E3%80%81%E6%96%87%E6%9C%AC%EF%BC%89.png)
我有一个 matlab 代码,我想像图片一样解释其中的一部分。或者任何其他想法都可以做得更好。
\documentclass{article}
\usepackage{listings}
\usepackage{color} %red, green, blue, yellow, cyan, magenta, black, white
\definecolor{mygreen}{RGB}{28,172,0} % color values Red, Green, Blue
\definecolor{mylilas}{RGB}{170,55,241}
\begin{document}
\lstset{language=Matlab,%
%basicstyle=\color{red},
breaklines=true,%
morekeywords={matlab2tikz},
keywordstyle=\color{blue},%
morekeywords=[2]{1}, keywordstyle=[2]{\color{black}},
identifierstyle=\color{black},%
stringstyle=\color{mylilas},
commentstyle=\color{mygreen},%
showstringspaces=false,%without this there will be a symbol in the places where there is a space
numbers=left,%
numberstyle={\tiny \color{black}},% size of the numbers
numbersep=9pt, % this defines how far the numbers are from the text
emph=[1]{for,end,break},emphstyle=[1]\color{red}, %some words to emphasise
%emph=[2]{word1,word2}, emphstyle=[2]{style},
}
\section*{Matlab Code}
\lstinputlisting{WEIGHT.m}
\end{document}
WEIGHT.m
matlab文件
clc
clear all
n=input('Please Enter Number Of Grid Point n=');
%-------------
x=zeros(n,1);A=zeros(n,n);
%-------------
for i=1:n
x(i)=0.5*(1-cos((i-1)*pi/(n-1)));
end
for i=1:n
for j=1:n
Pi=1;Pj=1;
for m=1:n
if m~=i
Pi=(x(i)-x(m))*Pi;
end
if m~=j
Pj=(x(j)-x(m))*Pj;
end
end
if i~=j
A(i,j)=Pi/((x(i)-x(j))*Pj);
end
end
for m=1:n
if m~=i
A(i,i)=A(i,i)-A(i,m);
end
end
end
答案1
可能的解决方案是tikz
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{decorations.pathmorphing, decorations.pathreplacing, positioning}
\usepackage[numbered,framed]{matlab-prettifier}
\begin{document}
\begin{tikzpicture}
[%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
node distance=.2cm,
code/.style={rectangle,draw=cyan!50,fill=cyan!20,thick,
inner sep=1cm,minimum size=2mm},
place/.style={rectangle,draw=green!50,fill=green!20,thick,
inner sep=0cm,minimum size=6mm}
]%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\node[code] (A) [text width=13cm]
{
\lstinputlisting[%##########################
style=Matlab-editor,
basicstyle = \mlttfamily,
escapechar = ",
]%#########################
{WEIGHT.m}
};
\node (1) [left=of A,yshift= 5.2cm] {};
\node (2) [below=of 1,yshift=-7mm] {};
\draw[very thick,decorate,decoration={brace,mirror}] (1.east) -- (2.east)
node[midway,left]{\Large{\textbf{1} $\leftarrow$}};
\node (3) [below=of 2,yshift= 3mm] {};
\node (4) [below=of 3,yshift=-6mm] {};
\draw[very thick,decorate,decoration={brace,mirror}] (3.east) -- (4.east)
node[midway,left]{\Large{\textbf{2 important} $\leftarrow$}};
\node (5) [below=of 4,yshift= 3mm] {};
\node (6) [below=of 5,yshift=-83mm] {};
\draw[very thick,decorate,decoration={brace,mirror}] (5.east) -- (6.east)
node[midway,left]{\Large{\textbf{3 Loop} $\leftarrow$}};
\node (7) [below=of 6,yshift= 4mm] {};
\node (8) [below=of 7,yshift=-1mm] {};
\draw[very thick,decorate,decoration={brace,mirror}] (7.east) -- (8.east)
node[midway,left]{\Large{\textbf{4 final} $\leftarrow$}};
\end{tikzpicture}
\end{document}