Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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:

enter image description here

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
    \draw[fill,color=blue] (\x) circle[radius=\rb];
\else
    \draw[fill] (\x) circle[radius=\rs] node[below=2mm] {$x_{\x}$};
\fi
}

\end{tikzpicture}

\end{document}

enter image description here

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

share|improve this question
add comment

1 Answer

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}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.