Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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..)

share|improve this question
2  
Try \square{1}{1}{3}{red} –  Kevin C Jan 20 at 8:48

3 Answers 3

up vote 11 down vote accepted

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

share|improve this answer
    
This is much better than my solution. Thanks. –  Erel Segal Halevi Jan 29 at 14:08

Of course calling:

\square(1,1,3,red)

can not work if you define:

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

because LaTeX handles arguments differently from other programming languages. You should call:

\square{1}{1}{3}{red}

A complete example:

\documentclass{article}
\usepackage{pstricks}

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

\begin{document}

\begin{pspicture}(0,0)(3,3)
\square{1}{1}{3}{red}
\end{pspicture}
\end{document}

If you really wish to use:

\square(1,1,3,red)

you have the possibility, but you should modify the definition with the following:

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

The example becomes:

\documentclass{article}
\usepackage{pstricks}

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

\begin{document}

\begin{pspicture}(0,0)(3,3)
\square(1,1,3,red)
\end{pspicture}
\end{document}
share|improve this answer

Just for advertising the existence of the new macro, \reversepath that was introduced in the late of 2013.

\documentclass[pstricks,border=12pt]{standalone}
\def\RightWing{\psline(0,.5)(.5,.5)(.5,-.5)(0,-.5)}
\def\square(#1,#2)#3#4{%
    \pscustom[linecolor=#4,origin={#1,#2}]
    {
        \scale{#3 #3}
        \RightWing
        \reversepath
        \scale{-1 1}
        \RightWing
        \closepath
    }
    \ignorespaces
}

\begin{document}
\begin{pspicture}[showgrid](-5,-5)(5,5)
    \square(0,0){3}{red}
    \square(3,3){2}{blue}
\end{pspicture}
\end{document}

enter image description here

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.