Como incluir o código Matlab no LaTeX em cores?

Como incluir o código Matlab no LaTeX em cores?

Preciso do código Matlab, por exemplo, este aqui, em LaTeX e em cores (o comentário é verde, for/if/else/end é azul).

% super cooles Programm
i = 1
for i = 1:10
    if i > 3
        i=i+2
    else 
        i=i+1
    end
end

tentei

`\begin{lstlisting}
code
\end{lstlisting}`

isso me deu o código, mas não em cores.

tentei

\lstinputlisting[language=Matlab, frame=single]{\MatlabCodeToLaTeX.m}

isso me deu a mensagem de erro de que o LaTeX não conseguiu iniciar o arquivo.

Como faço para que o código seja colorido corretamente?

Responder1

Adaptei o formulário de exemploTEXTO

\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}

saída:

insira a descrição da imagem aqui

PS: da próxima vez que houver uma dúvida sobre látex, tente pesquisar TEX primeiro!

informação relacionada