4

Given a loop variable \x, I would like to use it as (foo.\x west), for example, where \x could be north. My attempt:

\documentclass[tikz,border=1mm]{standalone}

\begin{document}

\begin{tikzpicture}
    \node[draw] (a) at (0,0) {foo};
    \node[draw] (b) at (1,0) {bar};

    \draw[red]
        foreach \x in {north,south} {(a.\x west) -- (b.\x east)};
\end{tikzpicture}

\end{document}

This causes the following error:

! Package PGF Math Error: Unknown function `northwest' (in 'northwest').

Unfortunately, (a.\x\ west) or (a.\x{} west) also does not work, then I get the Missing \endcsname inserted error.

1 Answer 1

7

Use \space right after your variable.

space after variable

\documentclass[tikz,border=1mm]{standalone}

\begin{document}

\begin{tikzpicture}
    \node[draw] (a) at (0,0) {foo};
    \node[draw] (b) at (1,0) {bar};

    \draw[red]
        foreach \x in {north,south} {(a.\x\space west) -- (b.\x\space east)};
\end{tikzpicture}

\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .