[Tex/LaTex] Define a “square” command

macrospstricks

I try to define the following command, for drawing a square in pstricks:

\newcommand{\square}[4] {
  \rput(#1,#2){\psframe[linecolor=#4](0,0)(#3,#3)}
}

I use it like this (for drawing a red square with a corner at (1,1) and a side-length of 3):

\square(1,1,3,red)

And get the following errors:

Package xcolor error: Undefined color '1'.
Missing number, treated as zero.
Illegal unit of measure (pt inserted).

What am I doing wrong?

(Additionally, if there is a simpler way to write this square command, I will be happy to learn..)

Best Answer

Use the PSTricks object definition. Then you have the same syntax for options and star versions:

\documentclass{article}
\usepackage{pstricks}

\makeatletter
\def\square{\pst@object{square}}% reads star and options and continues with \square@i
\def\square@i(#1,#2)#3{{\use@par\solid@star\psframe[origin={#1,#2}](#3,#3)}}
\makeatother

\begin{document}

\begin{pspicture}[showgrid](0,0)(4,4)
\square(1,1){3}
\square[linecolor=red](0,0.5){2}
\square[linecolor=blue,fillcolor=red!40,fillstyle=solid,opacity=0.5](2,0){2}
\square*[linecolor=cyan,opacity=0.4](0,2){2}
\end{pspicture}

\end{document}

enter image description here

Related Question