3

I wanted to draw a diagram and didn't like the fact it contains magic constants, so I tried to parametrize it. However it seems that occurences of (1 + \N) / 2 and \N - 2 have to be somehow escaped. What is a proper way to do it?

\begin{tikzpicture}
    \def \N {6}

    \node (p) at ((1 + \N) / 2, -2) {};

    \foreach \i in {1, ..., \N - 2, \N} {
        \node (p\i) at (\i, 0) {};
    }

    \path foreach \i in {1, ..., \N - 2, \N} {
        (p\i) -- ($(p.center)!0.5!(p.south)$)
    };

    \node at ($(p\N - 2)!0.5!(p\N)$) {${\cdots}$};

\end{tikzpicture}
6

Answer to the original question

The parentheses are syntax characters and needed to be protected in the math expression. Also, there is an additional closing parentheses that needs removing in both \node commands of the question.

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\def\N{4}
\node at ({(1 + \N)/ 2}, 0) {};
\end{tikzpicture}
\end{document}

Workaround for the \foreach loop by calculating the number via e-TeX:

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\def\N{4}
\foreach \i in {1, ..., \numexpr\N - 2\relax} {
  % \typeout{i=\i}
  \node at ({(1 + \i)/ 2}, 0) {};
}
\end{tikzpicture}
\end{document}

Or the number can be calculated before:

\pgfmathtruncatemacro\M{\N - 2}
\foreach \i in {1, ..., \M} {}

Answer to the updated question

Commented fixes of the MWE:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
    \def \N {6}

    % Term put in curly braces (argument braces), because
    % the outmost '(' and ')' serve as syntax characters.
    \node (p) at ({(1 + \N) / 2}, -2) {};

    % Workaround via e-TeX
    \foreach \i in {1, ..., \numexpr\N - 2\relax, \N} {
        \node (p\i) at (\i, 0) {};
    }

    % Another workaround by performing the calculation beforehand
    \pgfmathtruncatemacro\M{\N - 2}
    \path foreach \i in {1, ..., \M, \N} {
        (p\i) -- ($(p.center)!0.5!(p.south)$)
    };

    % Inside an expandable name, pgf/TikZ math cannot be used.
    % Therefore, e-TeX performs the expandable calculation.
    \node at ($(p\the\numexpr\N - 2\relax)!0.5!(p\N)$) {${\cdots}$};

\end{tikzpicture}
\end{document}
  • And how to do the same in foreach range? foreach \i in {1, ..., \N - 2} does not work. – user87690 Dec 24 '16 at 14:10
  • I'm sorry to bother you, but it seems that every occurence of an expression needs completely different escaping. I have reformulated the question and added full code. – user87690 Dec 24 '16 at 14:46
  • @user87690 Answer to the updated question (revision 7) added. Yes, there are different ways of escaping, because the circumstances and reasons are different. – Heiko Oberdiek Dec 24 '16 at 16:41
  • And the $\pgfmathtruncatemacro$ solution works also in the latest situation. – user87690 Dec 24 '16 at 18:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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