LaTeX forum ⇒ Text Formattinglistings 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

Postby Omcsesz » Sat Feb 24, 2018 2:26 pm

Hello, All!

I get the same results whatever language I choose when using the listings package.
MWE:
  1. \documentclass[a4paper,11pt]{article}
  2.  
  3. \usepackage[utf8]{inputenc} % utf8
  4. \usepackage[T1]{fontenc}
  5. \usepackage{listings}
  6.  
  7. \begin{document}
  8. \lstset{language=Java}
  9. \begin{lstlisting}
  10. public byte getSelectedPortAsByte(){
  11. int temp;
  12.  
  13. switch(selectedPort){
  14. case "PORTB":
  15. temp= 0x01;
  16. break;
  17. case "PORTC":
  18. temp= 0x02;
  19. break;
  20. default:
  21. temp= 0x00;
  22. break;
  23. }
  24. return (byte)temp;
  25. }
  26. \end{lstlisting}
  27. \end{document}


Do you have any idea regarding this?

Tags:

User avatar
Stefan Kottwitz
Site Admin
Posts: 9434
Joined: Mon Mar 10, 2008 9:44 pm

Postby Stefan Kottwitz » Sat Feb 24, 2018 2:30 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
Site admin

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

Postby Omcsesz » Sat Feb 24, 2018 7:48 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.

User avatar
Stefan Kottwitz
Site Admin
Posts: 9434
Joined: Mon Mar 10, 2008 9:44 pm

Postby Stefan Kottwitz » Sun Feb 25, 2018 2:40 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:

  1. \lstset{language = Java,
  2. keywordstyle = \color{myblue},
  3. stringstyle = \color{myred},
  4. commentstyle = \color{mygreen},
  5. ...
  6. }


Stefan
Site admin

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

Postby Omcsesz » Sun Feb 25, 2018 2:52 pm

I see. So no matter what language I choose, it will be black and white by default.

User avatar
Stefan Kottwitz
Site Admin
Posts: 9434
Joined: Mon Mar 10, 2008 9:44 pm

Postby Stefan Kottwitz » Sun Feb 25, 2018 3:32 pm

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

  1. \lst@definelanguage{Java}%
  2. {morekeywords={abstract,boolean,break,byte,case,catch,char,class,%
  3. const,continue,default,do,double,else,extends,false,final,%
  4. finally,float,for,goto,if,implements,import,instanceof,int,%
  5. interface,label,long,native,new,null,package,private,protected,%
  6. public,return,short,static,super,switch,synchronized,this,throw,%
  7. throws,transient,true,try,void,volatile,while},%
  8. sensitive,%
  9. morecomment=[l]//,%
  10. morecomment=[s]{/*}{*/},%
  11. morestring=[b]",%
  12. morestring=[b]',%
  13. }[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
Site admin

User avatar
Johannes_B
Site Moderator
Posts: 4044
Joined: Thu Nov 01, 2012 4:08 pm

Postby Johannes_B » Sun Feb 25, 2018 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

Postby Omcsesz » Fri Mar 02, 2018 1:14 am

I see. You are right.


Return to “Text Formatting”

Who is online

Users browsing this forum: No registered users and 1 guest