使用LaTeX中的列表格式化目标C代码

使用LaTeX中的列表格式化目标C代码,latex,listings,Latex,Listings,我想知道如何使用LaTeX的listings包格式化目标C代码?我知道该语言支持C(Objective),那么如何在\lstsetlanguage选项中设置它呢 谢谢你这样说: \documentclass{article} \usepackage{listings} \begin{document} \lstset{language=[Objective]C, breakindent=40pt, breaklines} \begin{lstlisting} @interface cla

我想知道如何使用LaTeX的
listings
包格式化目标C代码?我知道该语言支持C(Objective),那么如何在
\lstset
language
选项中设置它呢

谢谢你这样说:

\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}

详细介绍:

虽然这个答案本身并没有回答OP的问题,但我认为其他寻找Objective-C
清单
相关信息的人会偶然发现这个问题

下面是Objective-C2.0、GNU99和ANSI C99的
清单
\lstdefinelanguage。虽然
清单
包含了C和Objective-C的定义,但这些定义适用于较旧的C89标准和Objective-C1.0。下面的版本添加了C99,将GNU99添加到C99,然后将Objective-C(2.0)添加到GNU99

如果你对关键字使用的字体样式与“普通”字体样式不同,你可能只会看到不同

我是下面代码的作者(ANSI C99定义除外,该定义源自
清单
ANSI C并针对ANSI C99进行了修改)。您可以以任何方式使用它,包括将其合并到其他作品中,无需归属或补偿。我特此将其公布于公共领域。(注意:这主要针对那些为雇主工作的人,他们对这些事情非常挑剔,我真的不在乎。)

\lstdefinelanguage[Objective]{C}[GNU99]{C} {morekeywords={@catch、@class、@encode、@end、@finally、@implementation、% @接口、@private、@protected、@protocol、@public、@selector、% @同步、@throw、@try、BOOL、Class、IMP、NO、Nil、SEL、YES、\u cmd、% bycopy,byref,id,in,inout,nil,单向,out,self,super,% %接下来的两行是Objective-C2关键字。 @动态、@package、@property、@synthesis、readwrite、readonly、% 分配、保留、复制、非原子% },% moredirectives={import}% }% \lstdefinelanguage[GNU99]{C}[99]{C} {morekeywords={asm、\uuuuasm\uuuuu、\uuuuu扩展名\uuuuuuuu、typeof、\uuuuuuuuu typeof\uuuuu}% }% \LSTEfineLanguage[99]{C}% {morekeywords={u Bool,_Complex,_virtual,auto,break,case,char,% 常量、continue、default、do、double、else、enum、extern、float、for,% 转到,如果,内联,整数,长,寄存器,限制,返回,短,有符号,% sizeof、static、struct、switch、typedef、union、unsigned、void、volatile,% while},% 敏感,% morecomment=[s]{/*}{*/},% morecomment=[l]/,% morestring=[b],% morestring=[b]',% moredelim=*[指令]\\\,% moredirectives={define,elif,else,endif,error,if,ifdef,ifndef,line,% include,pragma,undf,warning}% }[关键字、注释、字符串、指令]%
这是针对C的。对于目标C,您需要使用
\lstset{language=[Objective]C}
。谢谢-不过我有一个问题,我的源代码很长,并且超出了页面宽度-满满的问题,可以解决吗?thanks@Joachim,我从未使用过Objective C,自从joec把它放在括号中之后,我猜两种语言没有太大区别。我错了!:)。从网络中获取了一些Objective C并修复了my示例。谢谢。@joec,看来您没有查看我发布的链接(或过于简短)。断行在
4.10页边距和线条形状中进行了解释。
。我调整了我的示例。下次请先阅读已经建议的内容。谢谢。我如何在\begin{lstlisting}中使用它?@meccan:只需将其添加到文档开头的\lstset语句后面即可 \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]%