GeneralDefining new languages for listings package

LaTeX specific issues not fitting into one of the other forums of this category.
ebigunso
Posts: 9
Joined: Sat Jul 29, 2017 5:04 pm

Defining new languages for listings package

Post by ebigunso »

I'm now working on a new language definition for use in the listings package, and I have a question.

In my coding language, there are cases where coloring for the same word changes according to what comes next. Such as "boolean" comes in orange when it is written alone, but in blue if it is written "boolean()" as a function name.
Is there any way to define this properly, so that in this case "boolean" would be blue only when "(" comes after the word?

Example of code:
Image
Last edited by ebigunso on Mon Aug 07, 2017 1:02 pm, edited 1 time in total.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

thomasb
Posts: 134
Joined: Thu Aug 03, 2017 10:54 am

Defining new languages for listings package

Post by thomasb »

In LaTeX ? You have probably mistaken the forum with an IDE one...
User avatar
Stefan Kottwitz
Site Admin
Posts: 10344
Joined: Mon Mar 10, 2008 9:44 pm

Defining new languages for listings package

Post by Stefan Kottwitz »

Hi ebigunso,

welcome to the forum!

Maybe you did not get an answer because you did not post code yet. Posting a Infominimal working example is usually a guarantee for a solution. Without code, every reader has to create some code by himself to actually test something (just because you did not show your code), sometimes nobody does this additional work.

I think it can be done by morekeywords={boolean(}.

Thomas, yes, we can do code highlighting for languages in LaTeX. By the way, it's great that you look at older questions and add an answer. i sometimes find a years old question and add an answer for future readers. It may be hard though, since many remaining unanswered questions are unclear.

Stefan
LaTeX.org admin
ebigunso
Posts: 9
Joined: Sat Jul 29, 2017 5:04 pm

Defining new languages for listings package

Post by ebigunso »

Stefan Kottwitz wrote:I think it can be done by morekeywords={boolean(}.
Thank you for the reply. That solution would highlight the left parenthesis as well I assume. I want the result to look like what I have shown in the screenshot, so that wouldn't quite fit in with what I'm looking for.

Also, This is what I have at this point.

Code: Select all

\documentclass{article}

\usepackage{listings}

%Define Colors
\definecolor{gray}{HTML}{666666}		%#666666
\definecolor{lightbule}{HTML}{006699}		%#006699
\definecolor{lightgreen}{HTML}{669900}		%#669900
\definecolor{bluegreen}{HTML}{33997e}		%#33997e
\definecolor{magenta}{HTML}{d94a7a}		%#d94a7a
\definecolor{orange}{HTML}{e2661a}		%#e2661a
\definecolor{purple}{HTML}{7d4793}		%#7d4793
\definecolor{green}{HTML}{718a62}		%#718a62

\lstdefinelanguage{Processing}{
  %keyword1&2&6
  morekeywords = [1]{abstract, class, continue, default, enum, extends, false, final, finally, implements, import, instanceof, interface, native, new, null, package, private, protected, public, static, strictfp, throws, transient, true, void, volatile, length, assert, case, return, super, this, throw},
  %keyword3
  morekeywords = [2]{catch, do, for, if, else, switch, synchronized, while, try},
  %keyword4
  morekeywords = [3]{width, height, pixelHight, displayHeight, displayWidth, focused, frameCount, frameRate, key, keyCode, keyPressed, mouseButton, mousePressed, mouseX, mouseY, pixels, pixelWidth, pmouseX, pmouseY},
  %keyword5
  morekeywords = [4]{Array, ArrayList, Boolean, Byte, BufferedReader, Character, Class, Double, Float, Integer, HashMap, PrintWriter, String, StringBuffer, StringBuilder, Thread, boolean, byte, char, color, double, float, int, long, short, FloatDict, FloatList, IntDict, IntList, JSONArray, JSONObject, PFont, PGraphics, PImage, PShader, PShape, PVector, StringDict, StringList, Table, TableRow, XML},
  keywordstyle = [1]\color{bluegreen},
  keywordstyle = [2]\color{lightgreen},
  keywordstyle = [3]\color{magenta},
  keywordstyle = [4]\color{orange},
  sensitive = true,
  morecomment = [l]{//},
  morecomment = [s]{/*}{*/},
  morecomment = [s]{/**}{*/},
  commentstyle = \color{gray},
  morestring = [b]",
  morestring = [b]',
  stringstyle = \color{purple}
}
\lstset{
  language={Processing},
  basicstyle={\small},
  identifierstyle={\small},
  commentstyle={\small\itshape},
  keywordstyle={\small\bfseries},
  ndkeywordstyle={\small},
  stringstyle={\small\ttfamily},
  frame={tb},
  breaklines=true,
  columns=[l]{fullflexible},
  numbers=left,
  xrightmargin=0zw,
  xleftmargin=3zw,
  numberstyle={\scriptsize},
  stepnumber=1,
  numbersep=1zw,
  lineskip=-0.5ex
}
User avatar
Stefan Kottwitz
Site Admin
Posts: 10344
Joined: Mon Mar 10, 2008 9:44 pm

Defining new languages for listings package

Post by Stefan Kottwitz »

Nice to see, but that cannot be tested.

I made somme effort and added

Code: Select all

\begin{document}
\begin{lstlisting}
boolean
boolan(1)
\end{lstlisting}
\end{document}
and loaded the xcolor package for the colors, but I got

Code: Select all

! Illegal unit of measure (pt inserted).
<to be read again> 
                   z
l.59 \begin{lstlisting}
Then I stopped. If you post unusable code, that any reader has to complete to fix first, it's hard to help.

Stefan
LaTeX.org admin
thomasb
Posts: 134
Joined: Thu Aug 03, 2017 10:54 am

Defining new languages for listings package

Post by thomasb »

Stefan Kottwitz wrote: Thomas, yes, we can do code highlighting for languages in LaTeX.
Seriouly ! Ebigunso, you need to post a working example so I can see that...
Stefan Kottwitz wrote: Then I stopped. If you post unusable code, that any reader has to complete to fix first, it's hard to help.
That's what I always said. :evil:
Last edited by thomasb on Mon Aug 07, 2017 2:17 am, edited 1 time in total.
ebigunso
Posts: 9
Joined: Sat Jul 29, 2017 5:04 pm

Defining new languages for listings package

Post by ebigunso »

I'm sorry for posting a non-working code. It's fixed now.
NOTE: Why Overleaf was having issues, was that the length unit "zw" was not useable on non-pLaTeX environments(default English environment for Overleaf). I failed to notice this issue since my local environment is in pLaTeX(commonly used for Japanese files), and in there it worked just fine. It takes quite a hassle to use pLaTeX on Overleaf so I just fixed the units. Also, Overleaf seems to not color things as defined, so probably better to copy the code on to your local TeX editor for testing.

Also I integrated the solution Stefan has proposed. This gave a peculiar result, with both "boolean" and "boolean(1)" having the "boolean" part in orange rather than the "boolean(1)" having the "boolean(" part in blue.

I tried around checking if this was due to order of definition, or numbering, but no. Changing the order of definition, or the keyword numbers did not change the result. It seems the shorter keyword gets noticed first, and then once that is done it doesn't recognize any other keyword definition.

I don't know how I could get around this...

Screenshot of working example:
Image

My fixed code:

Code: Select all

\documentclass{article}

\usepackage{listings}
\usepackage[dvipdfmx]{color}

%Define Colors
\definecolor{gray}{RGB}{102,102,102}		%#666666
\definecolor{lightblue}{RGB}{0,102,153}		%#006699
\definecolor{lightgreen}{RGB}{102,153,0}	%#669900
\definecolor{bluegreen}{RGB}{51,153,126}	%#33997e
\definecolor{magenta}{RGB}{217,74,122}		%#d94a7a
\definecolor{orange}{RGB}{226,102,26}		%#e2661a
\definecolor{purple}{RGB}{125,71,147}		%#7d4793
\definecolor{green}{RGB}{113,138,98}		%#718a62

\lstdefinelanguage{Processing}{
  %keyword1&2&6
  morekeywords = [1]{abstract, class, continue, default, enum, extends, false, final, finally, implements, import, instanceof, interface, native, new, null, package, private, protected, public, static, strictfp, throws, transient, true, void, volatile, length, assert, case, return, super, this, throw},
  %keyword3
  morekeywords = [2]{catch, do, for, if, else, switch, synchronized, while, try},
  %keyword4
  morekeywords = [3]{width, height, pixelHight, displayHeight, displayWidth, focused, frameCount, frameRate, key, keyCode, keyPressed, mouseButton, mousePressed, mouseX, mouseY, pixels, pixelWidth, pmouseX, pmouseY},
  %keyword5
  morekeywords = [4]{Array, ArrayList, Boolean, Byte, BufferedReader, Character, Class, Double, Float, Integer, HashMap, PrintWriter, String, StringBuffer, StringBuilder, Thread, boolean, byte, char, color, double, float, int, long, short, FloatDict, FloatList, IntDict, IntList, JSONArray, JSONObject, PFont, PGraphics, PImage, PShader, PShape, PVector, StringDict, StringList, Table, TableRow, XML},
  %function1
  morekeywords = [5]{boolean(},
  keywordstyle = [1]\color{bluegreen},
  keywordstyle = [2]\color{lightgreen},
  keywordstyle = [3]\color{magenta},
  keywordstyle = [4]\color{orange},
  keywordstyle = [5]\color{lightblue},
  sensitive = true,
  morecomment = [l]{//},
  morecomment = [s]{/*}{*/},
  morecomment = [s]{/**}{*/},
  commentstyle = \color{gray},
  morestring = [b]",
  morestring = [b]',
  stringstyle = \color{purple}
}
\lstset{
  language={Processing},
  basicstyle={\small},
  identifierstyle={\small},
  commentstyle={\small\itshape},
  keywordstyle={\small\bfseries},
  ndkeywordstyle={\small},
  stringstyle={\small\ttfamily},
  frame={tb},
  breaklines=true,
  columns=[l]{fullflexible},
  numbers=left,
  xrightmargin=0em,
  xleftmargin=3em,
  numberstyle={\scriptsize},
  stepnumber=1,
  numbersep=1em,
  lineskip=-0.5ex,
}

\begin{document}
\begin{lstlisting}
boolean;
boolean(1);
\end{lstlisting}
\end{document}
Last edited by ebigunso on Mon Aug 07, 2017 1:01 pm, edited 1 time in total.
thomasb
Posts: 134
Joined: Thu Aug 03, 2017 10:54 am

Defining new languages for listings package

Post by thomasb »

Do you have Array in the right color ?
ebigunso
Posts: 9
Joined: Sat Jul 29, 2017 5:04 pm

Defining new languages for listings package

Post by ebigunso »

thomasb wrote:Do you have Array in the right color ?
Image
Yes, on my local TeX editor.
Image
But not on Overleaf.
thomasb
Posts: 134
Joined: Thu Aug 03, 2017 10:54 am

Defining new languages for listings package

Post by thomasb »

The Array color is not bluegreen already... Try without the

Code: Select all

morekeywords = [5]{boolean(},
line, maybe it's the unclosed parenthesis that causes problem... (Here, everything is black, the pdf has no colors)
Post Reply