Text Formattinglistings gives the same result for every language

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

listings gives the same result for every language

Post by Omcsesz »

Hello, All!

I get the same results whatever language I choose when using the listings package.
MWE:

Code: Select all

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

Recommended reading 2024:

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

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

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

Stefan Kottwitz
Site Admin
Posts: 10335
Joined: Mon Mar 10, 2008 9:44 pm

listings gives the same result for every language

Post by Stefan Kottwitz »

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
LaTeX.org admin
Omcsesz
Posts: 4
Joined: Mon Feb 19, 2018 10:35 pm

listings gives the same result for every language

Post by Omcsesz »

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.
User avatar
Stefan Kottwitz
Site Admin
Posts: 10335
Joined: Mon Mar 10, 2008 9:44 pm

listings gives the same result for every language

Post by Stefan Kottwitz »

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:

Code: Select all

\lstset{language = Java,
  keywordstyle = \color{myblue},
  stringstyle = \color{myred},
  commentstyle = \color{mygreen},
  ...
}
Stefan
LaTeX.org admin
Omcsesz
Posts: 4
Joined: Mon Feb 19, 2018 10:35 pm

listings gives the same result for every language

Post by Omcsesz »

I see. So no matter what language I choose, it will be black and white by default.
User avatar
Stefan Kottwitz
Site Admin
Posts: 10335
Joined: Mon Mar 10, 2008 9:44 pm

listings gives the same result for every language

Post by Stefan Kottwitz »

Language definitions are more about keywords, comments, strings, numbers, delimiters, such as here (internally from listings):

Code: Select all

\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
LaTeX.org admin
User avatar
Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

listings gives the same result for every language

Post by Johannes_B »

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

listings gives the same result for every language

Post by Omcsesz »

I see. You are right.
Post Reply