Skip to content

Instantly share code, notes, and snippets.

@R0mb0
Last active November 11, 2023 14:14
Show Gist options
  • Save R0mb0/1e85f5c5dfbba7b110122388cacbe0ce to your computer and use it in GitHub Desktop.
Save R0mb0/1e85f5c5dfbba7b110122388cacbe0ce to your computer and use it in GitHub Desktop.
PHP code highlighting in listings for pdflatex.
\documentclass{article}
% To write dummy text
\usepackage{lipsum}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% My solution to highlighting PHP code with listings package in pdflatex
% Inspired by this discussion: https://tex.stackexchange.com/questions/53928/is-there-a-good-definition-for-highlighting-php-code-in-listings
\usepackage{listings}
\usepackage{xcolor}
\usepackage{lstautogobble}
\definecolor{PHP_comment}{HTML}{FF8000}
\definecolor{PHP_default}{HTML}{000000}
\definecolor{PHP_keyword}{HTML}{6c9c11}
\definecolor{PHP_emph1}{HTML}{0F58A2}
\definecolor{PHP_emph2}{HTML}{CCAA00}
\definecolor{PHP_emph4}{HTML}{C60484}
\definecolor{PHP_string}{HTML}{C78F0A}
\definecolor{PHP_variable}{HTML}{C82210}%C82210
\definecolor{PHP_number}{HTML}{BF1CA6}
\definecolor{PHP_background}{HTML}{F0F0F0}
\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}}%
\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},
frame =single,
rulecolor ={\color{PHP_default}},
autogobble =true
backgroundcolor ={\color{PHP_background}},
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
{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
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
\lipsum[1]\\
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{lstlisting}[language=php]
private static $affected_rows = 0;
private static $profile = array();
public static function connect(){
// config
$params = Config::get('database');
// set debug mode
static::$debug = Config::get('debug', false);
// build dns string
\end{lstlisting}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\end{document}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment