Matlab コードをカラーで LaTeX に含めるにはどうすればいいですか?

Matlab コードをカラーで LaTeX に含めるにはどうすればいいですか?

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: 次回 LaTeX に関する質問があったら、まず TEX を検索してみてください。

関連情報