I try to implement the currying function , and I start from implement the following :
\myproc<=b<=awill printa \mapsto b \mapsto {\rm output}\myproc<=bwill print{\bf a} \mapsto b \mapsto {\rm output}\myproc<<=awill printa \mapsto {\bf b} \mapsto {\rm output}the next step is to make
\myproc<<=abe the function that acceptsbas input (e.g.\myproc<<=a<=boutputs\myproc<=b<=a)
where the\bf stands for the default value .
However when I replace thea /b byx_1 / andx_2 , something undesired happened :
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}- 2You shouldn't use two-letter font switches in latexsamcarter_is_at_topanswers.xyz– samcarter_is_at_topanswers.xyz2025-11-28 09:46:44 +00:00Commented23 hours ago
- Using
\NewDocumentCommandmight simplify this problemsamcarter_is_at_topanswers.xyz– samcarter_is_at_topanswers.xyz2025-11-28 10:13:57 +00:00Commented23 hours ago - I'm not sure what this is used for: why do you want to specify the argument, since it's essentially fixed?egreg– egreg2025-11-28 13:41:13 +00:00Commented19 hours ago
2 Answers2
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.
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}You mustlog in to answer this question.
Explore related questions
See similar questions with these tags.


