5

This question already has an answer here:

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

marked as duplicate by Stefan Pinnow, Paul Gaborit, Romain Picot, egreg, user13907 May 24 '16 at 9:13

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • Welcome to TeX.SX Guillaume ! – ebosi May 9 '16 at 7:40
  • 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 9 '16 at 8:16
  • @guillaumelatour Hello :-) – touhami May 9 '16 at 8:18
  • 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 9 '16 at 9:13
  • 6
    Possible duplicate: Listings - Only highlight between delimiters – jkuczm May 22 '16 at 9:15
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\ }{(},

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