我拿了此代码生成此tex
文件。
我的问题是:如何使用 ConTeXt 获得相同的结果?
我的 MWE :
\documentclass[border=1cm]{standalone}
\usepackage{mathtools,amssymb}
\usepackage{xcolor}
%----------------------------------------
%base 2
%----------------------------------------
\def\makeatletter{\catcode`\@11\relax}
\def\makeatother{\catcode`\@12\relax}
\makeatletter
\def\@makeother#1{\catcode`#1=12\relax}
\def\@ifnextchar#1#2#3{%
\let\reserved@d=#1%
\def\reserved@a{#2}\def\reserved@b{#3}%
\futurelet\@let@token\@ifnch}
\def\@ifnch{%
\ifx\@let@token\@sptoken
\let\reserved@c\@xifnch
\else
\ifx\@let@token\reserved@d
\let\reserved@c\reserved@a
\else
\let\reserved@c\reserved@b
\fi
\fi
\reserved@c}
\begingroup
\def\:{\global\let\@sptoken= } \: % this makes \@sptoken a space token
\def\:{\@xifnch} \expandafter\gdef\: {\futurelet\@let@token\@ifnch}
\endgroup
\def\@ifstar#1{\@ifnextchar *{\@firstoftwo{#1}}}
\long\def\@dblarg#1{\@ifnextchar[{#1}{\@xdblarg{#1}}}
\long\def\@xdblarg#1#2{#1[{#2}]{#2}}
\long\def \@gobble #1{}
\long\def\@firstofone#1{#1}
\long\def\@firstoftwo#1#2{#1}
\long\def\@secondoftwo#1#2{#2}
\let\@empty\empty
\def\@carcube#1#2#3#4\@nil{#1#2#3}
\def\@star@or@long#1{%
\@ifstar
{\let\l@ngrel@x\relax#1}%
{\let\l@ngrel@x\long#1}}
\let\l@ngrel@x\relax
\def\newcommand{\@star@or@long\new@command}
\def\new@command#1{%
\@testopt{\@newcommand#1}0}
\def\@newcommand#1[#2]{%
\@ifnextchar [{\@xargdef#1[#2]}%
{\@argdef#1[#2]}}
\long\def\@argdef#1[#2]#3{%
\@ifdefinable #1{\@yargdef#1\@ne{#2}{#3}}}
\long\def\@xargdef#1[#2][#3]#4{%
\@ifdefinable#1{%
\expandafter\def\expandafter#1\expandafter{%
\expandafter
\@protected@testopt
\expandafter
#1%
\csname\string#1\expandafter\endcsname
{#3}}%
\expandafter\@yargdef
\csname\string#1\endcsname
\tw@
{#2}%
{#4}}}
\def\@testopt#1#2{%
\@ifnextchar[{#1}{#1[#2]}}
\def\@protected@testopt#1{%%
\ifx\protect\@typeset@protect
\expandafter\@testopt
\else
\@x@protect#1%
\fi}
\long\def\@yargdef#1#2#3{%
\@tempcnta#3\relax
\advance \@tempcnta \@ne
\let\@hash@\relax
\edef\reserved@a{\ifx#2\tw@ [\@hash@1]\fi}%
\@tempcntb #2%
\@whilenum\@tempcntb <\@tempcnta
\do{%
\edef\reserved@a{\reserved@a\@hash@\the\@tempcntb}%
\advance\@tempcntb \@ne}%
\let\@hash@##%
\l@ngrel@x\expandafter\def\expandafter#1\reserved@a}
\long\def\@reargdef#1[#2]#3{%
\@yargdef#1\@ne{#2}{#3}}
\def\renewcommand{\@star@or@long\renew@command}
\def\renew@command#1{%
{\escapechar\m@ne\xdef\@gtempa{{\string#1}}}%
\expandafter\@ifundefined\@gtempa
{\@latex@error{\string#1 undefined}\@ehc}%
{}%
\let\@ifdefinable\@rc@ifdefinable
\new@command#1}
\long\def\@ifdefinable #1#2{%
\edef\reserved@a{\expandafter\@gobble\string #1}%
\@ifundefined\reserved@a
{\edef\reserved@b{\expandafter\@carcube \reserved@a xxx\@nil}%
\ifx \reserved@b\@qend \@notdefinable\else
\ifx \reserved@a\@qrelax \@notdefinable\else
#2%
\fi
\fi}%
\@notdefinable}
\let\@@ifdefinable\@ifdefinable
\long\def\@rc@ifdefinable#1#2{%
\let\@ifdefinable\@@ifdefinable
#2}
\def\@ifundefined#1{%
\expandafter\ifx\csname#1\endcsname\relax
\expandafter\@firstoftwo
\else
\expandafter\@secondoftwo
\fi}
\newcount\@tempcnta
\newcount\@tempcntb
\long\def\@whilenum#1\do #2{\ifnum #1\relax #2\relax\@iwhilenum{#1\relax
#2\relax}\fi}
\long\def\@iwhilenum#1{\ifnum #1\expandafter\@iwhilenum
\else\expandafter\@gobble\fi{#1}}
\long\def\@whiledim#1\do #2{\ifdim #1\relax#2\@iwhiledim{#1\relax#2}\fi}
\long\def\@iwhiledim#1{\ifdim #1\expandafter\@iwhiledim
\else\expandafter\@gobble\fi{#1}}
\long\def\@whilesw#1\fi#2{#1#2\@iwhilesw{#1#2}\fi\fi}
\long\def\@iwhilesw#1\fi{#1\expandafter\@iwhilesw
\else\@gobbletwo\fi{#1}\fi}
\def\@nnil{\@nil}
\def\@empty{}
\ifx\@@input\@undefined
\let\@@input\input
\fi
\def\input{\@ifnextchar\bgroup\@iinput\@@input}
\def\@iinput#1{\@@input#1 }
%\input{colordvi.tex}
\newbox\nb@box
\newcount\nb@a
\newcount\nb@b
\newcount\iter@
\newcommand\division[2][2]{%
\def\dividende@{#2}\def\base@{#1}\iter@\@ne\division@{#2}{#1}}
\newcommand\division@[2]{%
\setbox\nb@box\hbox{\kern0.5em#1\kern0.5em}%
\nb@a#1 \nb@b#1 \divide\nb@b#2
\vtop{%
\begingroup
\multiply\nb@b#2 \advance\nb@a-\nb@b
\hbox to\wd\nb@box{\hfil#1\hfil}%
\vskip3pt\hrule height0pt width\wd\nb@box\vskip3.4pt
\hbox to\wd\nb@box{\hfil\bf\color{red}{\number\nb@a}\kern0.5em}%
\expandafter\xdef\csname reste@\number\iter@\endcsname{\number\nb@a}%
\endgroup}%
\setbox\nb@box\hbox{8}\vrule height\ht\nb@box depth3.5ex
\setbox\nb@box\hbox{\kern0.5em\ifnum#2>\nb@b #2\else\number\nb@b\fi\kern0.5em}%
\vtop{%
\hbox to\wd\nb@box{\kern0.5em#2\hfil}%
\vskip3pt\hrule height0.4pt width\wd\nb@box\vskip3pt
\hbox{%
\csname @\ifnum\nb@b>\z@ first\else second\fi oftwo\endcsname
{\advance\iter@\@ne\gdef\maxiter{\number\iter@}%
\expandafter\division@\expandafter{\number\nb@b}{#2}}%
{\kern0.5em\number\nb@b\xdef\maxiter{\number\iter@}}}}}
\newcommand\afficheresultat{$(\dividende@)_{10}=(\afficheresultat@\maxiter)_{\base@}$}
\newcommand\afficheresultat@[1]{%
\csname reste@#1\endcsname
\ifnum#1>\@ne
\expandafter\expandafter\expandafter\afficheresultat@
\else
\expandafter\@gobble
\fi{\number\numexpr#1-1}}
\makeatother
\begin{document}
\division{21}
\afficheresultat
\end{document}
结果
答案1
正如我所说,困难的部分是安排。Lua 有点过头了,所以 MetaPost 就足够了。
编辑:使用 Metafun 的passvariable
和\MPrunvar
,解释于Metafun 手册,第 15.8 节,我们生成的字符串对 TeX 可见,我们可以在除法之后直接对其进行排版。友情建议,要利用 ConTeXt 功能,您必须学习一些 MetaPost 和 Lua 的基础知识。我很确定你最终会做到的,对吧?