Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Does someone have a good definition for highlighting PHP code with the listings package?

share|improve this question
    
According to the manual a PHP definition is provided. –  qubyte May 1 '12 at 11:22
    
Sure, but there are no colours in this definition? –  Sam May 1 '12 at 11:25
1  
See the manual for details on how to change the default monochrome appearance. Note that this is not actually to do with PHP or any other language. You assign a style (with a colour if you choose) to something such as a string, and listings determines what part of the source code is a string and applies your style. Therefore you styling can apply to any language. See sections 1.3 and 2.8 of the manual for an example. –  qubyte May 1 '12 at 11:33
    
I'm aware of. My question is, does someone have a such definition, which meet the code specialities of php? –  Sam May 1 '12 at 11:42
1  
Perhaps not too helpful, but for the former take a look at this: tex.stackexchange.com/questions/42889/… Ignore the literate part of that style as it's not really necessary. Maybe it'll give you a good starting point. –  qubyte May 1 '12 at 11:57

2 Answers 2

up vote 6 down vote accepted

listings provides lots of options to color/customize the format of the style to your likings. Of course you should choose php as a language.

This may be a starting point:

\documentclass{article}
\usepackage{listings,xcolor}
\usepackage{inconsolata}

\definecolor{dkgreen}{rgb}{0,.6,0}
\definecolor{dkblue}{rgb}{0,0,.6}
\definecolor{dkyellow}{cmyk}{0,0,.8,.3}

\lstset{
  language        = php,
  basicstyle      = \small\ttfamily,
  keywordstyle    = \color{dkblue},
  stringstyle     = \color{red},
  identifierstyle = \color{dkgreen},
  commentstyle    = \color{gray},
  emph            =[1]{php},
  emphstyle       =[1]\color{black},
  emph            =[2]{if,and,or,else},
  emphstyle       =[2]\color{dkyellow}}

\begin{document}

\begin{lstlisting}
<?php
/* this is a stupid example */
$username = $_POST["username"];
$passwort = $_POST["passwort"];

$pass = md5($passwort);

// another comment
if($username=="Andavos" and
$pass=="fd0d9cdefd5d42dfa36c74a449aa8214")
   {
   echo "Herzlich Willkommen";
   }
else
   {
   echo "Login Fehlgeschlagen";
   }
?>
\end{lstlisting}

\end{document}

enter image description here

share|improve this answer
    
FYI: meta.tex.stackexchange.com/questions/2414/… I think tat this question probably qualifies. As it stands, it seems ambiguous and by answering it lowers the odds that the question will be improved to bring it up to standard. –  qubyte May 7 '12 at 9:59
    
I think it should stay now that it's here. If the question doesn't improve then the question should be closed. –  qubyte May 7 '12 at 10:11
    
I'm still hoping that it'll be updated. –  qubyte May 7 '12 at 10:15
    
Why this works when it is not included? When I include it: i.imgur.com/MdMrn38.png –  ilhan Jun 21 '13 at 21:28

I found that the default parsing is somewhat weird, some functions like 'header' will be highlight, even if they are class functions, but other functions will not. All identifiers will be colored the same way, no matter if they are functions or variables. To fix the whole lstlisting highlighting I hacked the following TeXcode for use in XeTeX:

\newcommand{\PHPamountofcolor}{75}
\newcommand{\SourceCodeContext}{5}
%Lets define the php language colors:
\definecolor{PHP_comment_old}{HTML}{FF8000}
\colorlet{PHP_comment}{PHP_comment_old!\PHPamountofcolor!black}
\definecolor{PHP_default_old}{HTML}{000000}
\colorlet{PHP_default}{PHP_default_old!\PHPamountofcolor!black}
\definecolor{PHP_keyword_old}{HTML}{6c9c11}
\colorlet{PHP_keyword}{PHP_keyword_old!\PHPamountofcolor!black}
\definecolor{PHP_emph1_old}{HTML}{0F58A2}
\colorlet{PHP_emph1}{PHP_emph1_old!\PHPamountofcolor!black}
\definecolor{PHP_emph2_old}{HTML}{CCAA00}
\colorlet{PHP_emph2}{PHP_emph2_old!\PHPamountofcolor!black}
\definecolor{PHP_emph4_old}{HTML}{C60484}
\colorlet{PHP_emph4}{PHP_emph4_old!\PHPamountofcolor!black}
\definecolor{PHP_string_old}{HTML}{C78F0A}
\colorlet{PHP_string}{PHP_string_old!\PHPamountofcolor!black}
\definecolor{PHP_variable_old}{HTML}{C82210}%C82210
\colorlet{PHP_variable}{PHP_variable_old!\PHPamountofcolor!black}
\definecolor{PHP_number_old}{HTML}{BF1CA6}
\colorlet{PHP_number}{PHP_number_old!\PHPamountofcolor!black}
%Now we want to highlight the variables. This will be done by triggering the function \PHPhighlightvar at the start of any $ run. This function wil only highlight variables and any other identifiers will be ignored. Luckily lstlisting will only give correct identifiers so we only will have to check if the previous call was made with a $
\usepackage{fontspec}
\setmonofont{Courier}
%\usepackage[utf8]{inputenc}
%\usepackage[T1]{fontenc}
%\usepackage{courier, textcomp}
\usepackage{etoolbox}
\newtoggle{InString}{}% Keep track of if we are within a string
\togglefalse{InString}% Assume not initally in string

\newcommand*{\ColorIfNotInString}[1]{\iftoggle{InString}{#1}{\color{PHP_number}#1}}%
%helper

\newcommand{\PHPhighlightvar}[1]{\ifnum\theDollarFlag=1 \color{PHP_variable} \fi#1\setcounter{DollarFlag}{0}}
\newcounter{DollarFlag}


\lstset{
  language        = php,
  basicstyle      = \footnotesize\ttfamily,
  keywordstyle    = \color{PHP_keyword},
  stringstyle     = \color{PHP_string!90!black}\toggletrue{InString},
  %this allows highlighting of variables:
  literate        =  {\$}{{\iftoggle{InString}{\$}{\setcounter{DollarFlag}{1}\color{PHP_variable}\$\color{PHP_default}}}}1
%    {"}{{{\ProcessQuote{"}}}}1% Disable coloring within double quotes
%    {'}{{{\ProcessQuote{'}}}}1% Disable coloring within single quote
    {0}{{{\ColorIfNotInString{0}}}}1
    {1}{{{\ColorIfNotInString{1}}}}1
    {2}{{{\ColorIfNotInString{2}}}}1
    {3}{{{\ColorIfNotInString{3}}}}1
    {4}{{{\ColorIfNotInString{4}}}}1
    {5}{{{\ColorIfNotInString{5}}}}1
    {6}{{{\ColorIfNotInString{6}}}}1
    {7}{{{\ColorIfNotInString{7}}}}1
    {8}{{{\ColorIfNotInString{8}}}}1
    {9}{{{\ColorIfNotInString{9}}}}1,
  identifierstyle = \color{PHP_default}\PHPhighlightvar,
  commentstyle    = \color{PHP_comment}\slshape,
  emph            =[1]{require_once, require, include_once, include, namespace, use, class, function, new},
  emphstyle       =[1]\color{PHP_emph1},%\bf,
  emph            =[2]{echo, empty, isset, array, instanceof},
  emphstyle       =[2]\color{PHP_emph2},%\bf,
  emph            =[3]{var, const, abstract, 
                        protected, private, public,
                        static, final, extends, implements,
                        global, if, else, foreach ,for,
                        endforeach, endif, endfor, elseif,
                        as},
  emphstyle       =[3]\color{PHP_keyword},%\bf,
  emph            =[4]{return, throw, exit, __halt_compiler, continue, break},
  emphstyle       =[4]\color{PHP_emph4},%\bf,
  breaklines      = true,
  captionpos      = b,
  rulecolor       =\color{black},
  keywords    ={__halt_compiler,    abstract,   and,    array,
                    as, break,  callable,   case,   catch,  class,
                    clone,  const,  continue,   declare,    default,
                    die,    do, echo,   else,   elseif,
                    empty,  enddeclare, endfor, endforeach, endif,
                    endswitch,  endwhile,   eval,   exit,   extends,
                    final,  finally,    for,    foreach,    function,
                    global, goto, if,   implements, include,
                    include_once,   instanceof, insteadof,
                    interface,  isset, list,    namespace,
                    new,    or, print, private, protected,  public,
                    require,    require_once, return,   static,
                    switch, throw,  trait, try, unset, use, var,
                    while,  xor,    yield,
  },
  numbers=left,
  stepnumber=1,  
  numberfirstline=true,
  numberstyle=\footnotesize,
  xleftmargin=4.0ex,
  upquote=true,
  showlines=true
  }

Maybe this is of use for someone. It produces something like this: enter image description here

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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