5

So I'm trying to render something beautiful for my PHP code using the \lstdefinestyle command.

I think the basic configuration is good, but I would like to highlight the function name to render it in the color normal.

Here is my code:

\documentclass[10pt,a4paper]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage{listings}             % to insert code
\usepackage{xcolor}
\definecolor{background}{RGB}{40, 44, 52}           % gris foncé
\definecolor{comment}{RGB}{92, 99, 112}             % gris clair
\definecolor{keyworld}{RGB}{86, 182, 194}           % cyan
\definecolor{string}{RGB}{152, 195, 121}            % vert
\definecolor{identifier}{RGB}{198, 120, 221}        % mauve
\definecolor{classic}{RGB}{224, 108, 117}           % genre de rouge
\definecolor{normal}{RGB}{171, 178, 191}            % blanc
\definecolor{specialkeyworld}{RGB}{229, 192, 123}   % jaune orange
\definecolor{veridad}{RGB}{209, 154, 102}           % orange jaune

\lstdefinestyle{myPHP}{
  language=PHP,              
  backgroundcolor=\color{background},   
  basicstyle=\footnotesize\ttfamily\color{normal} ,     
  keywordstyle=\color{keyworld},     
  commentstyle=\color{comment},     
  numberstyle=\color{background}, 
  identifierstyle=\color{classic},
  stringstyle=\color{string},    
  emph        =[1]{function,try,return,catch, new},
  emphstyle   =[1]\color{identifier},
  emph        =[2]{Exception, PDO},
  emphstyle   =[2]\color{specialkeyworld},
  emph        =[3]{true, false},
  emphstyle   =[3]\color{veridad},
  emph        =[4]{connectAccess},
  emphstyle   =[4]\color{normal},
  stringstyle=\color{string},     
  breakatwhitespace=false,              
  breaklines=false,                         
  keepspaces=true,                
  numbers=left,                   
  numbersep=4pt,                 
  showspaces=false,        
  showstringspaces=false,      
}
\begin{document}
    \begin{lstlisting}[style=myPHP]
    /**
     *  Connect an Access database
     *  @param  string  $path   Name of the database you want to connect to
     *  @return PDO object initialise
     */
    function connectAccess($path){
      try {
        $db = new PDO("odbc:DRIVER={Microsoft Access Driver (*.mdb)}; Dbq=$path;");
        return $db;
      } catch (Exception $e) {
        echo 'Connection failed: ' . $e->getMessage();
        return false;
      }
    }
    \end{lstlisting}
\end{document}

and the result :

result

I would like to replace the two lines

  emph        =[4]{connectAccess},
  emphstyle   =[4]\color{normal},

with something that will highlight what is between function and ( without selecting those two, because I will be displaying a lot of code in my document and I dont want to escape the function name each time.

I've tried moredelim=[is][\color{normal}\ttfamily]{function}{(}, but it render like this :

moredelim

with moredelim=[s][\color{normal}\ttfamily]{function}{(}, the 'function' selector is also rendered in the \color{normal} font...

and I have pretty much the same results with morecomment.

What am I doing wrong? aren't moredelim and morecomment the right parameters to use? if not, what are the original purpose of these two?

Is there a(nother) way to do it or will I have to escape the function name each time?

Thank you for your time and answer, Guillaume

  • I've seen these type of comments for improper questions, do you think I have to edit my question for something specific or is it just a way to say hello? – guillaume latour May 09 '16 at 08:16
  • @guillaumelatour Hello :-) – touhami May 09 '16 at 08:18
  • @touhami Hello :D – guillaume latour May 09 '16 at 08:40
  • 1
    That's quite a rainbow you show here. Imho you are overdoing with the colors. Beside this I don't think tha there is a better solution than the one you found. Delimiters are printed in the same style as the part between them. So you can't use them here. Side remark: You named your color "keyworld" instead of "keyword". – Ulrike Fischer May 09 '16 at 09:13
  • @guillaumelatour : actually not! I originally wanted to congratulate you for your very well asked (imho) first question, but I had to go and posted my comment without these. My bad. Anyway, my intention was to - briefly but warmly - welcome you (: – ebosi May 09 '16 at 09:14
  • @UlrikeFischer I'm trying to reproduce the colors used in Atom -> I kinda love rainbows ^^

    ty for the typo, it will certainly avoid some ragesearch.

    Ok, so neither moredelim or morecomment will do it but do you think something else can do the trick?

    I am quite new to latex so my problem is not only that I do not know where to look, but I don't even know what to look for; but I have faith something exists, in the shadow of my ignorance, and it will answer my needs :-)

    – guillaume latour May 09 '16 at 09:29
  • @ebo ok, thanks for the salutations then :-)

    (I wasn't sure the title was relevant so I wanted some confirmation before changing anything)

    – guillaume latour May 09 '16 at 09:34
  • 6

1 Answers1

1

Here is the trick used in @jkuczm 's link:

you have to define a new command:

\newcommand{\functionDefHighlight}[1]{\textcolor{identifier}{function} \textcolor{normal}{ #1}(}

and then use it when parametring moredelim:

moredelim = [is][\functionDefHighlight]{function\ }{(},