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?

FacuO.Z.
  • 161

2 Answers2

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}

JeT
  • 3,020
Ulrike Fischer
  • 327,261
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

Ulrich Diez
  • 28,770