1

In my paper I include Octave code with

\usepackage{listings}

No I want that the text and numbers be written in the same colors as the are represented with in Octave. Everything is fine, except that I don't know how to color the symbols red that have to be red.

For example keywords could easily be colored blue by writing

keywordstyle=\color{blue}

Is there a similar way for doing this with symbols written in red? I did it manually, but this doesn't work as expected.

This is what I wrote in LaTeX:

\documentclass[a4paper]{report}

\usepackage{color}
\usepackage{listings}
\definecolor{mygreen}{rgb}{0,0.6,0}
\definecolor{mygray}{rgb}{0.5,0.5,0.5}
\definecolor{mymauve}{rgb}{0.58,0,0.82}
\definecolor{myorange}{rgb}{0.855,0.576,0.027}
\lstset{
language=Octave,
frame=single,   
morecomment = [l][\itshape\color{blue}]{\%},
keywordstyle=\color{blue},
commentstyle=\color{mygreen},
breakatwhitespace=false,         
breaklines=true,  
numbers=left,
numbersep=5pt,
numberstyle=\tiny\color{mygray}, 
showstringspaces=false,
showtabs=false,                  
tabsize=2,
stringstyle=\color{myorange},
title=\lstname,
literate=
{+}{{{\color{red}+}}}1
{-}{{{\color{red}-}}}1
{*}{{{\color{red}*}}}1
{,}{{{\color{red},}}}1
{=}{{{\color{red}=}}}1
{)}{{{\color{red})}}}1
{(}{{{\color{red}(}}}1
{;}{{{\color{red};}}}1
{:}{{{\color{red}:}}}1
{[}{{{\color{red}[}}}1
{]}{{{\color{red}]}}}1
{>}{{{\color{red}>}}}1
}

\begin{document}

\begin{lstlisting}[caption={invar\_table.m}]
function out = invar_table(n,m,N)
if n>m, error('first number must be smaller than the second'), endif

t = cputime;
out = zeros(m-n+1,2);

for i=n:m
  out(i+1-n,1) = i;
  out(i+1-n,2) = invar(braidmatrix(i),N);
end

printf('Total CPU time: %f seconds\n', cputime-t);

end
\end{lstlisting}

\end{document}

An example of code in my LaTeX document:

enter image description here

And this is how it should look like:

enter image description here

How can I display the colors correctly?

  • 2
    Please, post a full compilable code so that we can tweak it to look like the picture. Solving problems is quite fun, but copying code from a screenshot will make people avoid your question and you'll probably get no answers. – Phelype Oleinik May 7 '18 at 19:57

Your Answer

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

Browse other questions tagged or ask your own question.