6

I use the following code in LaTeX to input the code Matlab there but I want to change the color of the background:

  \usepackage{textcomp}
  \usepackage{listings}
  \lstdefinestyle{customc}{
  belowcaptionskip=1\baselineskip,
  breaklines=true,
  frame=L,
xleftmargin=\parindent,
 language=Matlab,
  showstringspaces=false,
 basicstyle=\footnotesize\ttfamily,
 keywordstyle=\bfseries\color{green!40!black},
 commentstyle=\itshape\color{purple!40!black},
 identifierstyle=\color{blue},
stringstyle=\color{orange},
 }

  \lstdefinestyle{customasm}{
 belowcaptionskip=1\baselineskip,
 frame=L,
  xleftmargin=\parindent,
   language=[x86masm]Assembler,
  basicstyle=\footnotesize\ttfamily,
  commentstyle=\itshape\color{purple!40!black},
    }

   \lstset{escapechar=@,style=customc}

How can I do that to look like that in LaTeX report?

output

5

The background colour of listings can be set with the backgroundcolor key. To get the specific colour, you can open your screenshot in for example Gimp and sample the colour used in the Matlab cells, then use this to define a custom colour:

\definecolor{MatlabCellColour}{RGB}{252,251,220}

Then add this to your customc style.

backgroundcolor=\color{MatlabCellColour} 

Complete example:

enter image description here

\documentclass{article}
\usepackage{textcomp,xcolor}
\definecolor{MatlabCellColour}{RGB}{252,251,220}
\usepackage{listings}
\lstdefinestyle{customc}{
  belowcaptionskip=1\baselineskip,
  breaklines=true,
  frame=L,
  xleftmargin=\parindent,
  language=Matlab,
  showstringspaces=false,
  basicstyle=\footnotesize\ttfamily,
  keywordstyle=\bfseries\color{green!40!black},
  commentstyle=\itshape\color{purple!40!black},
  identifierstyle=\color{blue},
  stringstyle=\color{orange},
  backgroundcolor=\color{MatlabCellColour}
 }


\begin{document}
\begin{lstlisting}[style=customc]
for k = 1:1000
  disp(k^2)
end
\end{lstlisting}
\end{document}
  • 1
    @Educ Sorry, I must be blind. What is the problem exactly? – Torbjørn T. Sep 17 '13 at 20:38
  • it's okay now thanks so much could you pleasse tell me what is the name of color in the background of matlab in my picture – Educ Sep 17 '13 at 20:41
  • 1
    @Educ I have no idea, but give me a minute, I'll open the screenshot in Gimp and sample the color. – Torbjørn T. Sep 17 '13 at 20:42
  • The color in RGB model: \definecolor{myyellow}{RGB}{252,251,220}. – Gonzalo Medina Sep 17 '13 at 20:47
  • 1
    @Educ Torbjørn T. has just edited his answer showing you how. – Gonzalo Medina Sep 17 '13 at 20:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.