6

Suppose we are in math-mode and I want to use command \E with the following behaviour. If one or two primes follow the command, i.e. \E' or \E'', then I want to have in these cases certain expressions, say A or B. In all the other cases I want to have merely E independently what precedes or follows the \E. Here are exames

  1. $any \E' some$ -> any A some
  2. $any \E'' some$ -> any B some
  3. $any \E some$ -> any E some

I dont want to use the command in a longer form like \E{arg1}{arg2} but only in \E', \E'' forms. Let, for example,

\def\E#1#2{my code}

Then the problem arises when using something like

$$
 x \E' \sin x 
$$

Or

$$
A \E b
$$

Because "b" should not be considered as an argument of \E in the latter case and the same reason for a slash in front of sine in the former case.

Marco Daniel
  • 95,681

2 Answers2

9

You might enjoy a generic interface for defining as many commands of the same kind as you want:

\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\declareprimedcommand} { m m m m }
 {% #1 = command to define
  % #2 = text for no prime
  % #3 = text for one prime
  % #4 = text for two primes
  \maximav_primedcommand:Nnnn #1 { #2 } { #3 } { #4 }
 }
\cs_new_protected:Npn \maximav_primedcommand:Nnnn #1 #2 #3 #4
 {
  \cs_new_protected:Npn #1
   {
    \peek_charcode_remove:NTF '
     { \peek_charcode_remove:NTF ' { #4 } { #3 } }
     { #2 }
   }
 }
\ExplSyntaxOff

\declareprimedcommand{\E}{x}{y}{z}

\begin{document}
$a \E b$

$a \E' b$

$a \E'' b$
\end{document}

enter image description here

egreg
  • 1,121,712
  • Thanks again. The only pity is the fact that xparse monstrous should be used –  Nov 15 '13 at 16:27
  • @maximav You could take the underlying implementation and recode it yourself: xparse here is more syntactic sugar than anything else, and while the \peek_... functions are tricky they are not that long. – Joseph Wright Nov 15 '13 at 16:29
  • 1
    @maximav: Do you see any drawbacks of xparse? Normally it's one line in your header and you can use all the facilities of this great package. – Marco Daniel Nov 15 '13 at 16:40
8

Here an approach:

\documentclass{article}
\makeatletter
\def\@ifprime#1{\@ifnextchar'{\@firstoftwo{#1}}}
\newcommand\E{\@ifprime\E@i\E@}
\def\E@i{\@ifprime{B}{A}}
\def\E@{E}
\makeatother
\begin{document}
\[ x \E' \sin x  \]

\[ x \E'' \sin x  \]

\[ x \E \sin x  \]
\end{document}
Marco Daniel
  • 95,681
  • Nice job! I would never guess. Is it a simplest (shortest) solution? Three auxiliary commands... Anyway, my THANKS! –  Nov 15 '13 at 15:43
  • By the way, Marco. The command names are very simple - \E@ or \E@i. Is this dengerous? I mean possible overlapping of the names with other packages whose variable names we cannot control. –  Nov 15 '13 at 16:10
  • 3
    @maximav Use \newcommand instead of \def – egreg Nov 15 '13 at 16:13
  • I would prefere an independent extension of the \E@... codes above to the following situations: \commonE{E}{a}{b}{c} with the usage like \newcommand{\myE}[4]{\commonE{#1}{#2}{#3}{#4}} –  Nov 15 '13 at 16:38
  • @maximav: egreg shows a suitable solution. – Marco Daniel Nov 15 '13 at 16:39