3

I met one problem(maybe three, I will describe them in detail), I'm trying to add code list in latex with listings package. First with limit configuration:

\usepackage{listings}
\definecolor{myblue}{RGB}{20,105,176}
\lstset{ %
    language=C,                % choose the language of the code
    basicstyle=\footnotesize,       % the size of the fonts that are used for the code
    numbers=left,                   % where to put the line-numbers
    numberstyle=\footnotesize,      % the size of the fonts that are used for the line-numbers
    stepnumber=1,                   % the step between two line-numbers. If it is 1 each line will be numbered
    numbersep=5pt,                  % how far the line-numbers are from the code
    backgroundcolor=\color{white},  % choose the background color. You must add \usepackage{color}
    identifierstyle=\bfseries\color{black},
    directivestyle=\bfseries\color{violet},
    tabsize=4,          % sets default tabsize to 2 spaces
%   captionpos=b,           % sets the caption-position to bottom
    breaklines=true,        % sets automatic line breaking
    escapeinside={\%*}{*)},      % if you want to add a comment within your code
    % Define TYPE-1 Keywords
    keywords={},
    otherkeywords={\%s, \%d},
    keywordstyle=\bfseries\color{violet},
    % Define TYPE-2 Keywords
    keywordstyle=[2]\bfseries\color{blue}\ttfamily,
    keywords=[2]{
    auto,break,case,char,const,continue,default,do,double,%
    else,enum,extern,float,for,goto,if,int,long,register,%
    void,volatile,while,return},
%    literate=
%       {\{}{{{\color{white}{\{}}}}{1}
%       {\}}{{{\color{white}{\}}}}}{1}
%       {(}{{{\color{white}{(}}}}{1}
%       {)}{{{\color{white}{)}}}}{1}
%       {>}{{{\color{white}{$>$}}}}{1}
%       {=}{{{\color{white}{$=$}}}}{1}
%       {;}{{{\color{white}{$;$}}}}{1},
    stringstyle=\color{red}\ttfamily,
    commentstyle=\color{blue}\ttfamily,
    directivestyle=\bfseries\color{violet},
    morecomment=[l][\color{magenta}]{\#}
%   escapeinside=!!
}

I show my code above. Use this configuration, everything works fine. Then I need change the background to black, therefore I just change:

backgroundcolor=\color{black},
identifierstyle=\bfseries\color{white},

in above config, but after change, the {, },(,), and other like operators: =, can't be displayed and I think the colour of them keep in black. Therefore, I open the config for literate:

literate=*%
    {\{}{{{\color{white}{\{}}}}{1}
    {\}}{{{\color{white}{\}}}}}{1}
    {(}{{{\color{white}{(}}}}{1}
    {)}{{{\color{white}{)}}}}{1}
    {>}{{{\color{white}{$>$}}}}{1}
    {=}{{{\color{white}{$=$}}}}{1}
    {;}{{{\color{white}{$;$}}}}{1},

after this modification, I have one problem, the ) colour can't be changed. therefore, I found some topics here and I found some of people write literate as:

literate=*%
    {\{}{{\color{white}{\{}}}{1}
    {\}}{{\color{white}{\}}}}{1}
    {(}{{\color{white}{(}}}{1}
    {)}{{\color{white}{)}}}{1}
    {>}{{\color{white}{$>$}}}{1}
    {=}{{\color{white}{$=$}}}{1}
    {;}{{\color{white}{$;$}}}{1},

with one Pair of { } less in each line. I applied this and it do changed the ) colour, but it makes all rest characters can't be displayed, also the line number display has problem:

I type some characters here and no problem to display   
\begin{lstlisting}
\end{lstlisting}
I type some characters here and it can NOT be displayed and I think colour of these characters changed to white.

how can I modify the configuration of listing and solve this issue?

===============UPDATE===============

I just shared a sample code which can reproduce this issue:

\documentclass[a4paper,11pt]{book}

\usepackage{caption}
\DeclareCaptionFont{white}{\color{white}}
\DeclareCaptionFormat{listing}{\colorbox{gray}{\parbox{\textwidth}{#1#2#3}}}
\captionsetup[lstlisting]{format=listing,labelfont=white,textfont=white}

\usepackage{xcolor,colortbl}
\usepackage{listings}

\usepackage{listings}
\definecolor{myblue}{RGB}{20,105,176}
\lstset{ %
    language=C,                % choose the language of the code
    basicstyle=\footnotesize,       % the size of the fonts that are used for the code
    numbers=left,                   % where to put the line-numbers
    numberstyle=\footnotesize,      % the size of the fonts that are used for the line-numbers
    stepnumber=1,                   % the step between two line-numbers. If it is 1 each line will be numbered
    numbersep=5pt,                  % how far the line-numbers are from the code
    backgroundcolor=\color{black},  % choose the background color. You must add \usepackage{color}
    identifierstyle=\bfseries\color{white},
    directivestyle=\bfseries\color{violet},
%   showspaces=false,               % show spaces adding particular underscores
%   showstringspaces=false,         % underline spaces within strings
%   showtabs=false,                 % show tabs within strings adding particular underscores
%   frame=signal,           % adds a frame around the code
    tabsize=4,          % sets default tabsize to 2 spaces
%   captionpos=b,           % sets the caption-position to bottom
    breaklines=true,        % sets automatic line breaking
%   breakatwhitespace=false,    % sets if automatic breaks should only happen at whitespace
    escapeinside={\%*}{*)},      % if you want to add a comment within your code
    % Define TYPE-1 Keywords
    keywords={},
    otherkeywords={\%s, \%d},
    keywordstyle=\bfseries\color{violet},
    % Define TYPE-2 Keywords
    keywordstyle=[2]\bfseries\color{yellow}\ttfamily,
    keywords=[2]{
    auto,break,case,char,const,continue,default,do,double,%
    else,enum,extern,float,for,goto,if,int,long,register,%
    void,volatile,while,return},
    literate=*%
        {\{}{{\color{white}{\{}}}{1}
        {\}}{{\color{white}{\}}}}{1}
        {(}{{\color{white}{(}}}{1}
        {)}{{\color{white}{)}}}{1}
        {>}{{\color{white}{$>$}}}{1}
        {=}{{\color{white}{$=$}}}{1}
        {;}{{\color{white}{$;$}}}{1},
    stringstyle=\color{red}\ttfamily,
    commentstyle=\color{green}\ttfamily,
    directivestyle=\bfseries\color{violet},
    morecomment=[l][\color{magenta}]{\#}
%   escapeinside=!!
}

\begin{document}
I type some characters here and no problem to display   
\begin{lstlisting}[frame=single, label=118x_power_off,caption=213x platform power ON event detection]
int main(/*@ unused @*/int argc, /*@ unused @*/char *argv[]) //comment for lint
{
#if defined(test)   //howchen, for SHUTDOWN module
    /* test comment, (, ), >, <, -, =, .*/  

    /* test keyword */
    auto,break,case,char,const,continue,default,do,double,
    else,enum,extern,float,for,goto,if,int,long,register,
    void,volatile,while,return;

    item.a[1] = 0;
    item->b[1] = 1;

    foo();

    /* test string */
    printf(" >> test <<\r\n");  

    return 0;   
#endif
}
\end{lstlisting}
I type some characters here and it can NOT be displayed and I think colour of these characters changed to white.
\end{document}

pls use above code to see the result. enter image description here

you can find characters after \end{lstlisting} can't be displayed and line number stop at 1

How Chen
  • 733
  • 2
    Please do a full minimal example, so we do not have to fill in the rest of the preamble etc. It is a LOT easier to help, if we can just copy'n'paste an example and work with it immediately. – daleif May 22 '14 at 09:16
  • @daleif, I update the sample code at the end and can reproduce my issue, I think you can copy and compile it – How Chen May 22 '14 at 09:41

1 Answers1

4

No need for literate, here, really. If you want your text to be white, simply set

basicstyle=\color{white}\footnotesize

Also, because line numbers take the style of basicstyle by default, you need to set

numberstyle=\color{black}\footnotesize

(after setting the value of basicstyle). Finally, to avoid problems with the colour of the frame, you should use

rulecolor = \color{white}

See below. Note that the thin horizontal white lines crossing the listing on my screenshot are just artifacts of my PDF viewer.

enter image description here

\documentclass[a4paper,11pt]{book}

\usepackage{etoolbox}
\usepackage{caption}
\DeclareCaptionFont{white}{\color{white}}
\DeclareCaptionFormat{listing}{\colorbox{gray}{\parbox{\textwidth}{#1#2#3}}}
\captionsetup[lstlisting]{format=listing,labelfont=white,textfont=white}

\usepackage{xcolor,colortbl}
\usepackage{listings}

\usepackage{listings}
\definecolor{myblue}{RGB}{20,105,176}
\lstset{ %
    language=C,
    basicstyle=\color{white}\footnotesize,
    rulecolor = \color{white},
    numbers=left,
    numberstyle=\color{black}\footnotesize,
    stepnumber=1,
    numbersep=5pt,
    backgroundcolor=\color{black},
    identifierstyle=\bfseries\color{white},
    directivestyle=\bfseries\color{violet},
%   showspaces=false,
%   showstringspaces=false,
%   showtabs=false,
%   frame=signal,
    tabsize=4,
%   captionpos=b,
    breaklines=true,
%   breakatwhitespace=false,
    escapeinside={\%*}{*)},
    % Define TYPE-1 Keywords
    keywords={},
    otherkeywords={\%s, \%d},
    keywordstyle=\bfseries\color{violet},
    % Define TYPE-2 Keywords
    keywordstyle=[2]\bfseries\color{yellow}\ttfamily,
    keywords=[2]{
    auto,break,case,char,const,continue,default,do,double,%
    else,enum,extern,float,for,goto,if,int,long,register,%
    void,volatile,while,return},
    stringstyle=\color{red}\ttfamily,
    commentstyle=\color{green}\ttfamily,
    directivestyle=\bfseries\color{violet},
    morecomment=[l][\color{magenta}]{\#}
%   escapeinside=!!
}

\begin{document}
I type some characters here and no problem to display   
\begin{lstlisting}[frame=single, label=118x_power_off,caption=213x platform power ON event detection]
int main(/*@ unused @*/int argc, /*@ unused @*/char *argv[]) //comment for lint
{
#if defined(test)   //howchen, for SHUTDOWN module
    /* test comment, (, ), >, <, -, =, .*/  

    /* test keyword */
    auto,break,case,char,const,continue,default,do,double,
    else,enum,extern,float,for,goto,if,int,long,register,
    void,volatile,while,return;

    item.a[1] = 0;
    item->b[1] = 1;

    foo();

    /* test string */
    printf(" >> test <<\r\n");  

    return 0;   
#endif
}
\end{lstlisting}
I type some characters here and it can NOT be displayed and I think colour of these characters changed to white.
\end{document}
jub0bs
  • 58,916