2

I'd like to automatically determine the whitespace inside the arguments of a binary operator and provide appropriate spacing around the operator automatically. Example:

\documentclass{article} \usepackage{xparse} \ExplSyntaxOn \NewDocumentCommand{\binding}{mm}{#1\!\mapsto\!#2} \ExplSyntaxOff \begin{document} \noindent \verb!\(\binding{a}{b}\)! should produce \(a{\mapsto}b\).\\ \verb!\(\binding{aa}{bb}\)! should produce \(aa\!\mapsto\!bb\).\\ \verb!\(\binding{f(a+b)}{g(c+d)}\)! should produce \(f(a+b)\mapsto g(c+d)\).\\ \verb!\(\binding{f\,a}{g\,b}\)! should produce \(f\,a\,\mapsto\,g\,b\). \verb!\(\binding{i}{(\binding{a}{b})}\)! should produce \(i\mapsto(a{\mapsto}b)\). \end{document} 

How to do that in latex3? As a start, I would be happy just to distinguish between the first two kinds of inputs, i.e., to test whether the two arguments are at most one symbol long. As a maximum capability, I'd like to ensure that the whitespace before/after \mapsto is slightly larger than the largest whitespace in any of the two arguments.

1 Answer 1

3

This may be an answer to the first part of your question. Note that it counts tokens - it does not care what those tokens are. If you need to examine what the tokens are, you can do that, but obviously it will be more complex.

\documentclass{article} \usepackage{xparse} \ExplSyntaxOn \tl_new:N \l_leon_first_tl \tl_new:N \l_leon_second_tl \cs_new_protected_nopar:Nn \leon_binding:nn { \group_begin: \tl_set:Nn \l_leon_first_tl { #1 } \tl_set:Nn \l_leon_second_tl { #2 } \int_compare:nTF { ( \int_max:nn { \tl_count:V \l_leon_first_tl } { \tl_count:V \l_leon_second_tl } ) <= 1 } { \l_leon_first_tl {\mapsto} \l_leon_second_tl }{ \l_leon_first_tl \! \mapsto \! \l_leon_second_tl } \group_end: } \NewDocumentCommand{\binding}{mm} { \leon_binding:nn { #1 } { #2 } } \ExplSyntaxOff \begin{document} \(\binding{a}{b}\) should produce \(a{\mapsto}b\). \(\binding{aa}{bb}\) should produce \(aa\!\mapsto\!bb\). $\binding{i}{(\binding{a}{b})}$ \(\binding{f(a+b)}{g(c+d)}\) should produce \(f(a+b)\mapsto g(c+d)\). \(\binding{a\,a}{b\,b}\) should produce \(a\,a\,\mapsto\,b\,b\). \end{document} 

This distinguishes the first two cases i.e. 1 symbol versus 2 symbols and deals with the nesting case raised in comments.

distinguish first two cases & nesting

3
  • 1
    @LeonMeier Please see edit. I've cleaned up my comments a bit to keep things tidy. Commented Nov 17, 2016 at 22:34
  • @LeonMeier Someone else will have to do the second bit, though. I have no idea how to do that. Yet, anyway. Commented Nov 18, 2016 at 2:20
  • @LeonMeier Is it Turing complete? TeX is, but that doesn't mean expl3 is. In any case, it doesn't mean it would be at all practical to do it in TeX, even though it is Turing complete. Possibility is a weak standard. Commented Nov 18, 2016 at 3:10

You must log in to answer this question.