我需要 Matlab 代码,例如这里的代码,以 LaTeX 格式并带有颜色(注释为绿色,for/if/else/end 为蓝色)。
% super cooles Programm
i = 1
for i = 1:10
if i > 3
i=i+2
else
i=i+1
end
end
我试过
`\begin{lstlisting}
code
\end{lstlisting}`
这给了我代码,但不是彩色的。
我试过
\lstinputlisting[language=Matlab, frame=single]{\MatlabCodeToLaTeX.m}
这给了我错误消息,LaTeX 无法启动该文件。
如何才能让代码呈现出正确的颜色?
答案1
我改编了示例表格特克斯
\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}
\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},
}
\begin{document}
\begin{lstlisting}[frame=single]
% super cooles Programm
i = 1
for i = 1:10
if i > 3
i=i+2
else
i=i+1
end
end
\end{lstlisting}
\end{document}
输出:
PS:下次遇到有关乳胶的问题时,请先尝试搜索 TEX!