# I failed to use \newenvironment and \newcommand

The following code snippet does not work.

\documentclass{article}
\usepackage{pstricks}

\newcommand\Left{-1}
\newcommand\Right{1}
\newcommand\Bottom{-1}
\newcommand\Top{1}

\newenvironment{\PsPicture}[1][]
{\begin{pspicture}[#1](\Left,\Bottom)(\Right,\Top)\ignorespaces}
{\end{pspicture}}

\newcommand\GetCoordinates{(\Left,\Bottom)(\Right,\Top)}

\begin{document}
\begin{PsPicture}[showgrid]
\end{PsPicture}

\newpage

\begin{pspicture}[showgrid]\GetCoordinates
\end{pspicture}

\end{document}


The code above is a simplified scenario that was extracted from my own document class. The idea is to avoid calling (\Left,\Bottom)(\Right,\Top) such as in

1. \pspicture(\Left,\Bottom)(\Right,\Top)
2. or \psframe(\Left,\Bottom)(\Right,\Top)

My first attempt is creating a new environment to encapsulate \pspicture, it does not work. The second attempt is creating a new command to return (\Left,\Bottom)(\Right,\Top) so hopefully it can be passed to any PSTricks macros that need (\Left,\Bottom)(\Right,\Top), it does not work as well.

How to fix it?

To make it clearer, I add the following code snippet

\begin{PsPicture}[showgrid]
\psframe\GetCoordinates
\end{PsPicture}


that takes advantage of \GetCoordinates so I can save some keystrokes rather than typing (\Left,\Bottom)(\Right,\Top) that is very tedious.

• Please no "it does not work" statements. Always state which errors you get or what exactly is not how it should be. – Martin Scharrer Jul 25 '11 at 15:37

it doesn't really make sense to define such new environments/commands, they do not make things easier!

\documentclass{article}
\usepackage{pstricks}

\newcommand\Left{-1}
\newcommand\Right{1}
\newcommand\Bottom{-1}
\newcommand\Top{1}

\newenvironment{PsPicture}[1][]
{\pspicture[#1](\Left,\Bottom)(\Right,\Top)}
{\endpspicture}

\newcommand\GetCoordinates{(\Left,\Bottom)(\Right,\Top)}

\begin{document}
\begin{PsPicture}[showgrid]
\end{PsPicture}

\bigskip
\expandafter\pspicture\GetCoordinates
\psgrid[style=gridstyle]
\endpspicture

%or
\def\Temp{\pspicture[showgrid]}
\expandafter\Temp\GetCoordinates
\endpspicture

\end{document}

• +1 I have no idea about \expandafter, it is really a new macro that I must learn now. Thank you. – xport Jul 25 '11 at 15:59
• "Expand the token after the following command" Instead of \pspicture\GetCoordinates you get with \expandafter the correct syntax: \pspicture(\Left,\Bottom)(\Right,\Top) – user2478 Jul 25 '11 at 16:02
• How to use \exapandafter when I need to use [] such as in \expandafter\psframe[linecolor=red]\GetCoordinates? – xport Jul 25 '11 at 16:12
• see edited example – user2478 Jul 25 '11 at 16:45
• +1 It makes more complicated. I should cancel this idea. :-) – xport Jul 25 '11 at 17:36

Regarding your first question: Your syntax for \newenvironment is wrong. Replace

\newenvironment{\PsPicture}[1][]


with

\newenvironment{PsPicture}[1][]


(without backslash).

• Here's some more information on the \newcommand and \newenvironment definitions. – Werner Jul 25 '11 at 15:37
• +1 for answering the first problem. How about the second one, \GetCoordinates? – xport Jul 25 '11 at 15:40
• @xport: I think Martin gave the correct answer to your second question, and because it was the harder one, you should accept his answer. – lockstep Jul 25 '11 at 15:47

The pspicture awaits the ( ... ) as part of its syntax and placing it inside a macro like \GetCoordinates hides it from it, which causes the issue. I would even expand the coordinates in the PsPicture environment first before passing it to pstricks, just to be on the save side.

\newenvironment{PsPicture}[1][]{%
\begingroup
\edef\@tempa{(\Left,\Bottom)(\Right,\Top)}%
\def\@tempb{\endgroup\begin{pspicture}[#1]}%
\expandafter\@tempb\@tempa
\ignorespaces}
{\end{pspicture}}