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.
\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}
share|improve this question

2 Answers 2

up vote 3 down vote accepted

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
share|improve this answer
\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/.

share|improve this answer
    
\newconstb should take 2 arguments. Unfortunately, it does not work even with 2 arguments. I have no idea why. –  stalking is prohibited 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

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.