3

Is there a way to make a command \mixedfont{...} that turns uppercase letters into \mathcal letters and lowercase ones into \mathfrak?

For example, \mixedfont{AbcDef} should give the same output as \mathcal{A}\mathfrak{bc}\mathcal{D}\mathfrak{ef}.

4
  • 1
    I’m curious what this is for. If it’s for a decorative text font, I wouldn’t want the math spacing, myself.
    – Davislor
    May 20, 2021 at 1:20
  • In math mode, I’d probably use one of the calligraphic alphabets that support lowercase.
    – Davislor
    May 20, 2021 at 1:23
  • So far, of the calligraphic fonts that support lowercase I never like the uppercase typeset. So I've been trying to mix and match a bit. May 20, 2021 at 6:07
  • Hmm.. Doesn't sound very useful; if you would use this as a shortcut to write such symbols in just one font it seems likely to give problems for any paper that you might want to submit to a journal using its own style etc May 21, 2021 at 7:20

2 Answers 2

6

Very trivial with a token cycle, which treats each token of the argument successively. All characters in the input are processed, one at a time, through the \Characterdirective. I assume that content is characters alone...all macros, spaces, and groups will be discarded from the input. If punctuation finds its way into the input, it will just be echoed.

\documentclass{article}
\usepackage{amssymb,tokcycle}
\Characterdirective{%
  \tctestifcatnx A#1{\ifnum`#1<`[ \mathcal{#1}\else\mathfrak{#1}\fi}{#1}%
}
\newcommand\mixedfont[1]{\tokcyclexpress{#1}}
\begin{document}
$y = \mixedfont{AbcDef}$
\end{document}

enter image description here

If you want the token cycle to handle macros and groups, you just need to tell it what to do when it encounters them in the input. And when you deal with macros that take arguments, you can no longer output the transformed tokens directly (as I did before), but instead gather them in a token list, and output the token list at the end. If you try to output them directly, then the macros will be expecting their arguments before you process them.

\documentclass{article}
\usepackage{amssymb,tokcycle}
\newcommand\mixedfont[1]{\tokcycle
  {\tctestifcatnx A##1{\ifnum`##1<`[ \addcytoks{\mathcal{##1}}\else
   \addcytoks{\mathfrak{##1}}\fi}{\addcytoks{##1}}}% CHARACTERS
  {\processtoks{##1}}% GROUPS
  {\addcytoks{##1}}% MACROS
  {\addcytoks{##1}}% SPACES
  {#1}\the\cytoks}
\begin{document}
$y = \mixedfont{A\frac{b}{c}D^{2x+1}ef}$
\end{document}

enter image description here

3

It's a two-liner with expl3

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

\ExplSyntaxOn
\NewDocumentCommand{\calfrak}{m}
 {
  \tl_set:Nn \l_tmpa_tl { #1 }
  % search for strings of lowercase letters and replace them with \mathfrak{letters}
  \regex_replace_all:nnN { ([a-z]+) } { \c{mathfrak}\cB\{\1\cE\} } \l_tmpa_tl
  % search for strings of lowercase letters and replace them with \mathcal{LETTERS}
  \regex_replace_all:nnN { ([A-Z]+) } { \c{mathcal}\cB\{\1\cE\} } \l_tmpa_tl
  \tl_use:N \l_tmpa_tl
 }
\ExplSyntaxOff

\begin{document}

$\calfrak{AbcDef}$

\end{document}

enter image description here

You must log in to answer this question.

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