使用包“listings”和“pythonhighlight”在 LaTeX-Dokument-PDF 的 Python-Code-Section 中等号后的空格

使用包“listings”和“pythonhighlight”在 LaTeX-Dokument-PDF 的 Python-Code-Section 中等号后的空格

有人知道如何设置包“listings”和“pythonhighlight”以在“=”之前(这已经有效)和之后显示一个空白(也许还有其他操作符号以供将来使用)?

我有一个附录部分作为 .tex 文件:

\subsection{DALAtomObjectService}
\label{app:DALAtomObjectService} % chktex 24
Beispiel für die ``Object\_Service\_Atom''-Klasse im Data-Access-Layer (Ausschnitt):

\begin{python}
    class Object_Service_Atom(Object_Service_Abstract):

    def __init__(self):
        self.url = ATOM_BINDING_URL        

    def createDocument(self, repositoryId, properties, **kwargs):
        service = inspect.stack()[0][3]
        kwargs['service'] = service        
        url = self.url + "/" + repositoryId + "/children"
        data = Atom_Properties_Post(
            properties['name'], properties['objectTypeId']).getAtomProperty()
        params = {}
        if 'folderId' in kwargs:
            params['id'] = kwargs['folderId']
        create_response = RS().post(url, params=params, data=data, **kwargs)
        return create_response    
\end{python}

在创建的 PDF 中显示如下:

    1 class Object_Service_Atom(Object_Service_Abstract):
    2
    3    def __init__(self):
    4       self.url =ATOM_BINDING_URL
    5
    6    def createDocument(self, repositoryId, properties, **kwargs):
    7       service =inspect.stack()[0][3]
    8       kwargs[’service’] =service
    9       url =self.url +"/" + repositoryId +"/children"
    10      data =Atom_Properties_Post(
    11         properties[’name’], properties[’objectTypeId’]).getAtomProperty()
    12      params ={}
    13      if ’folderId’ in kwargs:
    14         params[’id’] =kwargs[’folderId’]
    15      create_response =RS().post(url, params=params, data=data, **kwargs)
    16      return create_response```

但我不想要这样的任务

self.url =ATOM_BINDING_URL

但只是这样:

自身.url = ATOM_BINDING_URL

在“Allgemein”(常规)部分的 Packages.tex 文件中,我对“listings”和“pythonhighlight”包进行了以下设置:

% zum Einbinden von Programmcode -----------------------------------------------
\usepackage[utf8]{inputenc}
\usepackage{listings}
\usepackage{pythonhighlight}
\usepackage{xcolor}
\definecolor{hellgelb}{rgb}{1,1,0.9}
\definecolor{colKeys}{rgb}{0,0,1}
\definecolor{colIdentifier}{rgb}{0,0,0}
\definecolor{colComments}{rgb}{0,0.5,0}
\definecolor{colString}{rgb}{1,0,0}
\lstset{
    float=hbp,
    basicstyle=\footnotesize,
    identifierstyle=\color{colIdentifier},
    keywordstyle=\color{colKeys},
    stringstyle=\color{colString},
    commentstyle=\color{colComments},
    backgroundcolor=\color{hellgelb},
    columns=flexible,
    tabsize=2,
    frame=single,
    extendedchars=true,
    showspaces=false,
    showstringspaces=false,
    numbers=left,
    numberstyle=\tiny,
    breaklines=true,
    breakautoindent=true,
    captionpos=b,
    showstringspaces=false
}
\lstdefinelanguage{cs}{
    sensitive=false,
    morecomment=[l]{//},
    morecomment=[s]{/*}{*/},
    morestring=[b]", % chktex 18
    morekeywords={
        abstract,event,new,struct,as,explicit,null,switch
        base,extern,object,this,bool,false,operator,throw,
        break,finally,out,true,byte,fixed,override,try,
        case,float,params,typeof,catch,for,private,uint,
        char,foreach,protected,ulong,checked,goto,public,unchecked,
        class,if,readonly,unsafe,const,implicit,ref,ushort,
        continue,in,return,using,decimal,int,sbyte,virtual,
        default,interface,sealed,volatile,delegate,internal,short,void,
        do,is,sizeof,while,double,lock,stackalloc,
        else,long,static,enum,namespace,string},
}
\lstdefinelanguage{natural}{
    sensitive=false,
    morecomment=[l]{/*},
    morestring=[b]", % chktex 18
    morestring=[b]',
    alsodigit={-,*},
    morekeywords={
        DEFINE,DATA,LOCAL,END-DEFINE,WRITE,CALLNAT,PARAMETER,USING,
        IF,NOT,END-IF,ON,*ERROR-NR,ERROR,END-ERROR,ESCAPE,ROUTINE,
        PERFORM,SUBROUTINE,END-SUBROUTINE,CONST,END-FOR,END,FOR,RESIZE,
        ARRAY,TO,BY,VALUE,RESET,COMPRESS,INTO,EQ},
}
\lstdefinelanguage{php}{
    sensitive=false,
    morecomment=[l]{/*},
    morestring=[b]", % chktex 18
    morestring=[b]',
    alsodigit={-,*},
    morekeywords={
        abstract,and,array,as,break,case,catch,cfunction,class,clone,const,
        continue,declare,default,do,else,elseif,enddeclare,endfor,endforeach,
        endif,endswitch,endwhile,extends,final,for,foreach,function,global,
        goto,if,implements,interface,instanceof,namespace,new,old_function,or,
        private,protected,public,static,switch,throw,try,use,var,while,xor
        die,echo,empty,exit,eval,include,include_once,isset,list,require,
        require_once,return,print,unset},
}
\lstset{language=python,texcl=true}
\lstset{literate=
  {á}{{\'a}}1 {é}{{\'e}}1 {í}{{\'i}}1 {ó}{{\'o}}1 {ú}{{\'u}}1 % chktex 7
  {Á}{{\'A}}1 {É}{{\'E}}1 {Í}{{\'I}}1 {Ó}{{\'O}}1 {Ú}{{\'U}}1
  {à}{{\`a}}1 {è}{{\`e}}1 {ì}{{\`i}}1 {ò}{{\`o}}1 {ù}{{\`u}}1 % chktex 7
  {À}{{\`A}}1 {È}{{\'E}}1 {Ì}{{\`I}}1 {Ò}{{\`O}}1 {Ù}{{\`U}}1
  {ä}{{\"a}}1 {ë}{{\"e}}1 {ï}{{\"i}}1 {ö}{{\"o}}1 {ü}{{\"u}}1 % chktex 7
  {Ä}{{\"A}}1 {Ë}{{\"E}}1 {Ï}{{\"I}}1 {Ö}{{\"O}}1 {Ü}{{\"U}}1
  {â}{{\^a}}1 {ê}{{\^e}}1 {î}{{\^i}}1 {ô}{{\^o}}1 {û}{{\^u}}1 % chktex 7
  {Â}{{\^A}}1 {Ê}{{\^E}}1 {Î}{{\^I}}1 {Ô}{{\^O}}1 {Û}{{\^U}}1
  {œ}{{\oe}}1 {Œ}{{\OE}}1 {æ}{{\ae}}1 {Æ}{{\AE}}1 {ß}{{\ss}}1
  {ç}{{\c c}}1 {Ç}{{\c C}}1 {ø}{{\o}}1 {å}{{\r a}}1 {Å}{{\r A}}1
  {€}{{\EUR}}1 {£}{{\pounds}}1
}

我正在使用模板,只有最后两个“\lsets”和“showstringspaces=false”是我后来添加的。

可能有一个简单的解决方案,但我还没有设法用 Google 找到符合我要求的东西。LaTeX 对我来说很新,否则我可能会更有目的地搜索。

谢谢您提供示例或提示应查找的位置!

答案1

要恢复您的空间,您可以添加keepspaces或使用columns=fixed而不是columns=flexible

来自listings 手动的

2.10 固定柱和柔性柱
读者首先注意到的——除了关键字等的不同样式——是列对齐。
[...]
目前提供四种列格式:固定、灵活、空间灵活和完全灵活。
[...]
从抽象上讲,我们可以说:固定列格式破坏了字体设计师想要的间距,而灵活格式破坏了程序员(可能)想要的列对齐。

keepspaces=true告诉包不要删除空格来修复列对齐,并始终将制表符转换为空格。

相关内容