Welcome to TeX.SX! Please always add a fully compilable minimal working example (MWE).
I think, you need something like that:
\documentclass{article}
\usepackage{listings}
\usepackage[usenames,dvipsnames]{xcolor}
\begin{document}
...
\end{document}
Now to your problem. If you look into listings/lstlang1.sty
you will see, that #include
is not a keyword
, but a directive
. So you can say
...
directivestyle=\bfseries\color{Violet},
...
and all those #...
directives are colored in violet.
With %s
that is another problem. You could escape %s
as \%
and give the option alsoletter={\%}
. But keywords are generally not highlighted in strings. So this would highlight a free-floating %s
, but not the ones in your example and also not other expressions like %03d
, which can be used in printf
.
There is a solution for this too, but only because of a bug in the listings
package. (Compare also this thread) You can give %s
as otherkeyword
and it will get highlighted in strings. As otherkeyword
is automatically in keyword group 1, you will have to move the regular keywords into another group, as they should be styled differently.
Numbers can be colored as in How can I change the color of digits when using the listings package?. Notice also the comment there on how to have no highlight in comments.
The <stdio.h>
is basically a string. So you can just put <
and >
as new string delimiters. But be careful that you have to escape them if they would occur elsewhere.
I can't say right now how you could highlight constants after #define
like SIZE
. (If it is even possible)
So all in all you will have the following:
\documentclass{article}
\usepackage{listings}
\usepackage[usenames,dvipsnames]{xcolor}
\def\digitcolor{\color{Magenta!80}}
\begin{document}
\lstset{
language=C, % choose the language of the code
stepnumber=1, % the step between two line-numbers. If it's 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}
showspaces=false, % show spaces adding particular underscores
showstringspaces=false, % underline spaces within strings
showtabs=false, % show tabs within strings adding particular underscores
tabsize=4, % sets default tabsize to 2 spaces
captionpos=t, % sets the caption-position to top
breaklines=true, % sets automatic line breaking
breakatwhitespace=true, % sets if automatic breaks should only happen at whitespace
% title=\lstname, % show the filename of files included with \lstinputlisting;
identifierstyle=\color{black},
caption={Array of Pointers to Strings},
frame=lrtb,
% Define TYPE-1 Keywords
directivestyle=\bfseries\color{Violet},
keywords={},
otherkeywords={\%s, \%d},
keywordstyle=\bfseries\color{Violet},
keywordstyle=[2]\bfseries\color{OliveGreen},
% Define TYPE-2 Keywords
keywords=[2]{
auto,break,case,char,const,continue,default,do,double,%
else,enum,extern,float,for,goto,if,int,long,register,%
short,signed,sizeof,static,struct,switch,typedef,union,unsigned,%
void,volatile,while},
% int, char,float, double, unsigned, signed,
% goto},
% Define TYPE-3 Keywords
keywordstyle=[3]\bfseries\color{Sepia},
keywords=[3]{
return},
literate=*%
{0}{{{\digitcolor0}}}1
{1}{{{\digitcolor1}}}1
{2}{{{\digitcolor2}}}1
{3}{{{\digitcolor3}}}1
{4}{{{\digitcolor4}}}1
{5}{{{\digitcolor5}}}1
{6}{{{\digitcolor6}}}1
{7}{{{\digitcolor7}}}1
{8}{{{\digitcolor8}}}1
{9}{{{\digitcolor9}}}1,
commentstyle=\bfseries\color{blue}, % comment style
morestring=[b]{<},
morestring=[b]{>},
stringstyle= \color{Magenta!80}, % string literal style
belowcaptionskip = 0.2in, % Space below caption
abovecaptionskip = 0.2in % Space above caption
}
\begin{lstlisting}
#include <stdio.h>
#define SIZE 4
int main()
{
char *strings[SIZE] =
{
"String1",
"String2",
"String3",
"String4"
};
char *ptr_swap; /* A temporary pointer to swap strings */
/* Swap "String2" with "String3" */
ptr_swap = strings [1];
strings [1] = strings [2];
strings [2] = ptr_swap;
printf ("%s %s %s %s", strings[0], strings[1], strings[2], strings[3]);
return 0;
}
\end{lstlisting}
\end{document}
Best Answer
As described in the documentation of the
listings
package, you have to define thecaption
andlabel
in the options of thelstlisting
environment. This is required because any text withinlstlisting
is typeset, so your code prints\label{phpheg}
in the PDF, instead of calling that as a LaTeX command.Note: as Werner says in his comment, as everything between
\begin{lstlisting}
and\end{lstlisting}
is printed, thus indented code is also typeset indented. As a work-around, you can (and should) write the listing without any indention (as above).As proposed by MaxNoe, you can use the
lstautogobble
package as a workaround: simply add\usepackage{autogobble}
and setautogobble=true
in the settings oflstlistings
.