2

I want to define a new character. For context, in particular, my new character would act similar to the ^ character for superscripts, but would put parentheses around the context of the superscript.

I know that I could write a normal command, like

\newcommand{\superscriptWithParentheses}[2]{{#1}^{({#2})}}

which produce the desired result. For instance, \superscriptWithParentheses{x}{i} would successfully create x^{(i)}. But I am hoping to define a character instead of an ordinary command. Why? For the same reasons that people would prefer to write x_i instead of x\sp{i}. (Basically, my motivations have to do with the (1) readability of the source code when the arguments get complex and (2) syntactic consistency with similar operators _ and ^. I could provide a concrete example if the additional motivation is necessary.)

Now following this link, if I wanted to redefine ^, I could do

\catcode`\^=\active
\newcommand{^}[1]{\sp{({#1})}}

However, I want to keep the ^ character for normal superscript usage.

So it seems like I need to define a new character. But which one? I can't think of one that doesn't already have a meaning that I shouldn't overwrite. Does anybody have advice? Is there some character that would be good to use that I'm not considering? Is there some way to provide a special meaning to double characters, like ^^? Is there some way to define \^ as a character that has different meaning than ^? Is there some other approach I'm not thinking of?

ashman
  • 121

2 Answers2

1

Of course, you can define

\def\^#1{^{(#1)}}

and use a\^{b+c} which expands to a^{(a+c)}. But there is a problem with old 8-bit LaTeX and its LICR (LaTeX internal character representation) where \^ is expected as accent-crating macro: \^o expands to ô. More exactly, if you redefine \^ and use old 8-bit LaTeX, then: if a user write ô in the input file, it expands internally to \^o and the following expansion crashes if \^ is redefined. So, you have to use a new LaTeX, i.e. LuaLaTeX or XeLaTeX. Or you can leave LaTeX completely and use OpTeX, for example.

Egreg showed classical solution for ^^ and he tried to compare it with Expl3 macros. I see the Expl3 code much more cryptic, but this is opinion based. If you are using OpTeX then you can use following code for ^^ and you can compare:

\adef^{\ea\isnextchar \string^{\spb}{\sp}}
\def\spb#1#2{\sp{(#2)}}
\mathcode`\^="8000 \catcode`\^=12

$ a^^b, \quad c^d $

\bye

wipet
  • 74,238
1

Not that I recommend it, but you can make ^ math active. In math mode, ^ will check for an immediately following ^; in this case it parenthesizes the superscript, otherwise it just emits a normal superscript token.

\documentclass{article}
\usepackage{amsmath}

\ExplSyntaxOn

\cs_new_protected:Npn __ashman_hat:w { \peek_charcode_remove:NTF ^ {% there is ^, remove it and do __ashman_hat_double:n } {% else, just normal ^ \c_math_superscript_token } }

\cs_new_protected:Nn __ashman_hat_double:n { \c_math_superscript_token { (#1) } }

\char_set_active_eq:NN ^ __ashman_hat:w

\ExplSyntaxOn

\AtBeginDocument{\mathcode^="8000 \catcode^=12 }

\begin{document}

$a^b+a^^b$

\end{document}

enter image description here

This has no consequence on the text mode accent \^.

The “classical” code would be

\documentclass{article}
\usepackage{amsmath}

\makeatletter \newcommand{\ashman@hat}{\futurelet\ashman@next\ashman@@hat} \newcommand{\ashman@@hat}{% \if\noexpand\ashman@next\string^% \expandafter\ashman@hathat \else \expandafter\sp \fi } \newcommand{\ashman@hathat}[2]{\sp{(#2)}}

\begingroup \catcode`^=\active \global\let^\ashman@hat \endgroup

\AtBeginDocument{\mathcode^="8000 \catcode^=12 } \makeatother

\begin{document}

$a^b+a^^b$

\end{document}

Not shorter and more cryptic.

If you find a way to easily key in then you can do

\documentclass{article}
\usepackage{amsmath}
\usepackage{newunicodechar}

\newunicodechar{↑}{\ashmanparens} \newcommand{\ashmanparens}[1]{^{(#1)}}

\begin{document}

$a^b+a↑b$

\end{document}

All methods work with any TeX engine (except Knuth TeX, for which only the “classical” method works).

egreg
  • 1,121,712
  • 1
    I like that $a^b$ and $a^^b$ give different behaviors. But just curious, why don't you recommend it? Is there a potential for this to cause large problems that I'm not thinking about? If so, how would you personally handle my situation? – ashman Sep 03 '22 at 02:31
  • 2
    @ashman I’d type a^{(b)}, that’s all. – egreg Sep 03 '22 at 07:56