使用 xlop 添加 3 个元素

使用 xlop 添加 3 个元素

我正在使用xlop软件包为孩子们做练习,但当我想在加法中添加第三个元素时,命令\opadd{12}{12}{12}不会计算这第三个元素。有什么可以添加的东西可以进行包含 3 个元素的加法吗?

答案1

我知道 0.3 版可以完成这项工作……但 0.3 版仍在开发中(已经好多年了)。您可以使用这个解决方法。它不会检查正确的语法,但我希望它能做得很好:

\documentclass{article}
\usepackage[a4paper, margin=2cm]{geometry}
\usepackage{xlop}
\usepackage{xcolor}

\makeatletter
\newcount\OMA@nbop
\newcommand\opmanyadd[1][nil]{%
  \begingroup
  \opset{#1}%
  \OMA@nbop \z@
  \opcopy{0}{OMA@result}%
   \op@manyadd
}
\newcommand\op@manyadd[1]{%
  \advance \OMA@nbop \@ne
  \op@split{#1}{a}%
  \op@split{#1}{OMA@op\the\OMA@nbop}%
  \ifop@deletezero
    \op@unzero{OMA@op\the\OMA@nbop}%
  \fi
  \opadd*{OMA@result}{#1}{OMA@result}%
  \op@split{OMA@result}{OMA@result}%
  \@ifnextchar\bgroup{\op@manyadd}{\op@manyaddend}
}
\newcommand\op@manyaddend{%
  % integer width
  \op@count@i = \csname OP@OMA@op1@i\endcsname
  \count@ \@ne
  \loop
    \ifnum\count@ < \OMA@nbop
    \advance\count@ \@ne
    \op@count@i = \op@max{\the\op@count@i}
                         {\csname OP@OMA@op\the\count@ @i\endcsname}%
  \repeat
  \op@count@i = \op@max{\the\op@count@i}{\OP@OMA@result@i}%
  \advance\op@count@i \@ne
  % decimal width
  \op@count@ii = \csname OP@OMA@op1@d\endcsname
  \count@ \@ne
  \loop
    \ifnum\count@ < \OMA@nbop
    \advance\count@ \@ne
    \op@count@ii = \op@max{\the\op@count@ii}
                          {\csname OP@OMA@op\the\count@ @d\endcsname}%
  \repeat
  \op@count@ii = \op@max{\the\op@count@ii}{\OP@OMA@result@d}%
  % total width
  \op@count@z = \op@count@i
  \advance\op@count@z \op@count@ii
  \setbox1=\hbox to\op@count@z\opcolumnwidth{%
    % 'add' symbol(s)
    \count@ \tw@
    \op@count@iii \@ne
    \loop
    \ifx\op@voperator\op@string@top
      \oplput(0,\the\count@){\op@addsymbol}%
    \else\ifx\op@voperator\op@string@bottom
      \oplput(0,\the\op@count@iii){\op@raddsymbol}%
    \else
      \oplput(0,\the\op@[email protected]){\op@addsymbol}%
    \fi\fi
    % rule
    \ophline(0,0.75){\the\op@count@z}%
    \ifnum \count@ < \OMA@nbop
      \advance \count@ \@ne
      \advance \op@count@iii \@ne
    \repeat
    % operand(s) and result
    \vbox{%
      \count@ \@ne
      \loop
        \op@makebox{\the\op@count@i}
                   {\the\op@count@ii}
                   {operandstyle.\the\count@}
                   {OMA@op\the\count@}%
        \hbox{\box0}%
      \ifnum \count@ < \OMA@nbop
        \advance \count@ \@ne
      \repeat
      \op@makebox{\the\op@count@i}
                 {\the\op@count@ii}
                 {resultstyle}
                 {OMA@result}%
      \hbox{\box0}%
    }%
  }%
  \leavevmode
  \dimen0=\OMA@nbop\oplineheight
  \ifx\op@voperation\op@string@center
    \lower0.5\dimen0\box1
  \else\ifx\op@voperation\op@string@top
    \lower\dimen0\box1
  \else
    \box1
  \fi\fi
  \endgroup
}
\makeatother

\begin{document}
\opmanyadd[operandstyle.2.3=\color{red},
           operandstyle.3;3=\color{red},
           resultstyle.3=\color{red},
           resultstyle.4=\color{red}]
           {12.6}{853.3}{158.1}

\opmanyadd[deletezero=false]
           {12.6}{853.3}{158.1}

\end{document} 

答案2

这是允许有单个或多个“+”符号的代码。

\documentclass{article}
\usepackage[a4paper, margin=2cm]{geometry}
\usepackage{xlop}
\usepackage{xcolor}

\makeatletter
% new option for symbol in \opmanyadd
% top, bottom, center (as for \opadd)
% Top, Bottom, Center (single symbol)
\def\pOP@vmanyoperator#1{\edef\@tempa{#1}%
  \ifx\@tempa\op@string@top
    \let\op@vmanyoperator\op@string@top
  \else\ifx\@tempa\op@string@bottom
    \let\op@vmanyoperator\op@string@bottom
  \else\ifx\@tempa\op@string@center
    \let\op@vmanyoperator\op@string@center
  \else\ifx\@tempa\op@string@Top
    \let\op@vmanyoperator\op@string@Top
  \else\ifx\@tempa\op@string@Bottom
    \let\op@vmanyoperator\op@string@Bottom
  \else\ifx\@tempa\op@string@Center
    \let\op@vmanyoperator\op@string@Center
  \else
    \op@error{Parameter vmanyoperator accept only `top', `center',
    `bottom', ^^J \space
    `Top', `Center', and `Bottom'. Here, the value is `#1'}%
    {Value must be `top', `center', `bottom', `Top', `Center',
      or `Bottom'}%
  \fi\fi\fi\fi\fi\fi
}
\def\op@string@Top{Top}
\def\op@string@Bottom{Bottom}
\def\op@string@Center{Center}
% initial value
\opset{vmanyoperator=center}
%
\newcount\OMA@nbop
\newcommand\opmanyadd[1][nil]{%
  \begingroup
  \opset{#1}%
  \OMA@nbop \z@
  \opcopy{0}{OMA@result}%
  \op@manyadd
}
\newcommand\op@manyadd[1]{%
  \advance \OMA@nbop \@ne
  \op@split{#1}{a}%
  \op@split{#1}{OMA@op\the\OMA@nbop}%
  \ifop@deletezero
    \op@unzero{OMA@op\the\OMA@nbop}%
  \fi
  \opadd*{OMA@result}{#1}{OMA@result}%
  \op@split{OMA@result}{OMA@result}%
  \@ifnextchar\bgroup{\op@manyadd}{\op@manyaddend}
}
\newcommand\op@manyaddend{%
  % integer width
  \op@count@i = \csname OP@OMA@op1@i\endcsname
  \count@ \@ne
  \loop
    \ifnum\count@ < \OMA@nbop
    \advance\count@ \@ne
    \op@count@i = \op@max{\the\op@count@i}
                         {\csname OP@OMA@op\the\count@ @i\endcsname}%
  \repeat
  \op@count@i = \op@max{\the\op@count@i}{\OP@OMA@result@i}%
  \advance\op@count@i \@ne
  % decimal width
  \op@count@ii = \csname OP@OMA@op1@d\endcsname
  \count@ \@ne
  \loop
    \ifnum\count@ < \OMA@nbop
    \advance\count@ \@ne
    \op@count@ii = \op@max{\the\op@count@ii}
                         {\csname OP@OMA@op\the\count@ @d\endcsname}%
  \repeat
  \op@count@ii = \op@max{\the\op@count@ii}{\OP@OMA@result@d}%
  % total width
  \op@count@z = \op@count@i
  \advance\op@count@z \op@count@ii
  \setbox1=\hbox to\op@count@z\opcolumnwidth{%
    % rule
    \ophline(0,0.75){\the\op@count@z}%
    % single 'add' symbol
    \ifx\op@vmanyoperator\op@string@Top
      \oplput(0,\the\OMA@nbop){\op@addsymbol}%
    \else\ifx\op@vmanyoperator\op@string@Bottom
      \oplput(0,1){\op@addsymbol}%
    \else\ifx\op@vmanyoperator\op@string@Center
      \count@ \OMA@nbop
      \divide \count@ \tw@
      \ifodd\OMA@nbop
        \advance\count@ \@ne
        \oplput(0,\the\count@){\op@addsymbol}%
      \else
        \oplput(0,\the\[email protected]){\op@addsymbol}%
      \fi
    \fi\fi\fi
    \count@ \tw@
    \op@count@iii \@ne
    \loop
      % multiple 'add' symbols
      \ifx\op@vmanyoperator\op@string@top
        \oplput(0,\the\count@){\op@addsymbol}%
      \else\ifx\op@vmanyoperator\op@string@bottom
        \oplput(0,\the\op@count@iii){\op@addsymbol}%
      \else\ifx\op@vmanyoperator\op@string@center
        \oplput(0,\the\op@[email protected]){\op@addsymbol}%
      \fi\fi\fi
    \ifnum \count@ < \OMA@nbop
      \advance \count@ \@ne
      \advance \op@count@iii \@ne
    \repeat
    % operand(s) and result
    \vbox{%
    \count@ \@ne
    \loop
      \op@makebox{\the\op@count@i}
                 {\the\op@count@ii}
                 {operandstyle.\the\count@}
                 {OMA@op\the\count@}%
      \hbox{\box0}%
    \ifnum \count@ < \OMA@nbop
      \advance \count@ \@ne
    \repeat
    \op@makebox{\the\op@count@i}{\the\op@count@ii}{resultstyle}{OMA@result}%
    \hbox{\box0}%
    }%
  }%
  \leavevmode
  \dimen0=\OMA@nbop\oplineheight
  \ifx\op@voperation\op@string@center
    \lower0.5\dimen0\box1
  \else\ifx\op@voperation\op@string@top
    \lower\dimen0\box1
  \else
    \box1
  \fi\fi
  \endgroup
}
\makeatother

\begin{document}
\opmanyadd[operandstyle.2.3=\color{red},
           operandstyle.3.3=\color{red},
           resultstyle.3=\color{red},
           resultstyle.4=\color{red}]
           {12.6}{853.3}{158.1}

left \opmanyadd[deletezero=false]
           {12.6}{853.3}{158.1}

left \opmanyadd[voperation=center]
           {12.6}{853.3}{158.1}{125}{15}{12.45}

left \opmanyadd[voperation=top]
           {12.6}{853.3}{158.1}{125}{15}{12.45}

\opmanyadd[vmanyoperator=Top]
           {12.6}{853.3}{158.1}{125}{15}{12.45}\quad
\opmanyadd[vmanyoperator=Center]
           {12.6}{853.3}{158.1}{125}{15}{12.45}\quad
\opmanyadd[vmanyoperator=Bottom]
           {12.6}{853.3}{158.1}{125}{15}{12.45}

\opmanyadd[vmanyoperator=Center]
           {12.6}{853.3}\quad
\opmanyadd[vmanyoperator=Center]
           {12.6}{853.3}{158.1}\quad
\opmanyadd[vmanyoperator=Center]
           {12.6}{853.3}{158.1}{125}\quad
\opmanyadd[vmanyoperator=Center]
           {12.6}{853.3}{158.1}{125}{15}\quad
\opmanyadd[vmanyoperator=Center]
           {12.6}{853.3}{158.1}{125}{15}{12.45}\quad

\end{document}

答案3

我知道,这不是您期望的答案,但在当前版本(0.25)文档的附录 C 中我们有:

0.3 版本的功能尚未完全修复,但计划了一些要点:

(...)

• 用于具有两个以上操作数的加法的宏

相关内容