## LaTeX forum ⇒ Text Formatting ⇒ listings gives the same result for every language

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
Omcsesz
Posts: 4
Joined: Mon Feb 19, 2018 10:35 pm

### listings gives the same result for every language

Hello, All!

I get the same results whatever language I choose when using the listings package.
MWE:
\documentclass[a4paper,11pt]{article}  \usepackage[utf8]{inputenc} % utf8\usepackage[T1]{fontenc} \usepackage{listings} \begin{document}	\lstset{language=Java}	\begin{lstlisting}		public byte getSelectedPortAsByte(){			int temp; 			switch(selectedPort){				case "PORTB":					temp= 0x01;					break;				case "PORTC":					temp= 0x02;					break;				default:					temp= 0x00;				break;			}			return (byte)temp;		}	\end{lstlisting}	\end{document}

Do you have any idea regarding this?

Tags:

Stefan Kottwitz
Posts: 9434
Joined: Mon Mar 10, 2008 9:44 pm
Hi Omcsesz,

welcome to the forum!

Can you please try this in the online compiler? Just click on "Open in Overleaf" above your code. Then look at the results with Java, C, TeX. They look different to me. Do you get the same on your PC?

Stefan

Omcsesz
Posts: 4
Joined: Mon Feb 19, 2018 10:35 pm
HI, Stefan!

They are the same in Overleaf as well, mainly. A few words are in bold in Java, which are not in C. But the words are the same color.

Stefan Kottwitz
Posts: 9434
Joined: Mon Mar 10, 2008 9:44 pm
Well, so there is no problem with having the same result for every language.

It seems you would like to have colors. You could load the xcolor package, define colors, and use them, such as by:

\lstset{language = Java,  keywordstyle = \color{myblue},  stringstyle = \color{myred},  commentstyle = \color{mygreen},  ...}

Stefan

Omcsesz
Posts: 4
Joined: Mon Feb 19, 2018 10:35 pm
I see. So no matter what language I choose, it will be black and white by default.

Stefan Kottwitz
Posts: 9434
Joined: Mon Mar 10, 2008 9:44 pm
Language definitions are more about keywords, comments, strings, numbers, delimiters, such as here (internally from listings):

\lst@definelanguage{Java}%  {morekeywords={abstract,boolean,break,byte,case,catch,char,class,%      const,continue,default,do,double,else,extends,false,final,%      finally,float,for,goto,if,implements,import,instanceof,int,%      interface,label,long,native,new,null,package,private,protected,%      public,return,short,static,super,switch,synchronized,this,throw,%      throws,transient,true,try,void,volatile,while},%   sensitive,%   morecomment=[l]//,%   morecomment=[s]{/*}{*/},%   morestring=[b]",%   morestring=[b]',%  }[keywords,comments,strings]%

There's no color coming with a language definition. It's the author/users decision to choose colors. You know, non-color printers could have issues with default colors. Just let your PDF reader search for color in the listings manual to see more specifically about color customization.

Stefan

Johannes_B
Site Moderator
Posts: 4044
Joined: Thu Nov 01, 2012 4:08 pm
If your work is published in a journal and lands at a library and a student makes a hard copy or your article, the poor student won't figure out what different colors on the copy. Same for diagrams and plots. That is way most printed publishing is done black and white.

If you are making a presentation, this is not an issue (unless the professor wants a hard copy of the slides).
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.

Omcsesz
Posts: 4
Joined: Mon Feb 19, 2018 10:35 pm
I see. You are right.