# Why won't \ifx match on list element of a foreach list if that element is a macro?

I have the following:

\documentclass{minimal}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}

\def\nend{6}
\def\rb{1.5mm}

\draw (0,0) -- (\nend,0);

\foreach \x in {0,...,\nend} {%
\coordinate (\x) at (\x,0);
}

\foreach \x in {\nend,0} {%
\ifx\x\nend
\draw[fill] (\x) circle[color=blue,radius=\rb] node[blue,below=2mm] {$x_{n}$};
\else
\draw[fill] (\x) circle[radius=\rb] node[black,below=2mm] {$x_{\x}$};
\fi
}

\end{tikzpicture}

\end{document}


I get this:

I expect the last node to be x_n, but it is not. I have seen the answers to this question, but I am working with an integer already so I would expect things to match. This happens whether \nend is first or last in the list.

This example does work:

\documentclass{minimal}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}

\def\nend{6}

\pgfmathparse{\nend-1}
\pgfmathsetmacro{\nm}{\pgfmathresult}

\def\nf{3}

\def\rb{1.5mm}
\def\rs{1mm}

\draw (0,0) -- (\nend,0);

\foreach \x in {0,...,\nend} {%
\coordinate (\x) at (\x,0);
}

\foreach \x in {1,...,\nm} {%
\ifx\x\nf
\else
\draw[fill] (\x) circle[radius=\rs] node[below=2mm] {$x_{\x}$};
\fi
}

\end{tikzpicture}

\end{document}


The moment I try to match on the last element, though, it fails.

-

In the second loop, \foreach does, successively,

\def\x{\nend}<loop code>
\def\x{0}<loop code>


Now, \ifx\x\nend will return false in both cases: in the case you would it return true the comparison is between a macro expanding to \nend and the macro \nend, which are different. You have to define a macro expanding to \nend in order to get true:

\documentclass{article}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}
\def\nend{6}\def\isnend{\nend}
\def\rb{1.5mm}
\draw (0,0) -- (\nend,0);
\foreach \x in {0,...,\nend} {%
\coordinate (\x) at (\x,0);
}
\foreach \x in {\nend,0} {%
\ifx\x\isnend
\draw[fill] (\x) circle[color=blue,radius=\rb] node[blue,below=2mm] {$x_{n}$};
\else
\draw[fill] (\x) circle[radius=\rb] node[black,below=2mm] {$x_{\x}$};
\fi
}

\end{tikzpicture}

\end{document}

-