# Listings package handles keywords inconsistently

I am using the listings package with pdfLaTeX and I notice that it handles keywords inconsistently. If I run the MWE at the bottom of my post, I get this:

As you can see, "For" is handled differntly in lines 2 and 10. Also, "Dim" is treated as a keyword. But in \lstset I said \deletekeyword{Dim}.

But it doesn't stop there: If I completely remove line 12 I get the following output:

Suddenly, "STR" is treated like a keyword (line 14); "Next" and "Do" aren't anymore (lines 15 and 19).

How can this be and how can I fix it?

%% MWE
\documentclass[12pt]{article}
\usepackage{listings,tikz}
\usetikzlibrary{babel}

\def\arr{
\begin{tikzpicture}[very thick,x=1em,y=1em,baseline={(0,-0.35em)}]
\draw [->](-0.4,0) -- (0.4,0);
\end{tikzpicture}
}
\def\arrend{
\begin{tikzpicture}[very thick,x=1em,y=1em,baseline={(0,-0.3em)}]
\draw[<-] (-0.25,0) -- (0.25,0) -- (0.25,0.3);
\end{tikzpicture}
}
\def\sp{\bfseries\textvisiblespace}
\lstset{
language=[Visual]Basic,
keywordstyle=\bfseries\underbar,
keepspaces=true,
frame=tBlR,
rulesepcolor=\color[RGB]{204,0,0},
numbers=left,
tabsize=2,
breaklines=true,
breakatwhitespace=true,
showstringspaces=false,
morekeywords={To,LpWhile,IfEnd,Step},
deletekeywords={Dim},
literate={->}{\arr}1 {;}{\arrend}1{=/}{$\neq$}1
}
\begin{document}

\begin{lstlisting}[firstline=1,caption={\texttt{SCORE} (Set Parameters)\label{score}},escapechar=!]
{6,16} -> Dim Mat Z;
For 1 -> I To 6;
-1!\textsc{e}!7 -> Mat Z[I,16];
Next;
ClrText;
"Boolean(show)"? -> D";
"Amount"? -> F;
4 -> X;
4 -> Y;
For 1 -> Mat Z[I,16];
ClrText;
Locate 1,2,"GIVE NAMES!\sp\sp!?:;
Locate 13,2,S;
Locate 3,4,"(!\sp\sp\sp\sp\sp\sp!)";
Prog "STR IN";
Next;
0 -> H;
7 -> X;
3 -> Y;
Do;
\end{lstlisting}
\end{document}


Your quotation marks were unbalanced in the row #6 which upset the parser. I removed the extra " after the D. This was also true for "GIVE NAMES"

EDIT the Dim problem was instead that you had to delete the keyword from list 4 for Basic.

    %% MWE
\documentclass[12pt]{article}
\usepackage{listings,tikz}
\usetikzlibrary{babel}

\def\arr{
\begin{tikzpicture}[very thick,x=1em,y=1em,baseline={(0,-0.35em)}]
\draw [->](-0.4,0) -- (0.4,0);
\end{tikzpicture}
}
\def\arrend{
\begin{tikzpicture}[very thick,x=1em,y=1em,baseline={(0,-0.3em)}]
\draw[<-] (-0.25,0) -- (0.25,0) -- (0.25,0.3);
\end{tikzpicture}
}
\def\sp{\bfseries\textvisiblespace}
\lstset{
language=[Visual]Basic,
keywordstyle=\bfseries\underbar,
keepspaces=true,
frame=tBlR,
rulesepcolor=\color[RGB]{204,0,0},
numbers=left,
tabsize=2,
breaklines=true,
breakatwhitespace=true,
showstringspaces=false,
morekeywords={To,LpWhile,IfEnd,Step},
deletekeywords=[4]{Dim},
literate={->}{\arr}1 {;}{\arrend}1{=/}{$\neq$}1
}
\begin{document}

\begin{lstlisting}[firstline=1,caption={\texttt{SCORE} (Set Parameters)\label{score}},escapechar=!]
{6,16} -> Dim Mat Z;
For 1 -> I To 6;
-1!\textsc{e}!7 -> Mat Z[I,16];
Next;
ClrText;
"Boolean(show)"? -> D;
"Amount"? -> F;
4 -> X;
4 -> Y;
For 1 -> Mat Z[I,16];
ClrText;
Locate 1,2,"GIVE NAMES"!\sp\sp!?:;
Locate 13,2,S;
Locate 3,4,"(!\sp\sp\sp\sp\sp\sp!)";
Prog "STR IN";
Next;
0 -> H;
7 -> X;
3 -> Y;
Do;
\end{lstlisting}
\end{document}


From a programming point of view I also am unsure about line #10 since the for syntax is different from the first for (i.e. for 1->I to 6 the first, but the I to is missing in the second)

EDIT2

Regarding the list 4 here are some remarks:

1. the correct usage of deletekeywords is deletekeywords=[number]{keywordlist} where the number indicates in which one of the keyword set for the language definition the specific keywords are located. I think you'd better check out the listings package documentation for more insight about it

2. Ok but how do I find where the keyword is defined? Well there is this complementary file lstdrvrs which contains the language definitions included in listings. You can obtain its documentation from ctan, or typing in your terminal texdoc lstdrvrs. Basic language should be around pages 12-15; at the bottom of page 14 you find Dim (suggestion: try for searching Dim and see whether it corresponds to Basic). You will promptly see that it is in morekeywords=[4], ie list 4

• Thank you for your answer. I could miss that " 100 times over. I am curious to know more about the "list 4 stuff". What list is this, and how can I know when I need this option in future code blocks? – gebruiker May 18 '17 at 19:36
• @gebruiker believe me the " stuff I missed for 10+ minutes. About the lists I'll make an add to the answer in a minute – Moriambar May 18 '17 at 19:37
• P.S. Don't worry about the programming point of view. That's not the actual line I wrote. I just deleted some stuff to make the MWE as short as possible, whilst keeping the error. It accidentally stopped making sense. But you're kind to mention it. – gebruiker May 18 '17 at 19:39
• @gebruiker I added an explanation of the list 4 and such – Moriambar May 18 '17 at 19:45