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

Code, edit and compile here:
\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));
 
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Recommended reading 2024:

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

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

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

Code, edit and compile here:
\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}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
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