4
\documentclass{article}
\usepackage{pstricks-add}
\usepackage{fp}

\def\newconst#1#2{%
    \expandafter\FPeval\csname#1\endcsname{#2}%
    \pstVerb{/#1 \csname#1\endcsname\space def}%
}


\begin{document}

% I want to define a macro that can only be invoked as below
%\newconst\speed{3*10^8}

% rather than
\newconst{speed}{3*10^8}

\end{document}

If I do as follows, then I have to remove the trailing \ in #1 before appending it to \pstVerb{/. How to do this?

\documentclass{article}
\usepackage{pstricks-add}
\usepackage{fp}

\def\newconst#1#2{%
    \FPeval#1{#2}%
    \pstVerb{/#1%<==== we must remove the trailing escape character \ in #1.
     #1\space def}%
}


\begin{document}

% I want to invoke
%\newconst\speed{3*10^8}

% instead of
\newconst{speed}{3*10^8}

\end{document}

2 Answers2

4

The difficult part is to strip the backslash for \pstVerb:

\def\newconst#1#2{%
    \FPeval#1{#2}%
    \begingroup\edef\x{\endgroup
      \noexpand\pstVerb{/\stripbs#1 #1\noexpand\space def}}\x
}
\makeatletter
\def\stripbs{\expandafter\@gobble\string}
\makeatother
egreg
  • 1,121,712
1
\def\newconstb#1{%
  {\escapechar-1
   \xdef\tmp{\noexpand\newconst{\string#1}}}\tmp}

Produces a string of the command name with no backslash then calls the original macro/.

David Carlisle
  • 757,742
  • \newconstb should take 2 arguments. Unfortunately, it does not work even with 2 arguments. I have no idea why. – kiss my armpit Jul 23 '12 at 16:39
  • No It would work as shown \newconstb\foo{abd} would be the same as \newconst{foo}{abc} as long as you don't need it in an expansion only context like \erite or \special. There is no need (and it is inefficient) to define a macro with an argument just to pass it on at the end of its expansion. – David Carlisle Jul 23 '12 at 17:43