2

I'm extremely new to Latex - I just started two days ago. I'm writing a programming manual, so I'm making use of the listings package. However, I don't know how to configure Latex yet, so I'm just piecing together stuff I've found off the internet.

One problem I've come across is that my code inside quotes has a weird character being inserted in spaces: 'Social␣Networking␣Site'. I don't know how to get rid of this character so that it just shows spaces instead of the weird little ␣ character.

Here is my configuration, can anyone provide some assistance with this?

\usepackage{listings}
\IfFileExists{xcolor.sty}{
    \RequirePackage{xcolor}
}{
    \RequirePackage{color}
}


%%%%%%%%%%%%%%%%%%%%%% PHP START %%%%%%%%%%%%%%%%%%%%%%%%%%

\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}
%\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,
  },
%  stepnumber=1,  
%  numberfirstline=true,
%  numberstyle=\footnotesize,
  xleftmargin=4.0ex,
  upquote=true,
  showlines=true
  }

%%%%%%%%%%%%%%%%%%%%%% PHP End %%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%% YML START %%%%%%%%%%%%%%%%%%%%%%%%%%

\newcommand\YAMLcolonstyle{\color{red}\mdseries}
\newcommand\YAMLkeystyle{\color{black}\bfseries}
\newcommand\YAMLvaluestyle{\color{blue}\mdseries}

\makeatletter

% here is a macro expanding to the name of the language
% (handy if you decide to change it further down the road)
\newcommand\language@yaml{yaml}

\expandafter\expandafter\expandafter\lstdefinelanguage
\expandafter{\language@yaml}
{
  keywords={true,false,null,y,n},
  keywordstyle=\color{darkgray}\bfseries,
  basicstyle=\YAMLkeystyle,                                 % assuming a key comes first
  sensitive=false,
  comment=[l]{\#},
  morecomment=[s]{/*}{*/},
  commentstyle=\color{purple}\ttfamily,
  stringstyle=\YAMLvaluestyle\ttfamily,
  moredelim=[l][\color{orange}]{\&},
  moredelim=[l][\color{magenta}]{*},
  moredelim=**[il][\YAMLcolonstyle{:}\YAMLvaluestyle]{:},   % switch to value style at :
  morestring=[b]',
  morestring=[b]",
  literate =    {---}{{\ProcessThreeDashes}}3
                {>}{{\textcolor{red}\textgreater}}1     
                {|}{{\textcolor{red}\textbar}}1 
                {\ -\ }{{\mdseries\ -\ }}3,
}

% switch to key style at EOL
\lst@AddToHook{EveryLine}{\ifx\lst@language\language@yaml\YAMLkeystyle\fi}
\makeatother

\newcommand\ProcessThreeDashes{\llap{\color{cyan}\mdseries-{-}-}}

%%%%%%%%%%%%%%%%%%% YML END %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  • Welcome to TeX.se. In general, it's helpful if you include a minimal working example (MWE) in your question (it gives us something to start with). Since you're new to LaTeX, it's understandable that you won't get it completely minimal, but you can start by removing whatever code you can, and seeing if you still have the problem. – Teepeemm Nov 26 '16 at 1:46
  • \IfFileExists{xcolor.sty}{ \RequirePackage{xcolor} }{ \RequirePackage{color} } can (should) simply be \usepackage{xcolor} as it will be available and loading color wouldn't work as you use xcolor syntax in the following commands. – David Carlisle Nov 26 '16 at 2:21
2

Have you found the listings documentation? It's available at CTAN. If you have a command line, you can also type the command texdoc listings. It's 60 pages long, but will definitely be useful.

Section 1.3 of the documentation starts out with an example, and uses showstringspaces=false in the language configuration (i.e., where you have xleftmargin=4.0ex,) in order to have "no special string spaces".

  • I found the solution in 1.3. But why does it change space to new character? Is it good typography? – alhelal Sep 26 '17 at 10:14
  • "Good typography" is a matter of opinion. But in many programming languages, spaces are significant (usually within string literals, but sometimes also elsewhere). Having spaces explicitly typeset with some character helps keep track of how many there are. – Teepeemm Sep 26 '17 at 17:28

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.