Take the 2-minute tour ×

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?


share|improve this question
add comment

2 Answers

up vote 9 down vote accepted

Like this:




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

@interface classname : superclassname {
    // instance variables

-(return_type)instanceMethod1:(param1_type)param1_varName :(param2_type)param2_varName;
-(return_type)instanceMethod2WithParameter:(param1_type)param1_varName andOtherParameter:(param2_type)param2_varName;


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

share|improve this answer
That's for C. For Objective C, you'd want to use \lstset{language=[Objective]C}. –  Joachim Sauer Apr 9 '10 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 '10 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 '10 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 '10 at 14:50
add comment

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.)

      % The next two lines are Objective-C 2 keywords.


share|improve this answer
how i use this in a \begin{lstlisting} ? –  CarlJ Aug 11 '11 at 15:45
@meccan: just add it behind the \lstset statement in the beginning of the document –  brainray Nov 10 '12 at 21:47
add comment

Your Answer


