Here is an example using \newcommand
instead of \def
. The coordinate calculation is performed at the postscript level level (\rput(! x y)
), consider the reverse polish notation:
\documentclass{article}
\usepackage{pstricks}
\newcommand{\rectangle}[5]{
\psframe(#1,#2)(#3,#4)
\rput(! #3 #1 add 2 div #4 #2 add 2 div){#5}
}
% #3 #1 sub 2 div #1 add => (#3 - #1) / 2 + #1 => x
% #4 #2 sub 2 div #2 add => (#4 - #2) / 2 + #2 => y
\begin{document}
\begin{figure}
\begin{pspicture}(4,4)
\rectangle{1}{1}{4}{4}{center}
\end{pspicture}
\end{figure}
\end{document}
If you want to keep the syntax
\rectangle(a,b)(c,d){text}
here's how you can do with xparse
:
\documentclass{article}
\usepackage{xparse}
\usepackage{pstricks}
\NewDocumentCommand{\rectangle}{
>{\SplitArgument{1}{,}} r() % argument of type (<x>,<y>), will be passed as {<x>}{<y>}
>{\SplitArgument{1}{,}} r() % ditto
m % argument in braces
}{%
\dorectangle#1#2{#3}%
}
% same as before, just changed the name
\NewDocumentCommand{\dorectangle}{ m m m m m }{%
\psframe(#1,#2)(#3,#4)%
\rput(! #3 #1 add 2 div #4 #2 add 2 div){#5}%
}
\begin{document}
\begin{pspicture}(4,4)
\rectangle(1,1)(4,4){center}
\end{pspicture}
\end{document}
And here is a solution using \def
:
\documentclass{article}
\usepackage{pstricks}
\def\rectangle(#1,#2)(#3,#4)#5{
\psframe(#1,#2)(#3,#4)
\rput(! #3 #1 add 2 div #4 #2 add 2 div){#5}
}
\begin{document}
\begin{figure}
\begin{pspicture}(4,4)
\rectangle(1,1)(4,4){center}
\end{pspicture}
\end{figure}
\end{document}