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}

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.