12

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

2 Answers 2

10

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

4
  • 2
    That's for C. For Objective C, you'd want to use \lstset{language=[Objective]C}. Apr 9, 2010 at 13:25
  • thanks - i have a problem though, my source is quite long, and is going off the width of the page - Overfull problems, can this be solved? thanks
    – joec
    Apr 9, 2010 at 13:38
  • @Joachim, I've never used Objective C and since joec put it between parenthesis, I guessed the languages didn't differ that much. I was wrong! :). Grabbed some Objective C from the net and fixed my example. Thanks.
    – Bart Kiers
    Apr 9, 2010 at 14:48
  • @joec, it appears you didn't look into the link I posted (or too briefly). The breaking of lines is explained in 4.10 Margins and line shape. I adjusted my example. Next time please first read what is already suggested. Thanks.
    – Bart Kiers
    Apr 9, 2010 at 14:50
4

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]%
2
  • 1
    how i use this in a \begin{lstlisting} ?
    – CarlJ
    Aug 11, 2011 at 15:45
  • @meccan: just add it behind the \lstset statement in the beginning of the document
    – brainray
    Nov 10, 2012 at 21:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.