# vim type Display for C Code in Latex

Target Display:

My Latex Code:

% xxxxxxxxxxxxxxxxxxxxxxxxx Code Snippet STARTS xxxxxxxxxxxxxxxxxxxxxx
\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,
%keywordstyle=\bfseries\color{OliveGreen},         % keyword style
keywordstyle=[1]\bfseries\color{OliveGreen},
% Define TYPE-1 Keywords
keywords=[1]{
int, char,float, double, unsigned, signed,
goto},
% Define TYPE-2 Keywords
keywordstyle=[2]\bfseries\color{Violet},
keywords=[2]{
%s, %d,
include, define},
% Define TYPE-3 Keywords
keywordstyle=[3]\bfseries\color{Sepia},
keywords=[3]{
return},
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}
% xxxxxxxxxxxxxxxxxxxxxxxxx Code Snippet ENDS xxxxxxxxxxxxxxxxxxxxxxxx


My Output is as follows:

The fields marked under Red Line have Different Formatting from the Target.

• Please help me correct these settings so that I can get the desired 'gvim' like output.

• How can I specify keywordstyle for Format Specifiers since, putting: %s, %d -> make the statement commented?

Thanks.

-

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,
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}


-
Thanks a lot ! This is exactly what I wanted. –  Sandeep Singh Aug 17 '12 at 7:29
The effects of "morestring=[b]{<}, morestring=[b]{>}," are disasterous. If I have a for loop like: for (i=0; i<4,...) => This will make EVERYTHING starting from '<' => a String. The entire remaining code will be displayed as a string. Is there any alternative workaround? I tried many combinations, but none seems to work. –  Sandeep Singh Aug 17 '12 at 10:15
@SandeepSingh: That is true. I thought, when reading the documentation, that you can escape those other < and >, but it seems you can only escape string delimiters inside of other strings. I don't know an easy solution for this problem. You could define morecomment[l][\color{Magenta!80}]{\#include}, but then you need to change the color of #include back. Maybe newsgroups.derkeiler.com/Archive/Comp/comp.text.tex/2008-07/… helps there. But before going to such lengths it is probably a good idea to try the minted package. –  canaaerus Aug 17 '12 at 11:30