1

I am trying to crate a "grid" of custom shapes that takes 8 inputs:

1 = origin x

2 = origin y

3 = box length x

4 = box height y

5 = # of columns x

6 = # of rows y

7 = column space x

8 = row space y

What I've attempted is this:

\documentclass{article}
\usepackage{tikz}
\begin{document}

\newcommand{\boxarray}[8]{
\foreach \k in {1,...,#6}
\foreach \i in {1,...,#5}
    \def\xo{{#1 + (\i - 1)*(#3 + #7)}}
    \def\yo{{#2 + (\k - 1)*(#4 + #8)}}
        \draw (\xo + .1, \yo)
          -- (\xo + #3 - .1,\yo)
          arc (180:270:.1)
          -- (\xo + #3,\yo - #4 - .1)
          arc (90:180:.1)
          -- (\xo + .1,\yo - #4)
          arc (0:90:.1)
          -- (\xo,\yo-.1)
          arc (270:360:.1)
          -- cycle;
}
\end{document}

However, I keep getting two "illegal unit of measure" and a "missing number" error. I know this is quite messy, but when I used the command I've created to make the shape inside the \foreach, it got around 23 errors. I'm really new to loops inside of latex, so any help would be appreciated.

Here is the shape I am trying to create:

\draw (6.6, 0) 
      -- (10.4,0)
      arc (180:270:.1)
      -- (10.5,-7.9)
      arc (90:180:.1)
      -- (6.6,-8)
      arc (0:90:.1)
      -- (6.5, -.1)
      arc (270:360:.1)
      -- cycle;
  • 1
    Welcome to TeX.SX! Please make your code compilable (if possible), or at least complete it with \documentclass{...}, the required \usepackage's, \begin{document}, and \end{document}. That may seem tedious to you, but think of the extra work it represents for TeX.SX users willing to give you a hand. Help them help you: remove that one hurdle between you and a solution to your problem. – dexteritas Jul 27 '17 at 12:03
  • You aren't in a tikzpicture environment when you issue the \draw, so you are bound to get errors. At least, that's what it looks like. You haven't shown us how you're trying to use the macro. – cfr Jul 27 '17 at 12:27
0

Your variables are not expanded timely. Change the macro definitions to

\newcommand{\boxarray}[8]{
\foreach \k in {1,...,#6}
\foreach \i in {1,...,#5}
    \pgfmathsetmacro{\xo}{{#1 + (\i - 1)*(#3 + #7)}}
    \pgfmathsetmacro{\yo}{{#2 + (\k - 1)*(#4 + #8)}}
        \draw (\xo + .1, \yo)
          -- (\xo + #3 - .1,\yo)
          arc (180:270:.1)
          -- (\xo + #3,\yo - #4 - .1)
          arc (90:180:.1)
          -- (\xo + .1,\yo - #4)
          arc (0:90:.1)
          -- (\xo,\yo-.1)
          arc (270:360:.1)
          -- cycle;
}
\begin{tikzpicture}
\boxarray11111111
\end{tikzpicture}

enter image description here

| improve this answer | |
  • Thank you! I didn't realize it was the actual macro definition. Also, you made me realize I forgot to edit the original code, so the box looks janky. – GAvenue Jul 27 '17 at 12:37

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.