
예를 들어 여기에 있는 LaTeX 및 색상의 Matlab 코드가 필요합니다(주석은 녹색, 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}
산출:
추신: 다음에 라텍스 관련 질문이 있을 때 먼저 TEX를 검색해 보세요!