Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

When I have this code with .sty file from here

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

it looks like this: Not styled

And when I add this styles:

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

It looks like this: Styled

Why keywordstyle is not taken into consideration no bold, no red color?

share|improve this question
    
Welcome to TeX.sx! –  Peter Jansson Feb 26 '13 at 11:43
    
There is a language definition, but I do not see that keywords are defined. –  Heiko Oberdiek Feb 26 '13 at 12:05
    
I thought that keywords where taken form the .sty file, but I were overriding that. –  hash Feb 26 '13 at 12:37

1 Answer 1

up vote 2 down vote accepted

You're basically overriding the language definition. Here's a way to do it:

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

I've provided some missing color definition, adjust them.

You can see that using the Go environment applies the settings to the Go language. For boldface you need to choose a monospaced font that has it (Computer Modern Typewriter hasn't).

enter image description here

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.