Text FormattingANSI C code listings

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
Post Reply
krislodz
Posts: 42
Joined: Sun Nov 08, 2009 1:13 pm

ANSI C code listings

Post by krislodz »

Hello,
I have to include C code into report using listings package. The current options used are presented on the example below. The pointer sign * is in the middle which is not so nice and maybe the general look can be improved. Has anybody appended C code before and can advice some neat solutions?

Kris

Code: Select all

\documentclass[a4paper,10pt]{article}
\usepackage[utf8x]{inputenc}

\usepackage{listings}

\begin{document}


\lstset{language=C,frame=single,numbers=left,tabsize=2}
\begin{lstlisting}
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

int
max(int a, int b)
{
 if(a>b) return a;
 else return b;
}

void
reconstruct(char *x, char *y, 
            int table[strlen(x)+1][strlen(y)+1])
{
 int n=strlen(x),m=strlen(y),i,j,currentChar;
 int maxlcs=table[n][m];
 char* lcschar=(char*)malloc((maxlcs+1)*sizeof(char));
 *(lcschar+maxlcs)='\0';

 currentChar=0;
 i=0;
 j=0;
 
 while(currentChar<maxlcs)
 {
  if( *(x+i) == *(y+j) )
  {
   *(lcschar+currentChar)=*(x+i);
   currentChar++;
   //printf("%c\n",*(x+i));
   i++;
   j++;
  }
  else if(table[i+1][j] >= table[i][j+1]) i++;
  else j++;
 }
 printf("%s\n",lcschar);
 free(lcschar);
}


int
lcs(char *x,char *y)
{
 //printf("%zu\n",strlen(x));
 int n=strlen(x), m=strlen(y), i, j, table[n+1][m+1];
 for(i=0;i<=n;i++) table[i][0]=0;
 for(j=0;j<=m;j++) table[0][j]=0;
 for(i=1;i<=n;i++)
  for(j=1;j<=m;j++)
   if( *(x+i) == *(y+j) ) table[i][j]=1+table[i-1][j-1];
   else table[i][j]=max(table[i-1][j], table[i][j-1]);
 
 reconstruct(x,y,table);
 return table[n][m];
}


void
main()
{
 int res=lcs("abdebcbb","adacbcb");
 //printf("%d\n",res);
}
\end{lstlisting}


\end{document}

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

User avatar
gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

ANSI C code listings

Post by gmedina »

Hi,

regarding the pointer sign, you could define a new command (I called it \Myast in my example code) to control the vertical alignment of the sign and then escape to LaTeX inside the lstlisting environment to use the newly defined command. The following example illustrates this approach:

Code: Select all

\documentclass[a4paper,10pt]{article}
\usepackage[utf8x]{inputenc}

\usepackage{listings}

\newcommand\Myast{\raisebox{-1pt}{*}}

\begin{document}

\lstset{language=C,frame=single,numbers=left,tabsize=2,escapeinside=||}
\begin{lstlisting}
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

int
max(int a, int b)
{
if(a>b) return a;
else return b;
}

void
reconstruct(char |\Myast|x, char |\Myast|y, 
            int table[strlen(x)+1][strlen(y)+1])
...
\end{lstlisting}

\end{document}
1,1,2,3,5,8,13,21,34,55,89,144,233,...
krislodz
Posts: 42
Joined: Sun Nov 08, 2009 1:13 pm

Re: ANSI C code listings

Post by krislodz »

That is not a good option in my case, because the source code is taken from file and there is quite a lot of it. It would have to be a method changing the look of * sign in listing directly.

Kris
Post Reply