3

I need to insert some code in my latex document, using listings. I create my own set, for a java language

\lstset{
language=Java,
aboveskip=3mm,
belowskip=3mm,
showstringspaces=false,
columns=flexible,
basicstyle={\small},
numbers=none,
numberstyle=\tiny\color{gray},
morekeywords=[1]{@},
keywordstyle=[1]\color{red},
keywordstyle={[2]\color{red}},
keywordstyle=\bfseries\color{purple!40!black},
otherkeywords={String,async,await,Task,var},
keywords=[2]{DatabaseField,DatabaseTable},
commentstyle=\color{ForestGreen},
stringstyle=\color{mauve},
captionpos=b,                    % sets the caption-position to bottom
breaklines=true,
breakatwhitespace=true,
tabsize=3}

As you can see, I've tryed many solution founded online, but neither of this is working for me.

I know that the correct syntax should be :

keywords=[<number>]{<list of keywords>}
keywordstyle={[<number>]<style commands>}

But, is not working, also the bold text in my "normal" keywordstyle is ignored, where's my mistake?

migrated from stackoverflow.com Sep 3 '14 at 12:04

This question came from our site for professional and enthusiast programmers.

3

Your set-up is essentially correct, however you should note that ordinary keywords are of class [1] so the keywordstyle=[1]\color{red} gets overridden by the subsequent keywordstyle. If you wish @ as a keyword to be highlighted in a different color, then you should give it at different number, e.g. [3] and define a corresponding style. However, I would guess that you are after using @ as the annotation prefix and would want to highlight such annotations. This can be done with keywordsprefix=@, but the restriction is that is class [1], so can not be give a different style.

Sample output

\documentclass{article}

\usepackage{listings,xcolor}

\lstset{
language=Java,
aboveskip=3mm,
belowskip=3mm,
showstringspaces=false,
columns=flexible,
basicstyle={\small},
numbers=none,
numberstyle=\tiny\color{gray},
keywordstyle=\bfseries\color{blue},
keywordstyle={[2]\color{green}},
keywordstyle={[3]\color{red}},
otherkeywords={String,async,await,Task,var},
keywords=[2]{DatabaseField,DatabaseTable},
keywords=[3]{@},
commentstyle=\color{ForestGreen},
stringstyle=\color{pink},
captionpos=b, 
breaklines=true,
breakatwhitespace=true,
tabsize=3}

\begin{document}
\begin{lstlisting}
  import java.db;
  @Object.name(field = "a",field = "b")
  @ Object.name(field = "a",field = "b")
  public DatabaseField(java.lang.String name,
                     java.lang.Class type,
                     int length,
                     int keyType)
\end{lstlisting}

\begin{lstlisting}[keywordsprefix=@]
  import java.db;
  @Object.name(field = "a",field = "b")
  @ Object.name(field = "a",field = "b")
  public DatabaseField(java.lang.String name,
                     java.lang.Class type,
                     int length,
                     int keyType)
\end{lstlisting}

\end{document}

I changed some of your colours to make the highlighting clearer.

0

Java is still supported in listings.

Try to comment language = Java. If you want to highlight just two words, like keywords=[2]{DatabaseField,DatabaseTable},

try to modify the style, in this way:

\lstdefinestyle{Java}
{ 
otherkeywords={String,MyClass,etc}, %you can add otherwords to the keywords
literate={
        {DatabaseTable}{{{\color{red}{DatabaseTable}}}}{1}   %DatabaseTable -> red
        {DatabaseField}{{{\color{yellow}{DatabaseField}}}}{1}  %DatabaseField -> yellow
}
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy