Debian Codegraph @

texmf-dist/source/latex/basicarith/basicarith.dtx

raw
1 % \iffalse
2 % +AMDG This document was begun on 7 October 11EX, the
3 % feast of the Blessed Virgin Mary for the Rosary, and it is
4 % humbly dedicated to her, for her prayers, and to the
5 % Sacred Heart of Jesus for His mercy.
6 %
7 % This document is copyright 2014 by Donald P. Goodman, and is
8 % released publicly under the LaTeX Project Public License. The
9 % distribution and modification of this work is constrained by the
10 % conditions of that license. See
11 % http://www.latex-project.org/lppl.txt
12 % for the text of the license. This document is released
13 % under version 1.3 of that license, and this work may be distributed
14 % or modified under the terms of that license or, at your option, any
15 % later version.
16 %
17 % This work has the LPPL maintenance status 'maintained'.
18 %
19 % The Current Maintainer of this work is Donald P. Goodman
20 % ([email protected]).
21 %
22 % This work consists of basicarith.dtx, basicarith.ins, and
23 % derived files basicarith.sty and basicarith.pdf.
24 % \fi
25
26 % \iffalse
27 %<package>\NeedsTeXFormat{LaTeX2e}[1999/12/01]
28 %<package>\ProvidesPackage{basicarith}[2015/01/01 v1.1 support for typesetting basic arithmetic in the American fashion]
29 %<*driver>
30 \documentclass{ltxdoc}
31
32 \usepackage{doc}
33 \usepackage{array}
34 \usepackage{lettrine}
35 \setcounter{DefaultLines}{3}
36 \setlength{\DefaultFindent}{2pt}
37 \renewcommand{\LettrineFontHook}{\color{red}}
38 \usepackage{url}
39 \usepackage{spverbatim}
40 \usepackage[nocounters]{dozenal}
41 \usepackage[noshowdivwork]{basicarith}
42 \usepackage[colorlinks]{hyperref}
43 \usepackage{makeidx}
44 \EnableCrossrefs
45 \PageIndex
46 \CodelineNumbered
47 \RecordChanges
48 \makeindex
49 \DoNotIndex{\?,\{,\},\|,\DeclareFontFamily,\DeclareFontShape,
50 \DeclareMathAccent,\DeclareMathAlphabet,\DeclareMathDelimiter,
51 \DeclareMathSymbol,\DeclareMathVersion,\DeclareSymbolFont,\def,
52 \drmsym,\encodingdefault,\familydefault,\fontencoding,\fontfamily,
53 \fontseries,\fontshape,\hfil,\hbox,\mathalpha,\mathclose,
54 \mathopen,\mathord,\mathversion,\mp,\nabla,\nbshortroman,
55 \RedefineMRmdclxvij,\relax,\renewcomand,\RequirePackage,
56 \selectfont,\SetMathAlphabet,\SetSymbolFont,\drmsymbolredef,
57 \nodefaultfalse,\nodefaultmathfalse,\nodefaultmathtrue,\nodefaulttrue,
58 \nodefaulttextfalse,\nodefaulttexttrue,\noindent,\numexpr,
59 \acute,\addtolength,\advance,\backslash,\baselineskip,
60 \char,\counterA,\counterB,\DeclareOption,\ProcessOptions,
61 \drmmathlets,\if,\else,\fi,\ifnum,\fi,\ifdim,\fi,
62 \symbolsonlyfalse,\fontsize,\newif,\newcount,
63 \loop,\iter,\let,\renewcommand,\setbox,\setlength,\the,\vss,
64 \vskip,\vbox,\newlength,\@tempboxa,\b@loopi,\b@parenfontsize,
65 \counterA,\counterB,\newcount,\r@@t,\z@,\DeclareMathRadical,
66 \DeclareRobustCommand,\bBigg@,\@ifnextchar,\@sqrt,\@makefnmark,
67 \@thefnmark,\catcode,\active,\b@spaceout,\b@xloop,\bf,
68 \b@totalprobwid,\checknil,\assignthencheck,\afterassignment,
69 \auxcountchar,\b@charcount,\gobblechar,\getdroparrowwidth,
70 \checknil,\countchar,\countunlessnil,\csname,\endcsname,
71 \divide,\multiply,\advance,\edef,\expandafter,\f@size,\ifx,\fi,
72 \ifshowdivisionwork,\fi,\linewidth,\newdimen,\next,\nil,
73 \wd,\xx,\hbox,\vbox,\vtop,\hrule,\vrule,\hskip,\hss,\footnotesize,
74 \downarrow,\leavevmode,\settowidth}
75
76 \tracingmacros=3
77 \begin{document}
78 \DocInput{basicarith.dtx}
79 \end{document}
80 %</driver>
81 % \fi
82 %
83 % \title{The |basicarith| Package, v1.1}
84 % \author{Donald P.\ Goodman III}
85 % \date{\today}
86 %
87 % \maketitle
88 %
89 % \begin{abstract}
90 % The |basicarith| package provides means for typesetting
91 % arithmetic problems, of whatever operations, in a clean
92 % and open fashion, suitable for educational texts rather
93 % than scholarly works. Digits are spaced out, work (such
94 % as carrying, borrowing, and dropping) can be shown
95 % visibly, and individual digits can be styled independently.
96 % \end{abstract}
97 %
98 % \tableofcontents
99 %
100 % \section{Introduction}
101 %
102 % \TeX\ and \LaTeX\ are, of course, justly celebrated for
103 % their ability to beautifully typeset mathematics.
104 % However, there are few utilities available for typesetting
105 % the sort of mathematics that we find in primary school
106 % textbooks. We can easily typeset incredibly complex
107 % equations, and \TeX\ will put them together perfectly; but
108 % to typeset an example addition problem, showing our work,
109 % is quite difficult. |basicarith| attempts to fill this
110 % need by providing macros for typesetting and organizing
111 % such problems.
112 %
113 % It is notable that basic arithmetic is done in several
114 % different styles in various places, though of course the
115 % algorithms are typically the same. The author being an
116 % American, |basicarith| typesets the problems according to
117 % the American custom. Most of these are shared with other
118 % English-speaking countries; however, some, particularly
119 % long division, will look quite odd to those from other
120 % countries. No option for using these other styles is
121 % currently available.
122 %
123 % \section{Basic Macros}
124 %
125 % |basicarith| offers a few basic macros for formulating
126 % problems. These must be divided into two main groups:
127 % long division, and other operations. Since the latter are
128 % simpler, we'll start with those.
129 %
130 % \subsection{Non-Division Operations}
131 %
132 % The fundamental macros for this are
133 % \DescribeMacro{\opline}|\opline|,
134 % \DescribeMacro{\probline}|\probline|,
135 % \DescribeMacro{\nextpline}|\nextpline|, and
136 % \DescribeMacro{\soluline}|\soluline|. The syntax for each
137 % is simple; we'll take them in their order of use.
138 %
139 % \begin{center}
140 % \cmd{\probline} \marg{width} \marg{number}
141 % \end{center}
142 %
143 % Both these arguments are mandatory. Simply put,
144 % |probline| takes first the total width of the problem you
145 % are typesetting; this is equivalent to the longest line of
146 % the problem. So if your longest line is, say, eight
147 % digits long, you put ``8'' here. The second is the number
148 % of the first line of the equation.
149 %
150 % \begin{center}
151 % \cmd{\nextpline} \marg{number}
152 % \end{center}
153 %
154 % |\nextpline| just takes the next number of the equation.
155 %
156 % \begin{center}
157 % \cmd{\opline} \marg{operator} \marg{number}
158 % \end{center}
159 %
160 % |\opline| takes two mandatory arguments; first, the
161 % operator to be typeset, and second, the number of the
162 % final line. It also prints a ``solution line'' underneath
163 % itself; this makes it the last line in the problem before
164 % we start figuring the answer. Note that, if you want a
165 % mathematical symbol for the operator (say, ``$\times$''),
166 % you have to include the dollar signs yourself. This means
167 % that \emph{any} character can be your operator, not only
168 % math characters.
169 %
170 % \begin{center}
171 % \cmd{\soluline} \marg{number}
172 % \end{center}
173 %
174 % |\soluline| is designed for the solution of the problem.
175 % Functionally this is equivalent to |\nextpline|, but
176 % semantically it is clearer. Furthermore, it resets the
177 % line counter, so that line styles will be correctly
178 % applied in future equations.
179 %
180 % There remains only one more basic macro: |\noopline|.
181 %
182 % \begin{center}
183 % \cmd{\noopline} \marg{number}
184 % \end{center}
185 %
186 % \DescribeMacro{\noopline}|\noopline|
187 % is identical to |\opline| except that it does not print
188 % the operator. This is useful for typesetting operations
189 % that require one or more intermediate solutions; for
190 % example, multiplication by more than one digit.
191 % Typically, in the American style the operator is printed
192 % only on the final line of the first solution; subsequent
193 % intermediate solutions require a rule above them but no
194 % operator on that rule. |\noopline| is what produces this.
195 %
196 % These are then combined in the way one might expect. For
197 % example:
198 %
199 % \bigskip
200 % \hbox to\linewidth{%
201 % \hfil%
202 % \hbox to0.5\linewidth{\vbox{%
203 % \begin{spverbatim}
204 % \probline{9}{58193}
205 % \nextpline{54}
206 % \nextpline{4397}
207 % \opline{$+$}{38291374}
208 % \nextpline{38354018}
209 % \noopline{54}
210 % \soluline{38354072}
211 % \end{spverbatim}}
212 % }\hfil%
213 % \problembox{%
214 % \probline{9}{58193}
215 % \nextpline{54}
216 % \nextpline{4397}
217 % \opline{$+$}{38291374}
218 % \nextpline{38354018}
219 % \noopline{54}
220 % \soluline{38354072}
221 % }%
222 % \hfil%
223 % }%
224 % \bigskip
225 %
226 % And that's the essentials of using |basicarith| for
227 % non-long division.
228 %
229 % \subsection{Long Division}
230 %
231 % Long division is typically more difficult to typeset than
232 % other functions; so |basicarith| offers different macros
233 % for handling it. The examples below are meaningless
234 % (unlike those above, which are actually correct); they are
235 % merely meant to show the macros which are available.
236 %
237 % \begin{center}
238 % \cmd{\longdiv} \marg{length} \marg{dividend} \marg{divisor}
239 % \end{center}
240 %
241 % The \DescribeMacro{\longdiv}|\longdiv| macro is,
242 % obviously, the most important; it typesets the
243 % all-important first line of a long division problem.
244 % |\longdiv| takes three arguments, all mandatory; the first
245 % is the length of the dividend in number of digits (it uses
246 % this to calculate the appropriate length of the line over
247 % of the dividend); the second is the dividend itself; and
248 % the third is the divisor. It uses a simple
249 % close-parenthesis between the dividend and divisor, scaled
250 % to 1.2 times the current font size.
251 %
252 % \begin{center}
253 % \cmd{\ldsoluline} \marg{solution} \marg{remainder}
254 % \end{center}
255 %
256 % \DescribeMacro{\ldsoluline}|\ldsoluline| typesets
257 % the answer. This is a bit tricky, because as mentioned
258 % before, in American-style long division, the answer is
259 % typeset \emph{above} the question, not below it; but we
260 % don't know how much space we will need for that answer
261 % until we've typeset the first line, containing the
262 % dividend, divisor, and close-parenthesis. So
263 % |\ldsoluline| should be input \emph{after} the |\longdiv|
264 % command but \emph{before} any |\nextldline|s; otherwise,
265 % |basicarith| won't be able to place it correctly.
266 %
267 % The second argument to |\ldsoluline| is the remainder; if
268 % you don't want to typeset a remainder, you still must
269 % include this argument, but it can be left blank.
270 %
271 % \begin{center}
272 % \cmd{\nextldline} \marg{cutoff} \marg{number}
273 % \end{center}
274 %
275 % In the American style of long division, intermediate
276 % values are placed \emph{underneath} the dividend, while
277 % the solution itself is placed \emph{above} it.
278 % \DescribeMacro{\nextldline}|\nextldline| typesets these
279 % intermediate values. It takes two arguments, both
280 % mandatory. The first is the number of digits of the
281 % dividend that are encompassed by this intermediate result
282 % (that is, in purely visual terms, the number of digits in
283 % the dividend which will not have a number underneath
284 % them); the second is the number itself.
285 %
286 % Notice that |basicarith| does not keep track of how much
287 % it must indent subsequent lines when you're showing your
288 % work. This has the benefit that you can skip steps as you
289 % see fit; it has the drawback that you have to tell
290 % |basicarith| how much each |\nextldline| must skip ahead
291 % in order to get the number properly placed.
292 %
293 % \bigskip
294 % \hbox to\linewidth{%
295 % \hfil%
296 % \hbox to0.5\linewidth{\vbox{%
297 % \begin{spverbatim}
298 % \longdiv{6}{430;932}{983}
299 % \ldsoluline{837;61}{4}
300 % \nextldline{3}{43}
301 % \nextldline{2}{4389}
302 % \end{spverbatim}}
303 % }\hfil%
304 % \problembox{%
305 % \longdiv{6}{430;932}{983}
306 % \ldsoluline{837;61}{4}
307 % \nextldline{3}{43}
308 % \nextldline{2}{4389}
309 % }%
310 % \hfil%
311 % }
312 %
313 %
314 % And this suffices for basic usage of |basicarith|.
315 % However, there are many other settings available with the
316 % package, which we will address in the next section.
317 %
318 % \section{Advanced Usage}
319 %
320 % |basicarith| allows each line of a mathematical problem to
321 % be specially styled. For example, say you are trying to
322 % draw particular attention to the subtrahend of a given
323 % problem:
324 %
325 % \hbox to\linewidth{\LARGE\hfil%
326 % \linestyle{2}{\color{red}}
327 % \problembox{%
328 % \probline{4}{548}
329 % \opline{$-$}{27}
330 % \soluline{521}
331 % }%
332 % \hfil}%
333 %
334 % Or for some reason you want to highlight each line of a
335 % subtraction problem differently, perhaps to visually
336 % demonstrate which part is which:
337 %
338 % \hbox to\linewidth{\LARGE\hfil%
339 % \linestyle{1}{\color{blue}}%
340 % \linestyle{2}{\color{red}}%
341 % \linestyle{3}{\color{green}}%
342 % \problembox{%
343 % \probline{4}{548}%
344 % \opline{$-$}{27}%
345 % \soluline{521}%
346 % }%
347 % \hfil}%
348 %
349 % This is accomplished using the
350 % \DescribeMacro{\linestyle}|\linestyle| command, which
351 % takes two arguments: the number of the line of the
352 % problem to be styled (starting with 1 at the top), and the
353 % style to be applied to that line.
354 %
355 % \begin{center}
356 % \cmd{\linestyle} \marg{line number} \marg{style}
357 % \end{center}
358 %
359 % The style commands can be anything at all; colors,
360 % weights, shapes, or any combination of the above. These
361 % commands will typically be confined to the innermost box
362 % (particularly, they will not span |\problembox|es), but if
363 % you do need to manually clear all these values back to
364 % default (which is no styling at all), issue the command
365 % \DescribeMacro{\clearlinestyles}|\clearlinestyles|.
366 %
367 % \bigskip
368 % \hbox to\linewidth{%
369 % \hfil%
370 % \hbox to0.5\linewidth{\vbox{%
371 % \begin{spverbatim}
372 % \linestyle{1}{\color{blue}}%
373 % \linestyle{2}{\color{red}}%
374 % \linestyle{3}{\color{green}}%
375 % \probline{4}{548}%
376 % \opline{$-$}{27}%
377 % \soluline{521}%
378 % \end{spverbatim}}
379 % }\hfil%
380 % \vbox{\linestyle{1}{\color{blue}}%
381 % \linestyle{2}{\color{red}}%
382 % \linestyle{3}{\color{green}}%
383 % \vskip-1em%
384 % \LARGE
385 % \problembox{
386 % \probline{4}{548}%
387 % \opline{$-$}{27}%
388 % \soluline{521}%
389 % }}%
390 % \hfil%
391 % }
392 %
393 % Digits can also be individually styled by means of the
394 % \DescribeMacro{\digstyle}|\digstyle| command.
395 %
396 % \begin{center}
397 % \cmd{\digstyle} \marg{column number} \marg{style}
398 % \end{center}
399 %
400 % Note that these are really styling \emph{columns}, not
401 % merely digits; the style will be applied to every number
402 % in that column until either another |\digstyle| for that
403 % column number is issued or a
404 % \DescribeMacro{\cleardigitstyles}|\cleardigitstyles|
405 % command is encountered. (This latter is like
406 % |\clearlinestyles|, and is pretty self-explanatory.)
407 %
408 % \bigskip
409 % \hbox to\linewidth{%
410 % \hfil%
411 % \hbox to0.5\linewidth{\vbox{%
412 % \begin{spverbatim}
413 % \linestyle{1}{\color{blue}}%
414 % \linestyle{2}{\color{red}}%
415 % \linestyle{3}{\color{green}}%
416 % \digstyle{3}{\color{black}\itshape}%
417 % \probline{4}{548}%
418 % \opline{$-$}{27}%
419 % \soluline{521}%
420 % \end{spverbatim}}
421 % }\hfil%
422 % \vbox{%
423 % \linestyle{1}{\color{blue}}%
424 % \linestyle{2}{\color{red}}%
425 % \linestyle{3}{\color{green}}%
426 % \digstyle{3}{\color{black}\itshape}%
427 % \vskip-1em%
428 % \LARGE
429 % \problembox{
430 % \probline{6}{21;\x48}%
431 % \opline{$-$}{27}%
432 % \soluline{5;21}%
433 % }}%
434 % \hfil%
435 % }
436 %
437 % Notice also that |\digstyle| commands always take precedence over
438 % |\linestyle| commands, no matter what order they are
439 % issued in.
440 %
441 % Oftentimes we want to show our work explicitly, including
442 % our carrying (in the case of addition or multiplication)
443 % and our borrowing (in the case of subtraction). We can
444 % show this work by using the
445 % \DescribeMacro{\carryline}|\carryline|
446 % macro. |\carryline| takes two arguments, the number of
447 % digits you'll be putting carries over, and the line
448 % with the carries or borrows that you wish to display.
449 %
450 % \begin{center}
451 % \cmd{\carryline} \marg{number of digits} \marg{carries}
452 % \end{center}
453 %
454 % |\carryline| will respect all |\linestyle| and |\digstyle|
455 % commands, and it \emph{does} count as a line for
456 % |\linestyle| purposes.
457 %
458 % \bigskip
459 % \hbox to\linewidth{%
460 % \hfil%
461 % \hbox to0.5\linewidth{\vbox{%
462 % \begin{spverbatim}
463 % \carryline{4}{{3}{\strike{18}17}}
464 % \probline{4}{548}%
465 % \opline{$-$}{29}%
466 % \soluline{519}%
467 % \end{spverbatim}}
468 % }\hfil%
469 % \vbox{
470 % \vskip-1em%
471 % \LARGE
472 % \problembox{
473 % \carryline{4}{{3}{\strike{18}17}}
474 % \probline{4}{548}%
475 % \opline{$-$}{29}%
476 % \soluline{519}%
477 % }}%
478 % \hfil%
479 % }
480 % \bigskip
481 %
482 % The first argument must be the same as that in the
483 % |\probline| that the carries will be a part of. This is a
484 % clunkiness in the interface that I haven't been able to
485 % resolve.
486 %
487 % Multiple digits can be put in a single carry place by enclosing
488 % them in brackets. Further, styling can be applied in a
489 % limited way within those brackets; most basic styling,
490 % such as italics and boldface, will work, but more complex
491 % styling will not. Experimentally, at the very least
492 % |soul| and |ulem| styling will not work here. For this
493 % reason, |basicarith| provides the
494 % \DescribeMacro{\strike}|\strike| command, which will
495 % strike out a borrow when another borrow is required. It
496 % is used as shown in the example above (quite erroneously
497 % applied there for the sake of example). |\strike| takes a
498 % single argument, the number to be struck out. It is
499 % implemented quite na\"\i{}vly, and will consequently only
500 % strike out one or two digit numbers.
501 %
502 % Oftentimes we want to arrange our equations in the page,
503 % and |basicarith|'s setup, with each line being a separate
504 % |\hbox|, can sometimes make this difficult. We therefore
505 % have \DescribeMacro{\problembox}|\problembox|, which can
506 % enclose any number of |basicarith| statements and make it
507 % easier to position it on the page. For example, a
508 % |basicarith| equation in a |center| environment will not
509 % be centered, due to some deep \TeX\ magic that we don't
510 % need to go into here. Wrap the equation up in
511 % |\problembox|, however, with the |basicarith| equation as
512 % the only element, and it will work just fine:
513 %
514 % \begin{center}
515 % \problembox{%
516 % \carryline{4}{{3}{\strike{18}17}}
517 % \probline{4}{548}%
518 % \opline{$-$}{29}%
519 % \soluline{519}%
520 % }%
521 % \end{center}
522 %
523 % \section{Configuration Commands}
524 %
525 % There are a variety of other bits and pieces of
526 % |basicarith| that can be customized. The width of
527 % solution line is held in the macro
528 % \DescribeMacro{\b@solverulewidth}|\b@solverulewidth|. As
529 % implied by the \@ in its name, this is considered an
530 % ``internal'' command, but with |\makeatletter| and
531 % |\makeatother| it can still be reset by a user, to values
532 % reasonable or ridiculous. For example,
533 % |\b@solverulewidth=2pt| (or
534 % |\setlength{\b@solverulewidth}{2pt}|) gives:
535 %
536 % \bigskip
537 % \makeatletter\b@solverulewidth=2pt\makeatother
538 % \hbox to\linewidth{\hfil%
539 % \problembox{
540 % \probline{3}{44}
541 % \opline{$+$}{44}
542 % }\hfil}
543 % \makeatletter\b@solverulewidth=0.4pt\makeatother
544 % \bigskip
545 %
546 % The longdivision equivalent is
547 % \DescribeMacro{\b@longdivlinewidth}|\b@longdivlinewidth|;
548 % setting it equal to 2pt gives:
549 %
550 % \bigskip
551 % \makeatletter\b@longdivlinewidth=2pt\makeatother
552 % \hbox to\linewidth{\hfil%
553 % \problembox{\longdiv{4}{3298}{24}}
554 % \hfil}%
555 % \makeatletter\b@longdivlinewidth=0.4pt\makeatother
556 % \bigskip
557 %
558 % Both of these values are the \TeX-default 0.4pt unless
559 % changed by the user.
560 %
561 % For adminstrative reasons, the sizes of problems are
562 % limited both in number of \emph{rows} and in number of
563 % \emph{columns}. The parameters controlling these things
564 % are, unsurprisingly,
565 % \DescribeMacro{\b@maxcols}|\b@maxcols| and
566 % \DescribeMacro{\b@maxrows}|\b@maxrows|. Both of these are
567 % set at twenty to begin with, which ought to be more than
568 % enough; but they can be changed if needed.
569 %
570 % By default, when a remainder is displayed in a long
571 % division problem, it is prefixed by ``R,'' in the American
572 % custom. If you'd like something different, you can
573 % redefine
574 % \DescribeMacro{\b@remaindertext}|\b@remaindertext|; its
575 % contents will be printed instead.
576 %
577 % When doing long division, sometimes work is shown in a
578 % more explicit way. In addition to writing intermediate
579 % solutions beneath the appropriate digits of the dividend,
580 % we often show visibly how digits are ``dropped'' from the
581 % dividend into those intermediate problems, by drawing an
582 % arrow down from those digits to the appropriate place.
583 % With |basicarith|, we do this by issuing the
584 % \DescribeMacro{\showdivwork}|\showdivwork|, and turn it
585 % off with \DescribeMacro{\noshowdivwork}|\noshowdivwork|.
586 % Showing work is turned \emph{on} by default.
587 %
588 % \def\fractionsymbol{.}
589 % \bigskip
590 % \hbox to\linewidth{%
591 % \hfil%
592 % \hbox to0.5\linewidth{\vbox{%
593 % \begin{spverbatim}
594 % \showdivwork
595 % \longdiv{6}{430.932}{983}
596 % \ldsoluline{837.61}{4}
597 % \nextldline{3}{43}
598 % \nextldline{2}{4389}
599 % \end{spverbatim}}
600 % }\hfil%
601 % \problembox{%
602 % \showdivwork
603 % \longdiv{6}{430.932}{983}
604 % \ldsoluline{837.61}{4}
605 % \nextldline{1}{40}
606 % \nextldline{2}{69}
607 % \nextldline{3}{73}
608 % \nextldline{4}{22}
609 % }%
610 % \hfil%
611 % }
612 % \bigskip
613 %
614 % These settings can also be set globally for the package at
615 % loading time, with the package options
616 % \DescribeMacro{noshowdivwork}|noshowdivwork| and
617 % \DescribeMacro{showdivwork}|showdivwork|. The latter is,
618 % of course, the default.
619 %
620 % Incidentally, the last of these examples demonstrates
621 % another configuration option:
622 % \DescribeMacro{\fractionsymbol}|\fractionsymbol|. The
623 % author is a dozenalist, and dozenalists customarily use a
624 % semicolon as a fractional point; but most people are
625 % decimalists, and usually use either a dot or a comma.
626 % With |basicarith|, you can use any symbol you want; the
627 % default is ``;'', but by redefining |\fractionsymbol|, you
628 % can get anything else. The above was typeset with:
629 %
630 % \begin{center}
631 % |\def\fractionsymbol{.}|
632 % \end{center}
633 %
634 % \def\fractionsymbol{;}
635 % The width of the box which encloses each digit is
636 % controlled by a macro called
637 % \DescribeMacro{\b@widthofdigit}|\b@widthofdigits|;
638 % redefining this will result in different size digit boxes.
639 % The following two lines show this macro set at 3ex and
640 % 1ex, respectively.
641 %
642 % \bigskip
643 % \hbox to\linewidth{%
644 % \hfil%
645 % \makeatletter\def\b@widthofdigit{3ex}\probline{4}{24;56}\makeatother
646 % \hfil%
647 % }
648 % \bigskip
649 % \hbox to\linewidth{%
650 % \hfil%
651 % \makeatletter\def\b@widthofdigit{1ex}\probline{4}{24;56}\makeatother
652 % \hfil%
653 % }
654 % \def\b@widthofdigit{2ex}
655 %
656 % The default for this setting is 2ex. Note that this is a
657 % macro, not a dimen; change it using |\def| or
658 % |\renewcommand|, not with |\setlength|.
659 %
660 % \section{Implementation}
661 %
662 % We begin by defining the necessary conditional for
663 % determining whether long division work should be shown
664 % (that is, whether to draw drop arrows), and then defining
665 % and processing the package options.
666 % \begin{macrocode}
667 \newif\ifshowdivisionwork % switch for drop arrows
668 \showdivisionworktrue
669 \DeclareOption{noshowdivwork}{\showdivisionworkfalse}
670 \DeclareOption{showdivwork}{\showdivisionworktrue}
671 \ProcessOptions
672 % \end{macrocode}
673 % We move on by defining the many dimensions and counters that
674 % are required to typeset basic arithmetic problems. Most
675 % of these are described afterward by a comment. First, we
676 % define those which rarely change; think limitations on the
677 % numbers of rows and columns; in the documentation we call
678 % these ``configuration options.''
679 % \begin{macrocode}
680 \newdimen\b@solverulewidth % rule under the operator line
681 \b@solverulewidth=0.4pt
682 \newcount\b@maxcols % maximum length of a problem
683 \b@maxcols=20
684 \newcount\b@maxrows % maximum lines of a problem
685 \b@maxrows=20
686 \newdimen\b@longdivlinewidth % width of above
687 \def\fractionsymbol{;}
688 % \end{macrocode}
689 % Next, we define those variables which change frequently;
690 % these are the counters that keep track of which row and
691 % column we're on and things of that nature.
692 % \begin{macrocode}
693 \newdimen\b@topdivline % length of the above
694 \newdimen\b@totalprobwid % width of widest line of problem
695 \newdimen\b@digitwid % width of a digit
696 \def\b@widthofdigit{2ex}
697 \newcount\b@colnum % row number of problem
698 \b@colnum=0%
699 \def\specialdigitstyle{} % style for a given digit
700 \def\speciallinestyle{} % style for a given digit
701 \b@longdivlinewidth=0.4pt
702 \newdimen\b@parenfontsize % size of parenthesis in longdiv
703 \newcount\b@linenum % row number of problem
704 \newdimen\b@divisorlen % length of divisor
705 \newdimen\b@divparenlen % width of the paren in ld
706 \newdimen\b@ldrowlen % length to add to b@divisorlen
707 \newdimen\b@fulldivlen % length of divisor + dividend
708 \b@fulldivlen=0pt
709 \newcount\b@charcount % number of chars in an argument
710 \b@charcount=0
711 \newcount\b@loopi % generic loop counter
712 \b@loopi=0
713 \def\b@remaindertext{R} % text for the remainder
714 \newdimen\b@droparrowlen % drop arrow length
715 \b@droparrowlen=0pt
716 % \end{macrocode}
717 % Now we define the macros that are used for counting the
718 % number of characters in various strings; these are adapted
719 % from macros by ``Florent'' at
720 % \url{tex-and-stuff.blogspot.com}.
721 % \begin{macrocode}
722 \def\gobblechar{\let\char= }
723 \def\assignthencheck{\afterassignment\checknil\gobblechar}
724 \def\countunlessnil{%
725 \ifx\char\nil \let\next=\relax%
726 \else%
727 \let\next=\auxcountchar%
728 \advance\b@charcount by1%
729 \fi%
730 \ifx\char;\advance\b@charcount by-1\fi%
731 \next%
732 }%
733 \def\auxcountchar{%
734 \afterassignment\countunlessnil\gobblechar%
735 }%
736 \def\countchar#1{\def\xx{#1}\b@charcount=0 \expandafter\auxcountchar\xx\nil}
737 % \end{macrocode}
738 % Now we define some macros for splitting a string into
739 % individual characters and putting them in boxes; these are
740 % adapted from David Carlisle's answer on
741 % \url{tex.stackexchange.com}, question 57598.
742 % \begin{macrocode}
743 \def\b@expandloop#1{%
744 \hbox{%
745 \b@xloop#1\relax
746 }%
747 }
748 \def\b@xloop#1{%
749 \if#1\fractionsymbol\else\advance\b@colnum by-1\fi%
750 \ifx\relax#1%
751 \else%
752 \if#1\fractionsymbol%
753 \rlap{\hbox to0pt{\hss#1\hss}}%
754 \else%
755 \hbox to\b@digitwid{\hfil{%
756 \csname speciallinestyle\romannumeral\b@linenum\endcsname%
757 \csname specialdigitstyle\romannumeral\b@colnum\endcsname%
758 #1%
759 }\hfil}%
760 \fi%
761 \expandafter\b@xloop%
762 \fi%
763 }
764 \def\b@spaceout#1{%
765 \countchar{#1}%
766 \b@colnum=\b@charcount%
767 \advance\b@colnum by1%
768 \b@expandloop{#1}%
769 }%
770 % \end{macrocode}
771 % Now we define our macros for non-long-division problems.
772 % \begin{macrocode}
773 \def\probline#1#2{%
774 \advance\b@linenum by1%
775 \b@digitwid=\b@widthofdigit%
776 \b@totalprobwid=\b@digitwid%
777 \multiply\b@totalprobwid by#1%
778 \hbox to\b@totalprobwid{%
779 \hfil\b@spaceout{#2}%
780 }%
781 }%
782 \def\opline#1#2{%
783 \advance\b@linenum by1%
784 \hbox{%
785 \hbox to\b@digitwid{\hfil#1}%
786 \advance\b@totalprobwid by-\b@digitwid%
787 \hbox to\b@totalprobwid{%
788 \hfil\b@spaceout{#2}%
789 }%
790 }%
791 \vskip0.5ex%
792 \hrule width\b@totalprobwid height\b@solverulewidth%
793 \vskip0.5ex%
794 }%
795 \def\noopline#1{%
796 \opline{}{#1}%
797 }%
798 \def\nextpline#1{%
799 \advance\b@linenum by1%
800 \hbox{%
801 \hbox to\b@totalprobwid{%
802 \hfil\b@spaceout{#1}%
803 }%
804 }%
805 }%
806 \def\soluline#1{%
807 \advance\b@linenum by1%
808 \hbox{%
809 \hbox to\b@totalprobwid{%
810 \hfil\b@spaceout{#1}%
811 }%
812 }%
813 \b@linenum=0%
814 }%
815 \def\carryline#1#2{%
816 {%
817 \advance\b@linenum by1%
818 \b@digitwid=\b@widthofdigit%
819 \b@totalprobwid=\b@digitwid%
820 \multiply\b@totalprobwid by#1%
821 \footnotesize%
822 \hbox to\b@totalprobwid{%
823 \hfil\b@spaceout{#2}%
824 }%
825 \hrule width\b@totalprobwid height0pt%
826 \vskip0.4em%
827 }%
828 }
829 % \end{macrocode}
830 % Now we proceed to define the macros for long division.
831 % \begin{macrocode}
832 \def\longdiv#1#2#3{%
833 \advance\b@linenum by1%
834 \vskip\baselineskip%
835 \b@digitwid=\b@widthofdigit%
836 \b@topdivline=\b@digitwid%
837 \settowidth{\b@divisorlen}{\b@spaceout{#3}}
838 \b@parenfontsize=\f@size pt%
839 \multiply\b@parenfontsize by12%
840 \divide\b@parenfontsize by10%
841 \settowidth{\b@divparenlen}{%
842 \fontsize{\b@parenfontsize}{\b@parenfontsize}\selectfont)}%
843 \multiply\b@topdivline by#1%
844 \advance\b@topdivline by0.5\b@digitwid%
845 \vskip0.5ex%
846 \vbox{%
847 \hbox{%
848 \hskip\b@divisorlen%
849 \vrule width\b@topdivline height\b@longdivlinewidth%
850 }%
851 \nointerlineskip%
852 \hbox{%
853 \b@spaceout{#3}%
854 \hfil{\fontsize{\b@parenfontsize}{\b@parenfontsize}\selectfont)}%
855 \b@spaceout{#2}%
856 }%
857 }%
858 \advance\b@divisorlen by\b@divparenlen%
859 }%
860 \def\ldsoluline#1#2{%
861 \advance\b@fulldivlen by\b@divisorlen%
862 \advance\b@fulldivlen by\b@topdivline%
863 \advance\b@fulldivlen by-\b@digitwid%
864 \advance\b@fulldivlen by\b@divparenlen%
865 \vskip-2\baselineskip%
866 \hbox to\b@fulldivlen{%
867 \hfil%
868 \b@spaceout{#1}%
869 \if#2\relax\else\rlap{\hskip1em \b@remaindertext{ }#2}\fi%
870 }%
871 \vskip\baselineskip%
872 }%
873 % \end{macrocode}
874 % This is an interesting little trick which gets the width
875 % of a box; we use this to determine the placement of the
876 % drop arrows when we're showing our long division work.
877 % \begin{macrocode}
878 \newdimen\b@droparrowwidth
879 \def\getdroparrowwidth{%
880 \setbox\@tempboxa\hbox{$\downarrow$}%
881 \b@droparrowwidth=\wd\@tempboxa%
882 }%
883 % \end{macrocode}
884 % Now we get back to long division macros (yes, these do
885 % take a rather long time). We start with the macro for
886 % subsequent long division lines.
887 % \begin{macrocode}
888 \def\nextldline#1#2{%
889 \advance\b@linenum by1%
890 \b@ldrowlen=\b@digitwid%
891 \multiply\b@ldrowlen by#1%
892 \hbox{%
893 \hskip\b@divisorlen%
894 \hskip\b@ldrowlen%
895 \b@spaceout{#2}%
896 \b@droparrowlen=\baselineskip%
897 \ifshowdivisionwork%
898 \ifnum\b@linenum>2%
899 \getdroparrowwidth%
900 \multiply\b@droparrowlen by\b@linenum%
901 \advance\b@droparrowlen by-2\baselineskip%
902 \hskip-0.5\b@digitwid%
903 \vtop{\vskip-\baselineskip\vskip-\b@droparrowlen%
904 \rlap{%
905 \vrule width0.4pt height\b@droparrowlen%
906 \hskip-0.5\b@droparrowwidth{$\downarrow$}%
907 }
908 }%
909 \fi%
910 \fi%
911 }%
912 }%
913 % \end{macrocode}
914 % Now we move on to the styling macros; this requires a lot
915 % of looping and other weirdities (weirdities for \TeX\
916 % programming, anyway). First we style lines; then we style
917 % columns.
918 % \begin{macrocode}
919 \def\linestyle#1#2{%
920 \b@loopi=0%
921 \loop\ifnum\the\b@loopi<\the\b@maxrows%
922 \advance\b@loopi by1%
923 \ifnum#1=\the\b@loopi
924 \expandafter\def\csname speciallinestyle\romannumeral\b@loopi\endcsname{#2}%
925 \fi
926 \repeat
927 }%
928 \def\digstyle#1#2{%
929 \b@loopi=0%
930 \loop\ifnum\the\b@loopi<\the\b@maxcols%
931 \advance\b@loopi by1%
932 \ifnum#1=\the\b@loopi
933 \expandafter\def\csname specialdigitstyle\romannumeral\b@loopi\endcsname{#2}%
934 \fi
935 \repeat
936 }%
937 % \end{macrocode}
938 % Now we get our commands to clear the styling; again, first
939 % lines, then columns.
940 % \begin{macrocode}
941 \def\clearlinestyles{%
942 \b@loopi=0%
943 \loop\ifnum\the\b@loopi<\the\b@maxrows%
944 \advance\b@loopi by1%
945 \expandafter\def\csname speciallinestyle\romannumeral\b@loopi\endcsname{}%
946 \repeat
947 }%
948 \def\cleardigitstyles{%
949 \b@loopi=0%
950 \loop\ifnum\the\b@loopi<\the\b@maxcols%
951 \advance\b@loopi by1%
952 \expandafter\def\csname specialdigitstyle\romannumeral\b@loopi\endcsname{}%
953 \repeat
954 }%
955 % \end{macrocode}
956 % A few miscellanies; a box for binding together problems,
957 % so that they can be positioned on the page more easily; a
958 % macro for doing strikethroughts, quite useful in the
959 % carries; and macros for showing or not showing our long
960 % division work.
961 % \begin{macrocode}
962 \def\problembox#1{%
963 \leavevmode\vbox{#1}%
964 }%
965 \def\strike#1{%
966 {\rlap{\bf---}#1}%
967 }
968 \def\showdivwork{%
969 \showdivisionworktrue%
970 }
971 \def\noshowdivwork{%
972 \showdivisionworkfalse%
973 }
974 % \end{macrocode}
975 % Now, finally, we clear all the digit styles (so that they
976 % at least exist), and then we're done. Thanks for reading;
977 % happy \TeX{}ing!
978 % \begin{macrocode}
979 \cleardigitstyles
980 % \end{macrocode}
981 % \PrintIndex