【问题标题】:Formatting Objective C code using listing in LaTeX使用 LaTeX 中的列表格式化 Objective C 代码
【发布时间】:2010-04-09 13:11:00
【问题描述】:

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

谢谢

【问题讨论】:

    标签: latex listings


    【解决方案1】:

    像这样:

    \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

    【讨论】:

    • 那是针对 C 的。对于 Objective C,您需要使用 \lstset{language=[Objective]C}
    • 谢谢 - 我有一个问题,我的源很长,并且超出了页面的宽度 - 溢出问题,可以解决吗?谢谢
    • @Joachim,我从来没有使用过Objective C,因为joec把它放在括号之间,我猜这两种语言并没有太大区别。我错了! :)。从网上抓取了一些 Objective C 并修复了我的示例。谢谢。
    • @joec,看来您没有查看我发布的链接(或过于简短)。 4.10 Margins and line shape 中解释了换行符。我调整了我的例子。下次请先阅读已经建议的内容。谢谢。
    【解决方案2】:

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

    下面是用于 Objective-C 2.0、GNU99 和 ANSI C99 的 listings \lstdefinelanguage。虽然listings 包括对 C 和 Objective-C 的定义,但这些定义是针对旧的 C89 标准和 Objective-C 1.0 的。下面的版本添加了C99,在C99中添加了GNU99,然后在GNU99中添加了Objective-C(2.0)。

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

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

    \lstdefinelanguage[目标]{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,% % 接下来的两行是 Objective-C 2 的关键字。 @dynamic,@package,@property,@synthesize,readwrite,readonly,% 分配、保留、复制、非原子% },% 更多指令={导入}% }% \lstdefinelanguage[GNU99]{C}[99]{C} {morekeywords={asm,__asm__,__extension__,typeof,__typeof__}% }% \lstdefinelanguage[99]{C}% {morekeywords={_Bool,_Complex,_Imaginary,auto,break,case,char,% const,继续,默认,do,double,else,enum,extern,float,for,% goto,if,inline,int,long,register,restrict,return,short,signed,% sizeof、静态、结构、开关、typedef、联合、无符号、无效、易失性、% 尽管},% 敏感的,% 更多评论=[s]{/*}{*/},% 更多评论=[l]//,% 更多字符串=[b]",% 更多字符串=[b]',% moredelim=*[指令]\#,% moredirectives={define,elif,else,endif,error,if,ifdef,ifndef,line,% 包括,编译指示,undef,警告}% }[关键字、cmets、字符串、指令]%

    【讨论】:

    • 我如何在 \begin{lstlisting} 中使用它?
    • @meccan: 只需将它添加到文档开头的\lstset 语句后面
    猜你喜欢
    • 2011-03-24
    • 2014-07-02
    • 2011-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多