6

As I work with matrices all the time, I want to create the following command:

\newcommand{\2x2}{2 \times 2}

So that, for example, I can write:

Let A \in \mathbb{K}^{\2x2}, ... etc.

I have seen that Tex, does not allow using numbers when creating commands.

Is there a way to do this? Maybe with \usepackage or with a new command?

6
  • 1
    Can't just \twobytwo do the job? Or do you insist on having numbers in the command name?
    – Diaa
    Commented Dec 17, 2021 at 21:19
  • I would like two have numbers in the command name, as I have also wanted to use numbers in other commands before. It will be very useful and quick to type for me.
    – FacuO.Z.
    Commented Dec 17, 2021 at 21:23
  • 1
  • 1
    \NewDocumentCommand{\MAT}{>{\SplitArgument{1}{x}}m}{\makeMAT#1} and \NewDocumentCommand{\makeMAT}{mm}{\mathbb{K}^{#1\IfValueT{#2}{\times#2}}} will allow you to use \MAT{2x2} and \MAT{2}; but also \MAT{m x n}
    – egreg
    Commented Dec 18, 2021 at 11:04
  • I mean, if you want \2x2, you might also want \3x3 or \3x4 and so on. Do you think this will make your input easier?
    – egreg
    Commented Dec 18, 2021 at 11:31

2 Answers 2

9

You can use \csdef, \csuse:

\documentclass{article}
\usepackage{etoolbox,amssymb}
\csdef{2x2}{2 \times 2}
\begin{document}
Let $A \in \mathbb{K}^{\csuse{2x2}}$, ..

\end{document}
4

As under normal category code régime you cannot obtain control-word-tokens like \2x2 directly by having TeX read and tokenize .tex-input and as correct invocation of \csname..\endcsname in combination with \expandafter sometimes seems cumbersome, I offer a macro \CsNameToCsToken to create, e.g., the control-word-token \2x2 from the character-token-sequence 2x2.

Syntax:

\CsNameToCsToken⟨stuff not in braces⟩{⟨NameOfCs⟩}

⟨stuff not in braces⟩\NameOfCs

(⟨stuff not in braces⟩ may be empty.)

(Due to \romannumeral-expansion the result is obtained by triggering two expansion-steps, e.g., by having two "hits" with \expandafter.)

With such a macro you are not bound to specific definition commands:

\CsNameToCsToken{foo}\foo  .

\CsNameToCsToken\newcommand{foo}\newcommand\foo  .

\CsNameToCsToken\DeclareRobustCommand{foo}\DeclareRobustCommand\foo  .

\CsNameToCsToken\global\long\outer\def{foo}\global\long\outer\def\foo  .

\CsNameToCsToken\expandafter{foo}\bar\expandafter\foo\bar  .

\CsNameToCsToken\let{foo}=\bar\let\foo=\bar  .

\CsNameToCsToken\CsNameToCsToken\let{foo}={bar}\CsNameToCsToken\let\foo={bar}\let\foo=\bar  .

\CsNameToCsToken\string{foo}\string\foo  .

\CsNameToCsToken\meaning{foo}\meaning\foo  .

\CsNameToCsToken\NewDocumentCommand{foo}...\NewDocumentCommand\foo...  .

\makeatletter
%%===============================================================================
%% End \romannumeral-driven expansion safely:
%%===============================================================================
\@ifdefinable\UD@stopromannumeral{\chardef\UD@stopromannumeral=`\^^00}%
%%===============================================================================
%% Obtain control sequence token from name of control sequence token:
%%===============================================================================
%% \CsNameToCsToken<stuff not in braces>{NameOfCs}
%% ->  <stuff not in braces>\NameOfCs
%% (<stuff not in braces> may be empty.)
\@ifdefinable\CsNameToCsToken{%
  \long\def\CsNameToCsToken#1#{\romannumeral\InnerCsNameToCsToken{#1}}%
}%
\newcommand\InnerCsNameToCsToken[2]{%
  \expandafter\UD@exchange\expandafter{\csname#2\endcsname}{\UD@stopromannumeral#1}%
}%
\newcommand\UD@exchange[2]{#2#1}%
\makeatother

\documentclass{article}
\usepackage{amsmath, amssymb}

\CsNameToCsToken\newcommand*{2x2}{2 \times 2}

\begin{document}

Let $A \in \mathbb{K}^{\CsNameToCsToken{2x2}}$, \dots

\end{document}

enter image description here

Not the answer you're looking for? Browse other questions tagged .