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

Ludovic C.
  • 8,888
Educ
  • 4,362

1 Answers1

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}
Torbjørn T.
  • 206,688