Graphics, Figures & TablesWhat is wrong with colors in these code listings ?

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

What is wrong with colors in these code listings ?

Post by Cham »

I'm having trouble to isolate the color bug on this simple code :

Code: Select all

\documentclass{book}
\usepackage[dvipsnames,table]{xcolor}
\usepackage{listings}

\begin{document}

%%%% Code 1 :
\lstset{
	language=mathematica,
	emph={[1]
		x, y
	},emphstyle={[1]\color{green}},
	emph={[2]
		u, v
	},emphstyle={[2]\color{blue}},
	emph={[3]
		a, b
	},emphstyle={[3]\color{red}}
	}

\begin{lstlisting}[caption={Test},label={test}]
 x, y,
 u, v,
 a, b,
\end{lstlisting}

%%%% Code 2 :
\lstset{
	language=mathematica,
	emph={[1]
		u, v, a, b
	},emphstyle={[1]\color{green}},
	emph={[2]
		x, y
	},emphstyle={[2]\color{blue}}
	}

\begin{lstlisting}[caption={Test},label={test}]
 u, v, a, b,
 x, y
\end{lstlisting}

\end{document}
The listing 1 is showing okay. The listing 2 have "a" and "b" shown in red, while they should be in green instead. Apparently, they are getting the red color defined in listing 1. Why ? All other symbols are properly shown with the right color.

What is wrong with this code ? :?:

Here's a preview of what this code is doing :
colors.jpg
colors.jpg (8.66 KiB) Viewed 7317 times
By the way, this new forum interface is HORRIBLE ! What was wrong with the previous interface ? it was perfect ! Why change something which was already great !?

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

Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

What is wrong with colors in these code listings ?

Post by Johannes_B »

Remove them from the class.

Code: Select all

\documentclass{book}
\usepackage[dvipsnames,table]{xcolor}
\usepackage{listings}

\begin{document}

%%%% Code 1 :
\lstset{
	language=mathematica,
	emph={[1]
		x, y
	},emphstyle={[1]\color{green}},
	emph={[2]
		u, v
	},emphstyle={[2]\color{blue}},
	emph={[3]
		a, b
	},emphstyle={[3]\color{red}}
}

\begin{lstlisting}[caption={Test},label={test}]
x, y,
u, v,
a, b,
\end{lstlisting}

%%%% Code 2 :
\lstset{
	language=mathematica,
	emph={[1]u,v,a,b},emphstyle={[1]\color{green}},
	emph={[2]x,y},emphstyle={[2]\color{blue}},
	emph={[3]},
}

\begin{lstlisting}[caption={Test},label={test}]
u, v, a, b,
x, y
\end{lstlisting}

\end{document}
Concerning the update, you should probably open a new question in the community talk subforum.
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

Re: What is wrong with colors in these code listings ?

Post by Cham »

It appears to work. Thanks !

But what the ... !? This is weird.
User avatar
Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

Re: What is wrong with colors in these code listings ?

Post by Johannes_B »

Why weird? They are stiil defined to be in that class. And that class is defined to print red.
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

Re: What is wrong with colors in these code listings ?

Post by Cham »

I believed that each listing was self-contained, i.e. independant from all previous listings ! Apparently, I was wrong. The previous listing is overwriting the new listing parameters. This is weird in some way.
User avatar
Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

What is wrong with colors in these code listings ?

Post by Johannes_B »

No, it is not, why would it be? It is set outside of a group. Are you looking for groups? Why don't you know about groups? You are a LaTeX user for quite some time.

Code: Select all

\documentclass{book}
\usepackage[dvipsnames,table]{xcolor}
\usepackage{listings}

\begin{document}

\begingroup
%%%% Code 1 :
\lstset{
	language=mathematica,
	emph={[1] x, y },emphstyle={[1]\color{green}},
	emph={[2] u, v },emphstyle={[2]\color{blue}},
	emph={[3] a, b },emphstyle={[3]\color{red}}
}

\begin{lstlisting}[caption={Test},label={test}]
x, y,
u, v,
a, b,
\end{lstlisting}
\endgroup

%%%% Code 2 :
\lstset{
	language=mathematica,
	emph={[1]u,v,a,b},emphstyle={[1]\color{green}},
	emph={[2]x,y},emphstyle={[2]\color{blue}},
}

\begin{lstlisting}[caption={Test},label={test}]
u, v, a, b,
x, y
\end{lstlisting}

\end{document}
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

Re: What is wrong with colors in these code listings ?

Post by Cham »

No, I didn't knew about groups. Beeing a long time LaTeX user doesn't imply we should know about all the LaTeX options and environments. There are so much commands and options, and since we tend to use the same commands for our work...

What is the purpose of groups ? Why should I know about it ?
User avatar
Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

What is wrong with colors in these code listings ?

Post by Johannes_B »

Grouping is one of the fundamentals of both LaTeX and TeX. Think of it like a closed fence, or a cage. Or Vegas. What happens in Vegas stays in Vegas. That is how LaTeX environments work.

Code: Select all

\documentclass{article}
\begin{document}
\tiny Here is text 
\begingroup
\newcommand{\text}{No wombat can be seen here}
\huge \text
\endgroup
text and some more\text
\end{document}
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

Re: What is wrong with colors in these code listings ?

Post by Cham »

Then it is weird that I never heard of this before. I have read many documentations about using LaTeX to create scientific documents, and I don't remember having seen this environment before.

Thanks for the info. My color problem is solved by "removing" the elements from the class, as you said.
Post Reply