4

I try to implement the currying function , and I start from implement the following :

  • \myproc<=b<=a will printa \mapsto b \mapsto {\rm output}
  • \myproc<=b will print{\bf a} \mapsto b \mapsto {\rm output}
  • \myproc<<=a will printa \mapsto {\bf b} \mapsto {\rm output}

    the next step is to make\myproc<<=a be the function that acceptsb as input (e.g.\myproc<<=a<=b outputs\myproc<=b<=a)

where the\bf stands for the default value .

However when I replace thea /b byx_1 / andx_2 , something undesired happened :

enter image description here

Here is the MWE :https://www.overleaf.com/9672474965wszpvxnkwkpr#bdc9bc

Here is the code in case you cannot access to the overleaf link :

\documentclass{article}\usepackage{amsmath}      \usepackage{amssymb}      \usepackage{amsfonts}         \usepackage{mathtools}    \usepackage{tikz}\begin{document}\makeatletter\def\myproc<{%     \@ifnextchar <%  {\myproc@NopB}  {\myproc@YopB}}\def\myproc@NopB<=#1{%  \myproc@YopB@YopA{{\bf x}_2}<={#1}%}\def\myproc@YopB=#1{%<=#2  \@ifnextchar <%  {\myproc@YopB@YopA#1}% myproc <=b<=a  myproc@YopB =b<=a myproc@YopB@YopA b<=a  {\myproc@YopB@NopA#1}% myproc <=b     myproc@YopB =b    myproc@YopB@NopA b}\def\myproc@YopB@NopA#1{%  \myproc@YopB@YopA{#1}<={{\bf x}_1}%}\def\myproc@YopB@YopA#1<=#2{  {#2} \mapsto {#1} \mapsto {\rm output}%}\makeatother% \myproc@NopB\(\myproc<<={x_1}\)\par\(\myproc<={x_2}\)\par\(\myproc<={x_2}<={x_1}\)\vspace{10pt}The desire result should be\par\(x_1\mapsto {\bf x}_2\mapsto {\rm output}\)\par\({\bf x}_1\mapsto x_2\mapsto {\rm output}\)\par\(x_1 \mapsto x_2 \mapsto {\rm output}\)\end{document}
asked23 hours ago
YCH817's user avatar
3
  • 2
    You shouldn't use two-letter font switches in latexCommented23 hours ago
  • Using\NewDocumentCommand might simplify this problemCommented23 hours ago
  • I'm not sure what this is used for: why do you want to specify the argument, since it's essentially fixed?Commented19 hours ago

2 Answers2

6

Replace your definition of\myproc@YopB by:

\def\myproc@YopB=#1{%<=#2  \@ifnextchar <%  {\myproc@YopB@YopA{#1}}% myproc <=b<=a  myproc@YopB =b<=a myproc@YopB@YopA b<=a  {\myproc@YopB@NopA{#1}}% myproc <=b     myproc@YopB =b    myproc@YopB@NopA b}

Note braces around parameter#1. Your code is buggy because there is only\myproc@YopB@YopA#1 and\myproc@YopB@NopA#1, so these macros read only first token from the parameterx_2 orx_1 which isx.

answered17 hours ago
wipet's user avatar
4

You could perhaps consider also using standard LaTeX syntax:

  • with a star* for the variant (instead of<<);
  • using square brackets for optional arguments.

This means

\myproc{x_2}\myproc{x_2}[x_1]\myproc*{x_1}\myproc*{x_1}[x_2]

for, respectively

\myproc<={x_2}\myproc<={x_2}<={x_1}\myproc<<={x_1}\myproc<<={x_1}<={x_2}

This can be easily defined with\NewDocumentCommand

\NewDocumentCommand{\myproc} { s m o }{%  \IfBooleanTF {#1}    {%      \IfValueTF {#3}        {%          %% \myproc<<=a<=b          #2 \mapsto #3%        }%        {%          %%  \myproc<<=a          #2 \mapsto \mathbf{x_2}%        }%    }%    {%      \IfValueTF {#3}        {%          %% \myproc<=b<=a          #3 \mapsto #2%        }%        {%          %%  \myproc<=b          \mathbf{x_1} \mapsto #2%        }%    }%  \mapsto \mathrm{output}%}

Example:

\documentclass{article}\usepackage{mathtools}    \NewDocumentCommand{\myproc} { s m o }{%  \IfBooleanTF {#1}    {%      \IfValueTF {#3}        {%          %% \myproc<<=a<=b          #2 \mapsto #3%        }%        {%          %%  \myproc<<=a          #2 \mapsto \mathbf{x_2}%        }%    }%    {%      \IfValueTF {#3}        {%          %% \myproc<=b<=a          #3 \mapsto #2%        }%        {%          %%  \myproc<=b          \mathbf{x_1} \mapsto #2%        }%    }%  \mapsto \mathrm{output}%}\begin{document}\begin{tabular}{ll}  Output & Desired \\[6pt]  %% \myproc<<={x_1}  \( \myproc*{x_1} \)  & \(x_1\mapsto {\bf x}_2\mapsto {\rm output}\)  \\  %% \myproc<={x_2}  \( \myproc{x_2} \)  & \({\bf x}_1\mapsto x_2\mapsto {\rm output}\)  \\  %% myproc<={x_2}<={x_1}  \( \myproc{x_2}[x_1] \)  & \(x_1 \mapsto x_2 \mapsto {\rm output} \)  \\  %% myproc<<={x_1}<={x_2}  \( \myproc*{x_1}[x_2] \)  & \(x_1 \mapsto x_2 \mapsto {\rm output}\)\end{tabular}\end{document}

Example

answered15 hours ago
jlab's user avatar

You mustlog in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.