LaTeX shenanigans

Published . #code

Just for fun, I wrote a small loop to define font-related single-letter commands, such as \cA = \mathcal{A} and so on. It is based on this answer by David Carlisle on TeX.SE. Here it is!

%%% Dark magic
\makeatletter
\def\font@loop#1{% define a loop
  \ifx\relax#1% if we get to \relax, do nothing
  \else% otherwise...
  % define \cA=\mathcal{A}, etc
  \expandafter\def\csname c#1\endcsname{\mathcal{#1}}
  % define \sA=\mathsf{A}, etc
  \expandafter\def\csname s#1\endcsname{\mathsf{#1}}
  % define \bA=\mathbf{A}, etc
  \expandafter\def\csname b#1\endcsname{\mathbf{#1}}
  % define \fA=\mathfrak{A}, etc
  \expandafter\def\csname f#1\endcsname{\mathfrak{#1}}
  % define \AA=\mathbb{A}, etc
  % warning: \AA and \SS are predefined LaTeX commands!
  \expandafter\def\csname #1#1\endcsname{\mathbb{#1}}
  \expandafter\font@loop%
  \fi}
% call the loop
\font@loop ABCDEFGHIJKLMNOPQRSTUVWXYZ\relax
\makeatother

It’s not pretty but it’s (in my opinion) a better alternative than writing down manually the different commands necessary.