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;
GAvenue
  • 23
  • 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

2 Answers2

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

percusse
  • 157,807
  • 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
0

Your extra pair of braces inside the defintion of \xo and \yo leads to parsing problems. Leave them out but protect the ) inside from the parser by enclosing the x or y value in braces.

Of course, it's much better to evaluate the values only once via \pgfmathsetmacro and then use them unprotected (because they don't contain any ) or {} anymore) as in percusse's answer.

Code

\documentclass[tikz]{standalone}
\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[delta angle=90, radius=.1]
         ({\xo + .1},      {\yo})
      -- ({\xo + #3 - .1}, {\yo})
      arc [start angle=180]
      -- ({\xo + #3},      {\yo - #4 - .1})
      arc [start angle=90]
      -- ({\xo + .1},      {\yo - #4})
      arc [start angle=0]
      -- ({\xo},           {\yo-.1})
      arc [start angle=270]
      -- cycle;
}
\begin{document}
\tikz{
  \boxarray11111111
}
\end{document}
Qrrbrbirlbel
  • 119,821