Question

I am wondering how one is supposed to format Objective C code using the listings package of LaTeX? I know that the language supports C (Objective) , so how do i set this in the \lstset language option?

Thanks

Was it helpful?

Solution

Like this:

\documentclass{article}

\usepackage{listings}

\begin{document}

\lstset{language=[Objective]C, breakindent=40pt, breaklines}

\begin{lstlisting}
@interface classname : superclassname {
    // instance variables
}
+classMethod1;
+(return_type)classMethod2;
+(return_type)classMethod3:(param1_type)param1_varName;

-(return_type)instanceMethod1:(param1_type)param1_varName :(param2_type)param2_varName;
-(return_type)instanceMethod2WithParameter:(param1_type)param1_varName andOtherParameter:(param2_type)param2_varName;
@end
\end{lstlisting}

\end{document}

Detailed introduction: http://mirror.hmc.edu/ctan/macros/latex/contrib/listings/listings.pdf

OTHER TIPS

Although this answer is not answering the OP's question per se, I figure others looking for Objective-C listings related information will stumble across this question.

Below is a listings \lstdefinelanguage for Objective-C 2.0, GNU99, and ANSI C99. While listings includes definitions for both C and Objective-C, the definitions are for the older C89 standard and Objective-C 1.0. The version below adds C99, adds GNU99 to C99, and then adds Objective-C (2.0) to GNU99.

You're probably only go to see a difference if you use a font style for keywords that's different than the "normal" font style.

I am the author of the code below (except for the ANSI C99 definition, which was derived from the listings ANSI C and modified for ANSI C99). You may use it any way you wish, including incorporating it in to other works, without attribution or compensation. I hereby place it in the public domain. (Note: this is mostly for those who work for employers who are really picky about such things, I really don't care.)

\lstdefinelanguage[Objective]{C}[GNU99]{C}
  {morekeywords={@catch,@class,@encode,@end,@finally,@implementation,%
      @interface,@private,@protected,@protocol,@public,@selector,%
      @synchronized,@throw,@try,BOOL,Class,IMP,NO,Nil,SEL,YES,_cmd,%
      bycopy,byref,id,in,inout,nil,oneway,out,self,super,%
      % The next two lines are Objective-C 2 keywords.
      @dynamic,@package,@property,@synthesize,readwrite,readonly,%
      assign,retain,copy,nonatomic%
      },%
   moredirectives={import}%
  }%

\lstdefinelanguage[GNU99]{C}[99]{C}
  {morekeywords={asm,__asm__,__extension__,typeof,__typeof__}%
  }%

\lstdefinelanguage[99]{C}%
  {morekeywords={_Bool,_Complex,_Imaginary,auto,break,case,char,%
      const,continue,default,do,double,else,enum,extern,float,for,%
      goto,if,inline,int,long,register,restrict,return,short,signed,%
      sizeof,static,struct,switch,typedef,union,unsigned,void,volatile,%
      while},%
   sensitive,%
   morecomment=[s]{/*}{*/},%
   morecomment=[l]//,%
   morestring=[b]",%
   morestring=[b]',%
   moredelim=*[directive]\#,%
   moredirectives={define,elif,else,endif,error,if,ifdef,ifndef,line,%
      include,pragma,undef,warning}%
  }[keywords,comments,strings,directives]%
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top