如何删除参数尾部的转义字符?

如何删除参数尾部的转义字符?
\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}

如果我按如下方式操作,则必须先删除结尾\的,#1然后再将其附加到\pstVerb{/。如何做到这一点?

\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}

答案1

困难的部分是删除反斜杠\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

答案2

\def\newconstb#1{%
  {\escapechar-1
   \xdef\tmp{\noexpand\newconst{\string#1}}}\tmp}

生成没有反斜杠的命令名称字符串,然后调用原始宏/。

相关内容