I'm very new to this so bare with me. To get the hang of macros I wanted to write a simple command to draw a square in tikz. Now I know there are a lot of ways to do this, but I want to be able to write more difficult macros.

My code below:


    \fill[blue!40!white, opacity=0.5] (#1, #2) rectangle (#1 + 2, #2 + 2);

    \draw[step=1cm,gray,very thin] (-2,-2) grid (4,4);

What I would have expected was something like this:


Instead I got this error:

[Loading MPS to PDF converter (version 2006.09.02).]
! Argument of \XC@definec@lor has an extra }.
<inserted text>
l.14 \end
Runaway argument?
{pgfstrokecolor}{)}\ifx \reserved@a \@currenvir \else \@badend {)}\fi \ETC.
! Paragraph ended before \XC@definec@lor was complete.
<to be read again>
l.14 \end

! LaTeX Error: \begin{tikzpicture} on input line 11 ended by \end{+}.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.

l.14 \end

Any help would be very much appreciated

  • 1
    Instead of \tikzsquare{0,0} you should use \tikzsquare{0}{0}. Every parameter in its own pair of braces. – Ignasi Mar 16 '16 at 14:33
  • Instead of commands I'd suggest using pics for more complex macros. Examples: tex.stackexchange.com/questions/126161/… – Ignasi Mar 16 '16 at 14:35

You have told LaTeX that \tikzsquare has two arguments, but have only given it one (0,0), so \end is being grabbed as #2 and all sorts of weirdness results. You want \tikzsquare{0}{0}.

(More generally, note that TeX arguments are not comma-separated, but must be given in separate brace groups.)


If you want you could define

\def\tikzsquare(#1,#2);{\fill[blue!40!white,opacity=0.5] (#1,#2) rectangle (#1+2,#2+2);}

and then use \tikzsquare(0,0); which has more TikZ-ish syntax.

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.