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

发布于 2024-08-28 15:14:28 字数 158 浏览 4 评论 0原文

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

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

画中仙 2024-09-04 15:14:29

像这样:

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

详细介绍:http://mirror.hmc.edu /ctan/macros/latex/contrib/listings/listings.pdf

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

网名女生简单气质 2024-09-04 15:14:29

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

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

如果您对关键字使用的字体样式与“正常”字体样式不同,您可能只会看到差异。

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

\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]%

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]%
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文