I am using xlop package to make exercises for kids but when I want to put a third element in the addition the \opadd{12}{12}{12} command doesn't count this third one. Is there something I can put to make additions with 3 elements?
- 206,688
- 949
- 1
- 8
- 19
-
I don't think it's supported. – egreg Nov 22 '14 at 10:42
-
Related: https://tex.stackexchange.com/questions/337840/how-do-i-typeset-multiple-additions-nicely – Steven B. Segletes Aug 31 '17 at 11:48
3 Answers
I know that version 0.3 will do the job... but version 0.3 is still in progess (since maaaaany years). You can use this workaround. It don't check the correct syntax but I hope it does a good job:
\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@count@iii.5){\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}
-
Thank you very much @Jean-Côme Charpentier for this beautiful package and this beautiful new macro. Is there a way to write a single sign + and not several? – AndréC Aug 30 '17 at 07:19
-
It's not difficult but I want to give the possibility of having one or multiple signs '+'. I post another code for that. – Jean-Côme Charpentier Aug 31 '17 at 11:41
-
@AndréC See also https://tex.stackexchange.com/questions/337840/how-do-i-typeset-multiple-additions-nicely – Steven B. Segletes Aug 31 '17 at 11:50
Here is a code which allow to have single or multiple '+' signs.
\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\count@.5){\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@count@iii.5){\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}
-
Excellent, thank you very much! Can I suggest you publish these macros in a new version like 0.26.a or 0.27 in order to reach your goals for version 0.3 in small steps?
Advance in small steps is now the standard in software development. read: http://www.commentcamarche.net/contents/477-methodes-agiles-rad-xp
This is less discouraging for the developer and allows the developer to have feedback from users on the newly included macros.
– AndréC Aug 31 '17 at 12:58 -
1It's a good idea to release the 0.27 version of xlop. Before, I want to correct a bug about division with negative operand. In fact xlop 0.3 has quite nothing to do with xlop 0.2x (but the syntax of course) then improvements to 0.2x is just a wasting time for 0.3... – Jean-Côme Charpentier Aug 31 '17 at 14:01
I know, that is not an answer that you are expecting, but in Appendix C of documentation of present version (0.25) we have:
The features of version 0.3 aren’t definitively fixed but there are some points planned:
(...)
• macro for addition with more than two operands
- 37,268