4

Is it possible to automatically separate the math string into several parts, for example a macro \mathdecompose such that \mathdecompose{x^A_B} and \mathdecompose{{x}_{B}^{A}} both result in {x}{B}{A}?

I'm particularly interested in an expl3 approach, for example some clever use of l3regex.

Bonus question: David Carlisle's answer below allows me to decompose the input once, so as to define my macro \ideal to work like \ideal{p_1}. Is it possible to make it work for multiple input, so that the macro can work (ultimately) like \ideal{p_1 p_2 \cdots p_n}? (This is not a necessary feature, but it would be nice to have it).

Motivation: I would like to define a command \ideal that applies \mathfrak to the mathematical argument, but ignores the scripts. Thus \ideal{p_1^q} should be transferred to \mathfrak{p}_1^q.

2
  • Your \ideal command has wrong syntax anyway and induces bad habits in users of your package.
    – egreg
    Aug 28, 2022 at 19:47
  • @egreg Yes, thanks for the remind, now I realize that the current version supporting syntax like \ideal{p_1} would be suffice for the purpose of algebraic number theory :)
    – Jinwen
    Aug 28, 2022 at 19:55

3 Answers 3

5

You don't need explicit expl3 code here

enter image description here

\documentclass{article}

\NewDocumentCommand\mathdecompose{m}{\mathdecomposex#1}
\NewDocumentCommand\mathdecomposex{me{_^}}{\mathdecomposey{#1}{#2}{#3}}

% just for testing
\NewDocumentCommand\mathdecomposey{mmm}{%
\mathrm{base}=#1,
\mathrm{sub}=\IfNoValueTF{#2}{?}{#2},
\mathrm{sup}=\IfNoValueTF{#3}{?}{#3}
}

\begin{document}

$\mathdecompose{x}$

$\mathdecompose{x_{b}^{a}}$

$\mathdecompose{x^{a}_{b}}$
\end{document}
1
  • Thank you for this! I didn't know about the e type argument of \NewDocumentCommand before, this is quite elegant.
    – Jinwen
    Aug 28, 2022 at 15:43
5

Just for reference. Based on David Carlisle's answer, here is my \ideal. One can use this as \ideal{p_i^q} to get \mathfrak{p}_i^q, so with this one can write \ideal{p_1} instead of having to write \ideal{p}_1. However, this does not work for multiple input, such as \ideal{p_1 p_2}.

\documentclass{article}

\usepackage{amssymb}

\ExplSyntaxOn

\NewDocumentCommand \mymodule_math_decompose_apply:Nnnn { m m m m }
  {
    #1{#2} \IfNoValueF{#3}{\sb{#3}} \IfNoValueF{#4}{\sp{#4}}
  }

\NewDocumentCommand \ideal { m } { \mymodule_math_ideal_aux:w #1 }
\NewDocumentCommand \mymodule_math_ideal_aux:w { m e{_^} }{ \mymodule_math_decompose_apply:Nnnn \mathfrak { #1 } { #2 } { #3 } }

\ExplSyntaxOff

\begin{document}

\( \ideal{p_1} \)

\end{document}
3

With a sequence, feed it a record structure:

xyz

MWE

\documentclass{article}
\usepackage{xparse}

\usepackage{amssymb}

\ExplSyntaxOn

\NewDocumentCommand \mymodule_math_decompose_apply:Nnnn { m m m m }
  {
    #1{#2} \IfNoValueF{#3}{\sb{#3}} \IfNoValueF{#4}{\sp{#4}}
  }

\NewDocumentCommand \ideal { m } { \mymodule_math_ideal_aux:w #1 }
\NewDocumentCommand \mymodule_math_ideal_aux:w { m e{_^} }{ \mymodule_math_decompose_apply:Nnnn \mathfrak { #1 } { #2 } { #3 } }


%****************************************************
%*
%****************************************************
%--------------------
\NewDocumentCommand { \idealseq } { +m } { % 1=data

    \seq_gset_split:Nnn 
            \g_tmpa_seq 
            { ; } 
            { #1 }

%   \seq_show:N
%           \g_tmpa_seq 

            \seq_map_function:NN 
                    \g_tmpa_seq
                    \jw_funcmathfrak:n

}

%------------------
    \cs_set:Npn \jw_funcmathfrak:n #1 {
            \tl_set:Nx \l_tmpa_tl {#1}
%           \tl_show:N \l_tmpa_tl
        \exp_args:Nx \ideal{\l_tmpa_tl} \tex_space:D
    }
    

\ExplSyntaxOff

\begin{document}

\[ \ideal{p_1} \]

\[
\idealseq{p_1^q; p_2^q; p_x^q; p_y^q; p_z^q}
\]

\end{document}

You must log in to answer this question.

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