1

Let say that I have the following code:

\documentclass{article}

\usepackage{xspace}

\def\cal#1{\ensuremath{\mathcal{#1}}\xspace}
\newcommand{\calA}{\cal A}

\begin{document}

Let \calA be a linearly ordered set.

\end{document}

Now, imagine that I need to use a lot mathcal for single letters, how can I make latex auto infer/auto generate the definition for the \calX where X is a capital letter. Of course, I could generate 26 newcommands but I would prefer not to do that.

I mean that I would like that I had not to put a space between \cal and X. Is there any trick available to do that?

  • 3
    You gain nothing by using \ensuremath and \xspace in place of the simpler and more semantic $\calA$. There are a few answers on the site about defining families of commands like those you want. See, for instance, https://tex.stackexchange.com/a/207992/4427 – egreg Jun 25 '19 at 14:06
  • Yes, I do not save much by using \ensuremath and \xspace. It just save two characters but takes more times to process for latex. I am more interested in how to generate such families of commands. I already tried searching but I don't think I am using the right keywords. – 永劫回帰 Jun 25 '19 at 14:11
  • Are you free to use LuaLaTeX? – Mico Jun 25 '19 at 14:12
  • Yes, I could use LuaLaTeX. – 永劫回帰 Jun 25 '19 at 14:13
  • related: https://tex.stackexchange.com/q/173236/51088 – 永劫回帰 Jun 25 '19 at 14:16

2 Answers2

3
\documentclass{article}
\usepackage{listofitems,xspace}
\newcommand\makecalx[1]{%
  \expandafter\def\csname cal#1\endcsname{\ensuremath{\mathcal{#1}}\xspace}%
}
\readlist\callets{A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z}
\foreachitem\z\in\callets[]{\expandafter\makecalx\expandafter{\z}}
\begin{document}
Here is \calG and \calZ.
\end{document}

enter image description here

  • Thank you for your answer. I see that you generate all the commands at once. Since the set of letters is small that is a reasonable choice. I am also wondering whether it would be possible to generate lazily commands on demand. \calG would trigger \calG if it exists and if it does not exists it would then trigger its creation through something like your macro \makecalx. – 永劫回帰 Jun 25 '19 at 14:21
  • 2
    @永劫回帰 I think the answer is no, because \calG is not \cal with an argument of G, but rather its own macro \calG. It is either defined or it isn't. If it isn't, TeX produces an error. Thus, for this type of syntax, all cases have to be dealt with in advance of their use. If you changed the syntax to \cal{G}, perhaps something could be done. – Steven B. Segletes Jun 25 '19 at 14:24
  • I see, thank you, that makes sense. – 永劫回帰 Jun 25 '19 at 14:26
2

A control sequence is either defined or undefined. There's no way to say

Hey, TeX, if you find a control sequence starting with \cal followed by an uppercase letter, say X, and which is currently undefined, define it so that it does \mathcal{X}.

Well, there might be some very fragile ways to do it. The trade between using complex and fragile code or defining the macros you need is in favor of the latter.

It's much simpler to use code suggested for Automatically generate new commands or the code in https://tex.stackexchange.com/a/207992/4427 for batch defining the macros you need.

About \ensuremath and \xspace, I recommend sticking instead to $\calA$, that's not much harder to type and carries more semantics.

egreg
  • 1,121,712