为什么不考虑 keywordstyle,没有粗体,没有红色?

为什么不考虑 keywordstyle,没有粗体,没有红色?

当我有此代码和.sty文件时这里

\begin{lstlisting}[language=Go, firstnumber=1, captionpos=b, caption=Golang, label=amb]
// You can edit this code!
// Click here and start typing.
package main

import "fmt"

func main() {
  fmt.Println("Hello, World.")
}
\end{lstlisting}

它看起来像这样: 未设计

当我添加这种样式时:

\lstdefinelanguage{Go}{
basicstyle=\normalfont\ttfamily,
keywordstyle=\color{red}\bfseries,
numbers=left,
numberstyle=\scriptsize,
stepnumber=1,
numbersep=8pt,
showstringspaces=false,
breaklines=true,
frame=lines,
backgroundcolor=\color{background},
literate=
*{0}{{{\color{numb}0}}}{1}
{1}{{{\color{numb}1}}}{1}
{2}{{{\color{numb}2}}}{1}
{3}{{{\color{numb}3}}}{1}
{4}{{{\color{numb}4}}}{1}
{5}{{{\color{numb}5}}}{1}
{6}{{{\color{numb}6}}}{1}
{7}{{{\color{numb}7}}}{1}
{8}{{{\color{numb}8}}}{1}
{9}{{{\color{numb}9}}}{1}
{:}{{{\color{punct}{:}}}}{1}
{,}{{{\color{punct}{,}}}}{1}
{\{}{{{\color{delim}{\{}}}}{1}
{\}}{{{\color{delim}{\}}}}}{1}
{[}{{{\color{delim}{[}}}}{1}
{]}{{{\color{delim}{]}}}}{1},
}

它看起来像这样: 风格化

为什么keywordstyle没有考虑没有粗体,没有红色?

答案1

您基本上是在覆盖语言定义。以下是实现方法:

\documentclass{article}
\usepackage{listings,xcolor}
\colorlet{background}{red!20}
\colorlet{delim}{green!50}
\colorlet{numb}{blue!50}

\lstdefinelanguage{Go}{
  % Keywords as defined in the BNF
  morekeywords=[1]{break,default,func,interface,%
    case,defer,go,map,struct,chan,else,goto,package,%
    switch,const,fallthrough,if,range,type,continue,%
    for,import,return,var,select},
  % Special identifiers, builtin functions
  morekeywords=[2]{make,new,nil,len,cap,copy,complex,%
    real,imag,panic,recover,print,println,iota,close,%
    closed,_,true,false,append,delete},
  % Basic types
  morekeywords=[3]{%
    string,int,uint,uintptr,double,float,byte,%
    int8,int16,int32,int64,int128,%
    uint8,uint16,uint32,uint64,uint128,%
    float32,float64,complex64,complex128,%
    rune},
  % Strings : "toto", 'toto', `toto`
  morestring=[b]{"},
  morestring=[b]{'},
  morestring=[b]{`},
  % Comments : /* comment */ and // comment
  comment=[l]{//},
  morecomment=[s]{/*}{*/},
  % Options
  sensitive=true
}

\lstnewenvironment{Go}[1][]
 {%
  \lstset{
    language=Go,
    basicstyle=\normalfont\ttfamily,
    keywordstyle=\color{red}\bfseries,
    numbers=left,
    numberstyle=\scriptsize,
    stepnumber=1,
    numbersep=8pt,
    showstringspaces=false,
    breaklines=true,
    frame=lines,
    backgroundcolor=\color{background},
    literate=
      *{0}{{{\color{numb}0}}}{1}
       {1}{{{\color{numb}1}}}{1}
       {2}{{{\color{numb}2}}}{1}
       {3}{{{\color{numb}3}}}{1}
       {4}{{{\color{numb}4}}}{1}
       {5}{{{\color{numb}5}}}{1}
       {6}{{{\color{numb}6}}}{1}
       {7}{{{\color{numb}7}}}{1}
       {8}{{{\color{numb}8}}}{1}
       {9}{{{\color{numb}9}}}{1}
       {:}{{{\color{punct}{:}}}}{1}
       {,}{{{\color{punct}{,}}}}{1}
       {\{}{{{\color{delim}{\{}}}}{1}
       {\}}{{{\color{delim}{\}}}}}{1}
       {[}{{{\color{delim}{[}}}}{1}
       {]}{{{\color{delim}{]}}}}{1},
    #1}
 }
 {}



\begin{document}

\begin{lstlisting}[language=Go, firstnumber=1, captionpos=b, caption=Golang, label=amb]
// You can edit this code!
// Click here and start typing.
package main

import "fmt"

func main() {
  fmt.Println("Hello, World.")
}
\end{lstlisting}

\begin{Go}[firstnumber=1, captionpos=b, caption=Golang, label=ambgood]
// You can edit this code!
// Click here and start typing.
package main

import "fmt"

func main() {
  fmt.Println("Hello, World.")
}
\end{Go}

\end{document}

我提供了一些缺失的颜色定义,请调整它们。

您可以看到,使用Go环境将设置应用于 Go 语言。对于粗体,您需要选择具有该字体的等宽字体(Computer Modern Typewriter 没有)。

在此处输入图片描述

相关内容