4

I'm trying to display some PHP code in a LaTeX document using the listings package. Other languages appear fine (HTML for instance) but PHP seems to be ignoring any strings within the code. The rest of the code appears fine which is what is throwing me off.

I've attached a sample of my document and a screenshot of the output. If anyone can help me I would be really grateful!

LaTeX:

\usepackage{listings}
\usepackage{courier}
\lstset{
    basicstyle=\footnotesize\ttfamily,
    numberstyle=\tiny,
    numbersep=5pt,
    tabsize=2,
    extendedchars=true,
    breaklines=true,
    keywordstyle=\color{red},
    frame=b,
    stringstyle=\color{white}\ttfamily,
    showspaces=false,
    showtabs=false,
    xleftmargin=17pt,
    framexleftmargin=17pt,
    framexrightmargin=5pt,
    framexbottommargin=4pt,
    %showstringspaces=false,
    %escapeinside={}{}
}
\usepackage{caption}
\DeclareCaptionFont{white}{\color{white}}
\DeclareCaptionFormat{listing}{\colorbox[cmyk]{0.43, 0.35, 0.35,0.01}{\parbox{\textwidth}{\hspace{15pt}#1#2#3}}}
\captionsetup[lstlisting]{format=listing,labelfont=white,textfont=white, singlelinecheck=false, margin=0pt, font={bf,footnotesize}}

and then...

\lstset{language=PHP,caption=Example PHP Code,label=code:sample}
\begin{lstlisting}
public function execute_via_api($api) {
    $api->require_authentication();
    $total_messages_imported = $this->execute();
    return $api->output(array('response'=>$total_messages_imported.' '.inflect($total_messages_imported,'message').' imported'));
} // end func: execute_via_api
\end{lstlisting}

Output:
enter image description here

  • 3
    Hi Dachande663, welcome to tex.sx. You need to indent your code with four spaces or use the '101010' button on top of the edit window. I took the liberty to fix that for you and to include your image. Please note that you need 10rep points to include images. If you edit your post with less the image will disappear again. – Martin Scharrer Feb 17 '11 at 17:46
  • Thank you Martin, I did initially try the ole' bbcode but that didn't work. I'll remember for next time. – Dachande663 Feb 17 '11 at 18:16
6

The culprit is stringstyle=\color{white}\ttfamily. After all, seeing white text on a white background is rather hard :).

Simply removing \color{white} or replacing it by some other color (e.g., \color{blue}) will solve the problem.

with stringstyle=\color{blue}\ttfamily

  • Ah brilliant, thank you! I had copy and pasted the snippet from elsewhere, obviously it hadn't been written with PHP in mind. Again, thanks! – Dachande663 Feb 17 '11 at 18:18

Your Answer

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

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