1 % \iffalse meta-comment
5 % The LaTeX3 Project and any individual authors listed elsewhere
8 % This file is part of the Standard LaTeX `Tools Bundle'.
9 % -------------------------------------------------------
11 % It may be distributed and/or modified under the
12 % conditions of the LaTeX Project Public License, either version 1.3c
13 % of this license or (at your option) any later version.
14 % The latest version of this license is in
15 % http://www.latex-project.org/lppl.txt
16 % and version 1.3c or later is part of all distributions of LaTeX
17 % version 2005/12/01 or later.
19 % The list of all files belonging to the LaTeX `Tools Bundle' is
20 % given in the file `manifest.txt'.
25 %% Copyright (C) 1992--1995
26 %% Kresten Krab Thorup and Frank Jensen.
27 %% Copyright (C) 1997--2007
28 %% Kresten Krab Thorup, Frank Jensen and the LaTeX3 Project.
30 %% The original authors (fj@hugin.dk and krab@daimi.aau.dk) have
31 %% contributed this package to the LaTeX distribution.
32 %% Problems with this package should now be sent using latexbug.tex to
33 %% the normal LaTeX bug report address.
36 \ProvidesFile{calc.dtx}
38 %<package>\NeedsTeXFormat{LaTeX2e}
39 %<package>\ProvidesPackage{calc}
40 %<driver> \ProvidesFile{calc.drv}
42 % \ProvidesFile{calc.dtx}
43 [2014/10/28 v4.3 Infix arithmetic (KKT,FJ)]
47 \documentclass{ltxdoc}
57 % \GetFileInfo{calc.dtx}
59 % \title{The \texttt{calc} package\\Infix notation
60 % arithmetic in \LaTeX\thanks{We thank Frank Mittelbach for his
61 % valuable comments and suggestions which have greatly improved
63 % \author{Kresten Krab Thorup, Frank Jensen (and Chris Rowley)}
66 % \MaintainedByLaTeXTeam{tools}
69 % \changes{v4.0d}{1997/11/08}
70 % {Contributed to tools distribution}
71 % \changes{v4.1a}{1998/06/07}
72 % {Added text sizes: CAR}
73 % \changes{v4.1a}{1998/06/07}
74 % {Attempt to make user-syntax robust: CAR}
76 % \newenvironment{calc-syntax}
78 % \parskip\medskipamount
79 % \def\is{\ \hangindent3\parindent$\longrightarrow$~}%
80 % \def\alt{\ $\vert$~}%
81 % \rightskip 0pt plus 1fil
82 % \def\<##1>{\mbox{\NormalSpaces$\langle$##1\/$\rangle$}}%
83 % \IgnoreSpaces\obeyspaces%
84 % }{\par\vskip\parskip}
85 % {\obeyspaces\gdef\NormalSpaces{\let =\space}\gdef\IgnoreSpaces{\def {}}}
87 % \def\<#1>{$\langle$#1\/$\rangle$}%
88 % \def\s#1{\ensuremath{[\![#1]\!]}}
89 % \def\savecode#1{\hbox{${}_{\hookrightarrow[#1]}$}}
90 % \def\gassign{\Leftarrow}
91 % \def\lassign{\leftarrow}
94 % The \texttt{calc} package reimplements the \LaTeX\ commands
95 % |\setcounter|, |\addtocounter|, |\setlength|, and |\addtolength|.
96 % Instead of a simple value, these commands now accept an infix
97 % notation expression.
100 % \section{Introduction}
102 % Arithmetic in \TeX\ is done using low-level operations such as
103 % |\advance| and |\multiply|. This may be acceptable when developing
104 % a macro package, but it is not an acceptable interface for the
107 % This package introduces proper infix notation arithmetic which is
108 % much more familiar to most people. The infix notation is more
109 % readable and easier to modify than the alternative: a sequence of
110 % assignment and arithmetic instructions. One of the arithmetic
111 % instructions (|\divide|) does not even have an equivalent in
114 % The infix expressions can be used in arguments to macros (the
115 % \texttt{calc} package doesn't employ category code changes to
116 % achieve its goals).\footnote{However, it therefore assumes that the
117 % category codes of the special characters, such as \texttt{(*/)}
118 % in its syntax do not change.}
120 % \section{Informal description}
122 % Standard \LaTeX\ provides the following set of commands to
123 % manipulate counters and lengths \cite[pages 194 and~216]{latexman}.
125 % \item[]\hskip-\leftmargin
126 % |\setcounter{|\textit{ctr}|}{|\textit{num}|}| sets the
127 % value of the counter \textit{ctr} equal to (the value of)
128 % \textit{num}. (Fragile)
129 % \item[]\hskip-\leftmargin
130 % |\addtocounter{|\textit{ctr}|}{|\textit{num}|}|
131 % increments the value of the counter \textit{ctr} by (the
132 % value of) \textit{num}. (Fragile)
134 % \item[]\hskip-\leftmargin
135 % |\setlength{|\textit{cmd}|}{|\textit{len}|}| sets the value of
136 % the length command \textit{cmd} equal to (the value of) \textit{len}.
138 % \item[]\hskip-\leftmargin
139 % |\addtolength{|\textit{cmd}|}{|\textit{len}|}| sets the value of
140 % the length command \textit{cmd} equal to its current value plus
141 % (the value of) \textit{len}. (Robust)
143 % (The |\setcounter| and |\addtocounter| commands have global effect,
144 % while the |\setlength| and |\addtolength| commands obey the normal
145 % scoping rules.) In standard \LaTeX, the arguments to these commands
146 % must be simple values. The \texttt{calc} package extends these
147 % commands to accept infix notation expressions, denoting values of
148 % appropriate types. Using the \texttt{calc} package, \textit{num} is
149 % replaced by \<integer expression>, and \textit{len} is replaced by
150 % \<glue expression>. The formal syntax of \<integer expression> and
151 % \<glue expression> is given below.
153 % In addition to these commands to explicitly set a length, many \LaTeX\
154 % commands take a length argument. After loading this package, most of
155 % these commands will accept a \<glue expression>. This includes
156 % the optional width argument of |\makebox|, the width argument of
157 % |\parbox|, |minipage|, and a |tabular| |p|-column, and many similar
158 % constructions. (This package does not redefine any of these commands,
159 % but they are defined by default to read their arguments by |\setlength|
160 % and so automatically benefit from the enhanced |\setlength| command
161 % provided by this package.)
163 % In the following, we shall use standard \TeX\ terminology. The
164 % correspondence between \TeX\ and \LaTeX\ terminology is as follows:
165 % \LaTeX\ counters correspond to \TeX's count registers; they hold
166 % quantities of type \<number>. \LaTeX\ length commands correspond to
167 % \TeX's dimen (for rigid lengths) and skip (for rubber lengths)
168 % registers; they hold quantities of types \<dimen> and \<glue>,
171 % \TeX\ gives us primitive operations to perform arithmetic on registers as
174 % \item addition and subtraction on all types of quantities without
176 % \item multiplication and division by an \emph{integer} can be
177 % performed on a register of any type;
178 % \item multiplication by a \emph{real} number (i.e., a number with a
179 % fractional part) can be performed on a register of any type,
180 % but the stretch and shrink components of a glue quantity are
183 % The \texttt{calc} package uses these \TeX\ primitives but provides a
184 % more user-friendly notation for expressing the arithmetic.
186 % An expression is formed of numerical quantities (such as explicit
187 % constants and \LaTeX\ counters and length commands) and binary
188 % operators (the tokens `\texttt{+}', `\texttt{-}', `\texttt{*}', and
189 % `\texttt{/}' with their usual meaning) using the familiar infix
190 % notation; parentheses may be used to override the usual precedences
191 % (that multiplication/division have higher precedence than
192 % addition/subtraction).
194 % Expressions must be properly typed. This means, e.g., that a dimen
195 % expression must be a sum of dimen terms: i.e., you cannot say
196 % `\texttt{2cm+4}' but `\texttt{2cm+4pt}' is valid.
198 % In a dimen term, the dimension part must come first; the same holds
199 % for glue terms. Also, multiplication and division by non-integer
200 % quantities require a special syntax; see below.
202 % Evaluation of subexpressions at the same level of precedence
203 % proceeds from left to right. Consider a dimen term such as
204 % ``\texttt{4cm*3*4}''. First, the value of the factor \texttt{4cm} is
205 % assigned to a dimen register, then this register is multiplied
206 % by~$3$ (using |\multiply|), and, finally, the register is multiplied
207 % by~$4$ (again using |\multiply|). This also explains why the
208 % dimension part (i.e., the part with the unit designation) must come
209 % first; \TeX\ simply doesn't allow untyped constants to be assigned
210 % to a dimen register.
212 % The \texttt{calc} package also allows multiplication and division by
213 % real numbers. However, a special syntax is required: you must use
214 % |\real{|\<decimal constant>|}|\footnote{Actually, instead of
215 % \<decimal constant>, the more general \<optional signs>\<factor> can
216 % be used. However, that doesn't add any extra expressive power to
217 % the language of infix expressions.} or
218 % |\ratio{|\<dimen expression>|}{|\<dimen expression>|}| to denote a
219 % real value to be used for multiplication/division. The first form has
220 % the obvious meaning, and the second form denotes the number obtained
221 % by dividing the value of the first expression by the value of the
224 % A later addition to the package (in June 1998) allows an additional
225 % method of specifying a factor of type dimen by setting some text
226 % (in LR-mode) and measuring its dimensions: these are denoted as
229 % |\widthof{|\<text>|}|\quad
230 % |\heightof{|\<text>|}|\quad
231 % |\depthof{|\<text>|}|
233 % These calculate the natural sizes of the \<text> in exactly the
234 % same way as is done for the commands |\settowidth| etc.~on
235 % Page~216 of the manual~\cite{latexman}.
236 % In August 2005 the package was further extended to provide the command
238 % |\totalheightof{|\<text>|}|
240 % This command does exactly what you'd expect from its name.
241 % Additionally the package also provides the command
243 % |\settototalheight{|\<cmd>|}{|\<text>|}|
247 % Note that there is a small difference in the usage of these two
248 % methods of accessing text dimensions. After
249 % |\settowidth{\txtwd}{Some text}| you can use:
251 % \setlength{\parskip}{0.68\textwd}
253 % whereas using the more direct access to the width of the text
254 % requires the longer form for multiplication, thus:
256 % \setlength{\parskip}{\widthof{Some text} * \real{0.68}}
259 % \TeX\ discards the stretch and shrink components of glue when glue
260 % is multiplied by a real number. So, for example,
262 % \setlength{\parskip}{3pt plus 3pt * \real{1.5}}
264 % will set the paragraph separation to 4.5pt with no stretch or
265 % shrink. Incidentally, note how spaces can be used to enhance
266 % readability. When \TeX\ is scanning for a \<number> etc.\ it is
267 % common to terminate the scanning with a space token or by inserting
268 % \cs{relax}. As of version~4.3 \textsf{calc} allows \cs{relax} tokens
269 % to appear in places where they would usually be used for terminating
270 % \TeX's scanning. In short this is just before any of \texttt{+-*/)}
271 % or at the end of the expression being evaluated.
273 % When \TeX\ performs arithmetic on integers, any fractional part of
274 % the results are discarded. For example,
276 % \setcounter{x}{7/2}
277 % \setcounter{y}{3*\real{1.6}}
278 % \setcounter{z}{3*\real{1.7}}
280 % will assign the value~$3$ to the counter~\texttt{x}, the value~$4$
281 % to~\texttt{y}, and the value~$5$ to~\texttt{z}. This truncation
282 % also applies to \emph{intermediate} results in the sequential
283 % computation of a composite expression; thus, the following command
285 % \setcounter{x}{3 * \real{1.6} * \real{1.7}}
287 % will assign~$6$ to~\texttt{x}.
289 % As an example of the use of |\ratio|, consider the problem of
290 % scaling a figure to occupy the full width (i.e., |\textwidth|) of
291 % the body of a page. Assume that the original dimensions of the
292 % figure are given by the dimen (length) variables, |\Xsize| and
293 % |\Ysize|. The height of the scaled figure can then be expressed by
295 % \setlength{\newYsize}{\Ysize*\ratio{\textwidth}{\Xsize}}
300 % Another new feature introduced in August 2005 was $\max$ and $\min$
301 % operations with associated macros
303 % |\maxof{|\<\textit{type} expression>|}{|\<\textit{type} expression>|}|
305 % |\minof{|\<\textit{type} expression>|}{|\<\textit{type} expression>|}|
307 % When \textit{type} is either \meta{glue} or \meta{dimen} these macros
308 % are allowed only as part of addition or subtraction but when
309 % \textit{type} is \meta{integer} they can also be used when
310 % multiplying and dividing. In the latter case they follow the
311 % same syntax rules as |\ratio| and |\real| which means they must come
312 % after the |*| or the |/|. Thus
314 % \setcounter{x}{3*\maxof{4+5}{3*4}+\minof{2*\real{1.6}}{5-1}}
316 % will assign $3\times\max(9,12)+\min(3,4)=39$ to |x|. Similarly
318 % \setlength{\parindent}{%
319 % \minof{3pt}{\parskip}*\real{1.5}*\maxof{2*\real{1.6}}{2-1}}
321 % will assign $\min(13.5\textrm{pt},4.5\cs{parskip})$ to \cs{parindent}
325 % \section{Formal syntax}
327 % The syntax is described by the following set of rules.
328 % Note that the definitions of \<number>, \<dimen>, \<glue>,
329 % \<decimal constant>, and \<plus or minus> are
330 % as in Chapter~24 of The \TeX book~\cite{texbook}; and \<text>
331 % is LR-mode material, as in the manual~\cite{latexman}.
332 % We use \textit{type} as a meta-variable, standing for
333 % `integer', `dimen', and `glue'.\footnote{This version of the
334 % \texttt{calc} package doesn't support evaluation of muglue expressions.}
336 % \begin{calc-syntax}
337 % \<\textit{type} expression>^^A
338 % \is \<\textit{type} term>^^A
339 % \alt \<\textit{type} expression> \<plus or minus> \<\textit{type} term>
341 % \<\textit{type} term>^^A
342 % \is \<\textit{type} term> \<\textit{type} scan stop>
343 % \alt \<\textit{type} factor>^^A
344 % \alt \<\textit{type} term> \<multiply or divide> \<integer>^^A
345 % \alt \<\textit{type} term> \<multiply or divide> \<real number>^^A
346 % \alt \<\textit{type} term> \<multiply or divide>^^A
347 % \<$\max$ or $\min$ integer>^^A
349 % \<\textit{type} scan stop>^^A
351 % \alt \<optional space>^^A
354 % \<\textit{type} factor>^^A
355 % \is \<\textit{type}>^^A
356 % \alt \<text dimen factor>^^A
357 % \alt \<$\max$ or $\min$ \textit{type}>^^A
358 % \alt |(|$_{12}$ \<\textit{type} expression> |)|$_{12}$
360 % \<integer> \is \<number>
362 % \<$\max$ or $\min$ \textit{type}> \is \<$\max$ or $\min$ command>^^A
363 % |{| \<\textit{type} expression> |}|^^A
364 % |{| \<\textit{type} expression> |}|
366 % \<$\max$ or $\min$ command> \is |\maxof|^^A
369 % \<text dimen factor>^^A
370 % \is \<text dimen command>|{| \<text> |}|
372 % \<text dimen command>^^A
374 % \alt |\heightof|^^A
376 % \alt |\totalheightof|^^A
378 % \<multiply or divide>^^A
383 % \is |\ratio{| \<dimen expression> |}{| \<dimen expression> |}|^^A
384 % \alt |\real{| \<optional signs> \<decimal constant> |}|
386 % \<plus or minus>^^A
390 % \<decimal constant>^^A
393 % \alt \<digit> \<decimal constant>^^A
394 % \alt \<decimal constant> \<digit>
408 % \<optional signs>^^A
409 % \is \<optional spaces>^^A
410 % \alt \<optional signs> \<plus or minus> \<optional spaces>
415 % Relying heavily on \TeX\ to do the underlying assignments, it is
416 % only natural for \texttt{calc} to simulate \TeX's parsing machinery
417 % for these quantities. Therefore it a)~imposes the same restrictions
418 % on the catcode of syntax characters as \TeX\ and b)~tries to expand
419 % its argument fully. a)~means that implicit characters for the tokens
420 % |*|$_{12}$, |/|$_{12}$, |(|$_{12}$, and |)|$_{12}$ will not
421 % work\footnote{e\TeX\ also assumes these catcodes when parsing a
422 % \cs{numexpr}, \cs{dimexpr}, \cs{glueexpr}, or \cs{muglueexpr} and
423 % does not allow implicit characters.} but because of~b), the
424 % expansion should allow you to use macros that expand to explicit
429 % \begin{thebibliography}{1}
431 % \textsc{D. E. Knuth}.
432 % \newblock \textit{The \TeX{}book} (Computers \& Typesetting Volume A).
433 % \newblock Addison-Wesley, Reading, Massachusetts, 1986.
435 % \textsc{L. Lamport}.
436 % \newblock \textit{\LaTeX, A Document Preparation System.}
437 % \newblock Addison-Wesley, Reading, Massachusetts, Second
439 % \end{thebibliography}
443 % \section{The evaluation scheme}
444 % \label{evaluation:scheme}
446 % In this section, we shall for simplicity consider only expressions
447 % containing `$+$' (addition) and `$*$' (multiplication) operators.
448 % It is trivial to add subtraction and division.
450 % An expression $E$ is a sum of terms: $T_1+\cdots+T_n$; a term is a
451 % product of factors: $F_1*\cdots*F_m$; a factor is either a simple
452 % numeric quantity~$f$ (like \<number> as described in the \TeX book),
453 % or a parenthesized expression~$(E')$.
455 % Since the \TeX\ engine can only execute arithmetic operations in a
456 % machine-code like manner, we have to find a way to translate the
457 % infix notation into this `instruction set'.
459 % Our goal is to design a translation scheme that translates~$X$ (an
460 % expression, a term, or a factor) into a sequence of \TeX\ instructions
461 % that does the following [Invariance Property]: correctly
462 % evaluates~$X$, leaves the result in a global register~$A$ (using a
463 % global assignment), and does not perform global assignments to the
464 % scratch register~$B$; moreover, the code sequence must be balanced
465 % with respect to \TeX\ groups. We shall denote the code sequence
466 % corresponding to~$X$ by \s{X}.
468 % In the replacement code specified below, we use the following
471 % \item $A$ and $B$ denote registers; all assignments to~$A$ will
472 % be global, and all assignments to~$B$ will be local.
473 % \item ``$\gassign$'' means global assignment to the register on
475 % \item ``$\lassign $'' means local assignment to the register on
477 % \item ``\savecode C'' means ``save the code~$C$ until the current
478 % group (scope) ends, then execute it.'' This corresponds to
479 % the \TeX-primitive |\aftergroup|.
480 % \item ``$\{$'' denotes the start of a new group, and ``$\}$''
481 % denotes the end of a group.
484 % Let us consider an expression $T_1+T_2+\cdots+T_n$. Assuming that
485 % \s{T_k} ($1\le k\le n$) attains the stated goal, the following code
486 % clearly attains the stated goal for their sum:
488 % \s{T_1+T_2+\cdots+T_n}&\Longrightarrow&
489 % \{\,\s{T_1}\,\} \; B\lassign A \quad
490 % \{\,\s{T_2}\,\} \; B\lassign B+A \\
491 % &&\qquad \ldots \quad \{\,\s{T_n}\,\} \; B\lassign B+A
494 % Note the extra level of grouping enclosing each of \s{T_1}, \s{T_2},
495 % \ldots,~\s{T_n}. This will ensure that register~$B$, used to
496 % compute the sum of the terms, is not clobbered by the intermediate
497 % computations of the individual terms. Actually, the group
498 % enclosing~\s{T_1} is unnecessary, but it turns out to be simpler if
499 % all terms are treated the same way.
501 % The code sequence ``$\{\,\s{T_2}\,\}\;B\lassign B+A$'' can be translated
502 % into the following equivalent code sequence:
503 % ``$\{\savecode{B\lassign B+A}\,\s{T_2}\,\}$''. This observation turns
504 % out to be the key to the implementation: The ``$\savecode{B\lassign
505 % B+A}$'' is generated \emph{before} $T_2$ is translated, at the same
506 % time as the `$+$' operator between $T_1$ and~$T_2$ is seen.
508 % Now, the specification of the translation scheme is straightforward:
510 % \s{f}&\Longrightarrow&A\gassign f\\[\smallskipamount]
511 % \s{(E')}&\Longrightarrow&\s{E'}\\[\smallskipamount]
512 % \s{T_1+T_2+\cdots+T_n}&\Longrightarrow&
513 % \{\savecode{B\lassign A}\,\s{T_1}\,\} \quad
514 % \{\savecode{B\lassign B+A}\,\s{T_2}\,\} \\
515 % &&\qquad \ldots \quad \{\savecode{B\lassign B+A}\,\s{T_n}\,\}
517 % \\[\smallskipamount]
518 % \s{F_1*F_2*\cdots*F_m}&\Longrightarrow&
519 % \{\savecode{B\lassign A}\,\s{F_1}\,\} \quad
520 % \{\savecode{B\lassign B*A}\,\s{F_2}\,\}\\
521 % &&\qquad \ldots \quad \{\savecode{B\lassign B*A}\,\s{F_m}\,\}
524 % By structural induction, it is easily seen that the stated property
527 % By inspection of this translation scheme, we see that we have to
528 % generate the following code:
530 % \item we must generate ``$\{\savecode{B\lassign
531 % A}\{\savecode{B\lassign A}$'' at the left border of an
532 % expression (i.e., for each left parenthesis and the implicit
533 % left parenthesis at the beginning of the whole expression);
534 % \item we must generate ``$\}A\gassign B\}A\gassign B$'' at the
535 % right border of an expression (i.e., each right parenthesis
536 % and the implicit right parenthesis at the end of the full
538 % \item `\texttt{*}' is replaced by ``$\}\{\savecode{B\lassign
540 % \item `\texttt{+}' is replaced by
541 % ``$\}A\gassign B\}\{\savecode{B\lassign
542 % B+A}\{\savecode{B\lassign A}$'';
543 % \item when we see (expect) a numeric quantity, we insert the
544 % assignment code ``$A\gassign$'' in front of the quantity and let
548 % \section{Implementation}
551 % \begin{calc-syntax}
552 % \<numeric> \is \<number> \alt \<dimen> \alt \<glue> \alt \<muglue>
554 % So far we have ignored the question of how to determine the type of
555 % register to be used in the code. However, it is easy to see that
556 % (1)~`$*$' always initiates an \<integer factor>, (2)~all
557 % \<numeric>s in an expression, except those which are part of an
558 % \<integer factor>, are of the same type as the whole expression, and
559 % all \<numeric>s in an \<integer factor> are \<number>s.
561 % We have to ensure that $A$ and~$B$ always have an appropriate type
562 % for the \<numeric>s they manipulate. We can achieve this by having
563 % an instance of $A$ and~$B$ for each type. Initially, $A$~and~$B$
564 % refer to registers of the proper type for the whole expression.
565 % When an \<integer factor> is expected, we must change $A$ and~$B$ to
566 % refer to integer type registers. We can accomplish this by
567 % including instructions to change the type of $A$ and~$B$ to integer
568 % type as part of the replacement code for~`$*$; if we append such
569 % instructions to the replacement code described above, we also ensure
570 % that the type-change is local (provided that the type-changing
571 % instructions only have local effect). However, note that the
572 % instance of~$A$ referred to in $\savecode{B\lassign B*A}$ is the
573 % integer instance of~$A$.
575 % We shall use |\begingroup| and |\endgroup| for the open-group and
576 % close-group characters. This avoids problems with spacing in math
577 % (as pointed out to us by Frank Mittelbach).
579 % \subsection{Getting started}
581 % Now we have enough insight to do the actual implementation in \TeX.
582 % First, we announce the macro package.\footnote{Code moved to top of file}
585 %\NeedsTeXFormat{LaTeX2e}
586 %\ProvidesPackage{calc}[\filedate\space\fileversion]
589 % \subsection{Assignment macros}
591 % \begin{macro}{\calc@assign@generic}
592 % \changes{v4.2}{2005/08/06}{Removed a few redundant \cs{expandafter}s}
593 % The |\calc@assign@generic| macro takes four arguments: (1~and~2) the
594 % registers to be used
595 % for global and local manipulations, respectively; (3)~the lvalue
596 % part; (4)~the expression to be evaluated.
598 % The third argument (the lvalue) will be used as a prefix to a
599 % register that contains the value of the specified expression (the
602 % In general, an lvalue is anything that may be followed by a variable
603 % of the appropriate type. As an example, |\linepenalty| and
604 % |\global\advance\linepenalty| may both be followed by an \<integer
607 % The macros described below refer to the registers by the names
608 % |\calc@A| and |\calc@B|; this is accomplished by
609 % |\let|-assignments.
611 % As discovered in Section~\ref{evaluation:scheme}, we have to
613 % if the expression is parenthesized. As described below,
614 % |\calc@open| is the macro that replaces a left parenthesis by its
615 % corresponding \TeX\ code sequence. When the scanning process sees
616 % the exclamation point, it generates an |\endgroup| and stops. As we
617 % recall from Section~\ref{evaluation:scheme}, the correct expansion
619 % parenthesis is ``$\}A\gassign B\}A\gassign B$''. The remaining
620 % tokens of this expansion are inserted explicitly, except that the
621 % last assignment has been replaced by the lvalue part (i.e.,
622 % argument~|#3| of |\calc@assign@generic|) followed by |\calc@B|.
624 \def\calc@assign@generic#1#2#3#4{\let\calc@A#1\let\calc@B#2%
626 \global\calc@A\calc@B\endgroup#3\calc@B}
630 % \begin{macro}{\calc@assign@count}
631 % \begin{macro}{\calc@assign@dimen}
632 % \begin{macro}{\calc@assign@skip}
633 % We need three instances of the |\calc@assign@generic| macro,
634 % corresponding to the types \<integer>, \<dimen>, and \<glue>.
636 \def\calc@assign@count{\calc@assign@generic\calc@Acount\calc@Bcount}
637 \def\calc@assign@dimen{\calc@assign@generic\calc@Adimen\calc@Bdimen}
638 \def\calc@assign@skip{\calc@assign@generic\calc@Askip\calc@Bskip}
640 % \end{macro}\end{macro}\end{macro}
641 % These macros each refer to two registers, one
642 % to be used globally and one to be used locally.
643 % We must allocate these registers.
645 \newcount\calc@Acount \newcount\calc@Bcount
646 \newdimen\calc@Adimen \newdimen\calc@Bdimen
647 \newskip\calc@Askip \newskip\calc@Bskip
650 % \subsection{The \LaTeX\ interface}
652 % \begin{macro}{\setcounter}
653 % \begin{macro}{\addtocounter}
654 % \changes{v4.2}{2005/08/06}
655 % {Fix to make \cs{addtocounter} work with \texttt{amstext}}
656 % \begin{macro}{\steptocounter}
657 % \changes{v4.2}{2005/08/06}
658 % {Avoid redundant processing. PR/3795}
659 % \begin{macro}{\setlength}
660 % \begin{macro}{\addtolength}
661 % As promised, we redefine the following standard \LaTeX\ commands:
663 % |\addtocounter|, |\setlength|, and |\addtolength|.
665 \def\setcounter#1#2{\@ifundefined{c@#1}{\@nocounterr{#1}}%
666 {\calc@assign@count{\global\csname c@#1\endcsname}{#2}}}
669 \def\addtocounter#1#2{\@ifundefined{c@#1}{\@nocounterr{#1}}%
670 {\calc@assign@count{\global\advance\csname c@#1\endcsname}{#2}}}%
672 % We also fix \cs{stepcounter} to not go through the whole \texttt{calc}
675 \def\stepcounter#1{\@ifundefined {c@#1}%
677 {\global\advance\csname c@#1\endcsname \@ne
679 \let\@elt\@stpelt \csname cl@#1\endcsname
682 % If the \texttt{amstext} package is loaded we must add the
683 % |\iffirstchoice@| switch as well. We patch the commands this
684 % way since it's good practice when we know how many arguments they take.
686 \@ifpackageloaded{amstext}{%
687 \expandafter\def\expandafter\stepcounter
688 \expandafter#\expandafter1\expandafter{%
689 \expandafter\iffirstchoice@\stepcounter{#1}\fi
691 \expandafter\def\expandafter\addtocounter
692 \expandafter#\expandafter1\expandafter#\expandafter2\expandafter{%
693 \expandafter\iffirstchoice@\addtocounter{#1}{#2}\fi
698 \DeclareRobustCommand\setlength{\calc@assign@skip}
699 \DeclareRobustCommand\addtolength[1]{\calc@assign@skip{\advance#1}}
701 % (|\setlength| and |\addtolength| are robust according to
709 % \subsection{The scanner}
711 % We evaluate expressions by explicit scanning of characters. We do
712 % not rely on active characters for this.
714 % The scanner consists of two parts, |\calc@pre@scan| and
715 % |\calc@post@scan|; |\calc@pre@scan| consumes left parentheses, and
716 % |\calc@post@scan| consumes binary operator, |\real|, |\ratio|, and
717 % right parenthesis tokens.
719 % \begin{macro}{\calc@pre@scan}
720 % \begin{macro}{\@calc@pre@scan}
721 % \changes{v4.2}{2005/08/06}
722 % {Added macro and force expansion}
724 % Note that this is called at least once on every use of calc
725 % processing, even when none of the extended syntax is present; it
726 % therefore needs to be made very efficient.
728 % It reads the initial part of expressions, until some \<text dimen
729 % factor> or \<numeric> is seen; in fact, anything not explicitly
730 % recognized here is taken to be a \<numeric> of some sort as this
732 % `\texttt{+}' and unary `\texttt{-}' to be treated easily and
733 % correctly\footnote{In the few contexts where signs are allowed:
734 % this could, I think, be extended (CAR).} but means that anything
735 % illegal will simply generate a \TeX-level error, often a
736 % reasonably comprehensible one!
738 % The |\romannumeral-`\a| part is a little trick which forces expansion
739 % in case |#1| is a normal macro, something that occurs from time to
740 % time. A conditional test inside will possibly leave a trailing
741 % \cs{fi} but this remnant is removed later when \cs{calc@post@scan}
742 % performs the same trick.
744 % The many |\expandafter|s are needed to efficiently end the nested
745 % conditionals so that |\calc@textsize| and |\calc@maxmin@addsub| can
746 % process their argument.
747 % \changes{v4.1a}{1998/06/07}
748 % {Added code for text sizes: CAR}
749 % \changes{v4.1b}{1998/07/07}
750 % {Correction to ifx true case}
751 % \changes{v4.2}{2005/08/06}
752 % {Added \cs{maxof} and \cs{minof} operations}
754 \def\calc@pre@scan#1{%
755 \expandafter\@calc@pre@scan\romannumeral-`\a#1}
756 \def\@calc@pre@scan#1{%
758 \expandafter\calc@open
761 \expandafter\expandafter\expandafter\calc@textsize
764 \expandafter\expandafter\expandafter\expandafter
765 \expandafter\expandafter\expandafter\calc@maxmin@addsub
767 \calc@numeric% no \expandafter needed for this one.
776 % \begin{macro}{\calc@open}
777 % \begin{macro}{\calc@initB}
778 % |\calc@open| is used when there is a left parenthesis right ahead.
779 % This parenthesis is replaced by \TeX\ code corresponding to the code
780 % sequence ``$\{\savecode{B\lassign A}\{\savecode{B\lassign A}$''
781 % derived in Section~\ref{evaluation:scheme}. Finally,
782 % |\calc@pre@scan| is
785 \def\calc@open({\begingroup\aftergroup\calc@initB
786 \begingroup\aftergroup\calc@initB
788 \def\calc@initB{\calc@B\calc@A}
792 % \begin{macro}{\calc@numeric}
793 % |\calc@numeric| assigns the following value to |\calc@A| and then
794 % transfers control to |\calc@post@scan|.
796 \def\calc@numeric{\afterassignment\calc@post@scan \global\calc@A}
800 % \begin{macro}{\widthof}
801 % \begin{macro}{\heightof}
802 % \begin{macro}{\depthof}
803 % \changes{v4.1a}{1998/06/07}
804 % {Added macros: CAR}
805 % \begin{macro}{\totalheightof}
806 % \changes{v4.2}{2005/08/06}
808 % \changes{v4.2}{2005/08/06}
809 % {Added informative message for reserved macros}
811 % These do not need any particular definition when they are scanned
812 % so, for efficiency and robustness, we make them all equivalent to
813 % the same harmless (I hope) unexpandable command.\footnote{If this
814 % level of safety is not needed then the code can be sped up:
815 % CAR.} Thus the test in |\@calc@pre@scan| finds any of them.
817 % As we have to check for these commands explicitly we must ensure
818 % that our definition wins. Using \cs{newcommand} gives an error when
819 % loading \texttt{calc} and may be mildly surprising. This should be
820 % a little more informative.
822 \@for\reserved@a:=widthof,heightof,depthof,totalheightof,maxof,minof\do
823 {\@ifundefined{\reserved@a}{}{%
824 \PackageError{calc}{%
825 The\space calc\space package\space reserves\space the\space
826 command\space name\space `\@backslashchar\reserved@a'\MessageBreak
827 but\space it\space has\space already\space been\space defined\space
828 with\space the\space meaning\MessageBreak
829 `\expandafter\meaning\csname\reserved@a\endcsname'.\MessageBreak
830 This\space original\space definition\space will\space be\space lost}%
831 {If\space you\space need\space a\space command\space with\space
832 this\space definition,\space you\space must\space use\space a\space
833 different\space name.}}%
835 \let\widthof\ignorespaces
836 \let\heightof\ignorespaces
837 \let\depthof\ignorespaces
838 \let\totalheightof\ignorespaces
845 % \begin{macro}{\calc@textsize}
846 % \changes{v4.1a}{1998/06/07}
848 % \changes{v4.1a}{1998/06/07}
850 % \changes{v4.2}{2005/08/06}
851 % {Extended macro with \cs{totalheightof}}
852 % The presence of the above four commands invokes this code, where
853 % we must distinguish them from each other.
854 % This implementation is somewhat optimized by using low-level
855 % code from the commands |\settowidth|, etc.\footnote{It is based on
856 % suggestions by Donald Arseneau and David Carlisle.}
858 % Within the text argument we must restore the normal meanings of
859 % the four user-level commands since arbitrary material can appear
860 % in here, including further uses of calc.
862 \def\calc@textsize #1#2{%
867 \def\totalheightof{\ht\dp}%
869 % We must expand the argument one level if it's \cs{totalheightof}
870 % and it doesn't hurt the other three.
872 \expandafter\@settodim\expandafter{#1}%
875 \let\widthof\ignorespaces
876 \let\heightof\ignorespaces
877 \let\depthof\ignorespaces
878 \let\totalheightof\ignorespaces
885 % \begin{macro}{\calc@post@scan}
886 % \begin{macro}{\@calc@post@scan}
887 % \changes{v4.2}{2005/08/06}{Added macro and force expansion}
888 % \changes{v4.3}{2007/08/22}{Discard terminating \cs{relax} tokens and
889 % avoid extra error message from \cs{calc@next}}
890 % The macro |\calc@post@scan| is called right after a value has been
891 % read. At this point, a binary operator, a sequence of right
892 % parentheses, an optional \cs{relax}, and the end-of-expression mark
893 % (`|!|') is allowed.\footnote{Is \texttt{!} a good choice, CAR?}
894 % Depending on our findings, we call a suitable macro to generate the
895 % corresponding \TeX\ code (except when we detect the
896 % end-of-expression marker: then scanning ends, and control is
897 % returned to |\calc@assign@generic|).
899 % This macro may be optimized by selecting a different order of
900 % |\ifx|-tests. The test for `\texttt{!}' (end-of-expression) is
901 % placed first as it will always be performed: this is the only test
902 % to be performed if the expression consists of a single \<numeric>.
903 % This ensures that documents that do not use the extra expressive
904 % power provided by the \texttt{calc} package only suffer a minimum
905 % slowdown in processing time.
907 \def\calc@post@scan#1{%
908 \expandafter\@calc@post@scan\romannumeral-`\a#1}
909 \def\@calc@post@scan#1{%
910 \ifx#1!\let\calc@next\endgroup \else
911 \ifx#1+\let\calc@next\calc@add \else
912 \ifx#1-\let\calc@next\calc@subtract \else
913 \ifx#1*\let\calc@next\calc@multiplyx \else
914 \ifx#1/\let\calc@next\calc@dividex \else
915 \ifx#1)\let\calc@next\calc@close \else
916 \ifx#1\relax\let\calc@next\calc@post@scan \else
917 \def\calc@next{\calc@error#1}%
930 % \begin{macro}{\calc@add}
931 % \begin{macro}{\calc@subtract}
932 % \begin{macro}{\calc@generic@add}
933 % \begin{macro}{\calc@addAtoB}
934 % \begin{macro}{\calc@subtractAfromB}
935 % The replacement code for the binary operators `\texttt{+}' and
936 % `\texttt{-}' follow a common pattern; the only difference is the
937 % token that is stored away by |\aftergroup|. After this replacement
938 % code, control is transferred to |\calc@pre@scan|.
940 \def\calc@add{\calc@generic@add\calc@addAtoB}
941 \def\calc@subtract{\calc@generic@add\calc@subtractAfromB}
942 \def\calc@generic@add#1{\endgroup\global\calc@A\calc@B\endgroup
943 \begingroup\aftergroup#1\begingroup\aftergroup\calc@initB
945 \def\calc@addAtoB{\advance\calc@B\calc@A}
946 \def\calc@subtractAfromB{\advance\calc@B-\calc@A}
954 % \begin{macro}{\real}
955 % \begin{macro}{\ratio}
956 % \begin{macro}{\calc@ratio@x}
957 % \begin{macro}{\calc@real@x}
958 % The multiplicative operators, `\texttt{*}' and `\texttt{/}', may be
959 % followed by a |\real|, |\ratio|, |\minof|, or |\maxof| token. The
960 % last two of these control sequences are defined by \texttt{calc} as
961 % they are needed by the scanner for addition or subtraction while the
962 % first two are not defined (at least not by the \texttt{calc}
964 % unfortunately, leaves them highly non-robust. We therefore
965 % equate them to |\relax| but only if they have not already been
966 % defined\footnote{Suggested code from David Carlisle.}
967 % (by some other package: dangerous but possible!); this
968 % will also make them appear to be undefined to a \LaTeX{} user
969 % (also possibly dangerous).
970 % \changes{v4.1a}{1998/06/07}
971 % {Added macro set-ups to make them robust but undefined: CAR}
973 \ifx\real\@undefined\let\real\relax\fi
974 \ifx\ratio\@undefined\let\ratio\relax\fi
976 % In order to test for |\real| or |\ratio|, we define these
977 % two.\footnote{May not need the extra names, CAR?}
979 \def\calc@ratio@x{\ratio}
980 \def\calc@real@x{\real}
986 % \begin{macro}{\calc@multiplyx}
987 % \changes{v4.2}{2005/08/06}
988 % {Added $\protect\max$ and $\protect\min$ operations}
989 % \begin{macro}{\calc@dividex}
990 % \changes{v4.2}{2005/08/06}
991 % {Added $\protect\max$ and $\protect\min$ operations}
992 % Test which operator followed |*| or |/|. If none followed it's just
993 % a standard multiplication or division.
995 \def\calc@multiplyx#1{\def\calc@tmp{#1}%
996 \ifx\calc@tmp\calc@ratio@x \let\calc@next\calc@ratio@multiply \else
997 \ifx\calc@tmp\calc@real@x \let\calc@next\calc@real@multiply \else
998 \ifx\maxof#1\let\calc@next\calc@maxmin@multiply \else
999 \let\calc@next\calc@multiply
1004 \def\calc@dividex#1{\def\calc@tmp{#1}%
1005 \ifx\calc@tmp\calc@ratio@x \let\calc@next\calc@ratio@divide \else
1006 \ifx\calc@tmp\calc@real@x \let\calc@next\calc@real@divide \else
1007 \ifx\maxof#1\let\calc@next\calc@maxmin@divide \else
1008 \let\calc@next\calc@divide
1017 % \begin{macro}{\calc@multiply}
1018 % \begin{macro}{\calc@divide}
1019 % \begin{macro}{\calc@generic@multiply}
1020 % \begin{macro}{\calc@multiplyBbyA}
1021 % \begin{macro}{\calc@divideBbyA}
1022 % The binary operators `\texttt{*}' and `\texttt{/}' also insert code
1023 % as determined above. Moreover, the meaning of |\calc@A| and
1024 % |\calc@B| is changed as factors following a multiplication and
1025 % division operator always have integer type; the original meaning of
1026 % these macros will be restored when the factor has been read and
1029 \def\calc@multiply{\calc@generic@multiply\calc@multiplyBbyA}
1030 \def\calc@divide{\calc@generic@multiply\calc@divideBbyA}
1031 \def\calc@generic@multiply#1{\endgroup\begingroup
1032 \let\calc@A\calc@Acount \let\calc@B\calc@Bcount
1033 \aftergroup#1\calc@pre@scan}
1034 \def\calc@multiplyBbyA{\multiply\calc@B\calc@Acount}
1035 \def\calc@divideBbyA{\divide\calc@B\calc@Acount}
1037 % Since the value to use in the multiplication/division operation is
1038 % stored in the |\calc@Acount| register, the |\calc@multiplyBbyA| and
1039 % |\calc@divideBbyA| macros use this register.
1046 % \begin{macro}{\calc@close}
1047 % |\calc@close| generates code for a right parenthesis (which was
1048 % derived to be ``$\}A\gassign B\}A\gassign B$'' in
1049 % Section~\ref{evaluation:scheme}). After this code, the control is
1051 % |\calc@post@scan| in order to look for another right parenthesis or
1052 % a binary operator.
1055 {\endgroup\global\calc@A\calc@B
1056 \endgroup\global\calc@A\calc@B
1061 % \subsection{Calculating a ratio}
1063 % \begin{macro}{\calc@ratio@multiply}
1064 % \begin{macro}{\calc@ratio@divide}
1065 % When |\calc@post@scan| encounters a |\ratio| control sequence, it hands
1066 % control to one of the macros |\calc@ratio@multiply| or |\calc@ratio@divide|,
1067 % depending on the preceding character. Those macros both forward the
1068 % control to the macro |\calc@ratio@evaluate|, which performs two steps: (1) it
1069 % calculates the ratio, which is saved in the global macro token
1070 % |\calc@the@ratio|; (2) it makes sure that the value of |\calc@B| will be
1071 % multiplied by the ratio as soon as the current group ends.
1073 % The following macros call |\calc@ratio@evaluate| which multiplies
1074 % |\calc@B| by the ratio, but |\calc@ratio@divide| flips the arguments
1075 % so that the `opposite' fraction is actually evaluated.
1077 \def\calc@ratio@multiply\ratio{\calc@ratio@evaluate}
1078 \def\calc@ratio@divide\ratio#1#2{\calc@ratio@evaluate{#2}{#1}}
1082 % \begin{macro}{\calc@Ccount}
1083 % \begin{macro}{\calc@numerator}
1084 % \begin{macro}{\calc@denominator}
1085 % We shall need two registers for temporary usage in the
1086 % calculations. We can save one register since we can reuse
1089 \newcount\calc@Ccount
1090 \let\calc@numerator=\calc@Bcount
1091 \let\calc@denominator=\calc@Ccount
1096 % \begin{macro}{\calc@ratio@evaluate}
1097 % Here is the macro that handles the actual evaluation of ratios. The
1099 % this: First, the two expressions are evaluated and coerced to
1100 % integers. The whole procedure is enclosed in a group to be able to
1101 % use the registers |\calc@numerator| and |\calc@denominator| for temporary
1104 \def\calc@ratio@evaluate#1#2{%
1105 \endgroup\begingroup
1106 \calc@assign@dimen\calc@numerator{#1}%
1107 \calc@assign@dimen\calc@denominator{#2}%
1109 % Here we calculate the ratio. First, we check for negative numerator
1110 % and/or denominator; note that \TeX\ interprets two minus signs the
1111 % same as a plus sign. Then, we calculate the integer part.
1112 % The minus sign(s), the integer part, and a decimal point, form the
1113 % initial expansion of the |\calc@the@ratio| macro.
1115 \gdef\calc@the@ratio{}%
1116 \ifnum\calc@numerator<0 \calc@numerator-\calc@numerator
1117 \gdef\calc@the@ratio{-}%
1119 \ifnum\calc@denominator<0 \calc@denominator-\calc@denominator
1120 \xdef\calc@the@ratio{\calc@the@ratio-}%
1122 \calc@Acount\calc@numerator
1123 \divide\calc@Acount\calc@denominator
1124 \xdef\calc@the@ratio{\calc@the@ratio\number\calc@Acount.}%
1126 % Now we generate the digits after the decimal point, one at a time.
1127 % When \TeX\ scans these digits (in the actual multiplication
1128 % operation), it forms a fixed-point number with 16~bits for
1129 % the fractional part. We hope that six digits is sufficient, even
1130 % though the last digit may not be rounded correctly.
1132 \calc@next@digit \calc@next@digit \calc@next@digit
1133 \calc@next@digit \calc@next@digit \calc@next@digit
1136 % Now we have the ratio represented (as the expansion of the global
1137 % macro |\calc@the@ratio|) in the syntax \<decimal constant>
1138 % \cite[page~270]{texbook}. This is fed to |\calc@multiply@by@real|
1140 % perform the actual multiplication. It is important that the
1141 % multiplication takes place at the correct grouping level so that the
1142 % correct instance of the $B$ register will be used. Also note that
1143 % we do not need the |\aftergroup| mechanism in this case.
1145 \calc@multiply@by@real\calc@the@ratio
1150 % The |\begingroup| inserted before the |\calc@post@scan| will be
1151 % matched by the |\endgroup| generated as part of the replacement of a
1152 % subsequent binary operator or right parenthesis.
1153 % \begin{macro}{\calc@next@digit}
1155 \def\calc@next@digit{%
1156 \multiply\calc@Acount\calc@denominator
1157 \advance\calc@numerator -\calc@Acount
1158 \multiply\calc@numerator 10
1159 \calc@Acount\calc@numerator
1160 \divide\calc@Acount\calc@denominator
1161 \xdef\calc@the@ratio{\calc@the@ratio\number\calc@Acount}}
1164 % \begin{macro}{\calc@multiply@by@real}
1165 % In the following code, it is important that we first assign the
1166 % result to a dimen register. Otherwise, \TeX\ won't allow us to
1167 % multiply with a real number.
1169 \def\calc@multiply@by@real#1{\calc@Bdimen #1\calc@B \calc@B\calc@Bdimen}
1171 % (Note that this code wouldn't work if |\calc@B| were a muglue
1172 % register. This is the real reason why the \texttt{calc} package
1173 % doesn't support muglue expressions. To support muglue expressions
1174 % in full, the |\calc@multiply@by@real| macro must use a muglue register
1175 % instead of |\calc@Bdimen| when |\calc@B| is a muglue register;
1176 % otherwise, a dimen register should be used. Since integer
1177 % expressions can appear as part of a muglue expression, it would be
1178 % necessary to determine the correct register to use each time a
1179 % multiplication is made.)
1182 % \subsection{Multiplication by real numbers}
1184 % \begin{macro}{\calc@real@multiply}
1185 % \begin{macro}{\calc@real@divide}
1186 % This is similar to the |\calc@ratio@evaluate| macro above, except that
1187 % it is considerably simplified since we don't need to calculate the
1188 % factor explicitly.
1190 \def\calc@real@multiply\real#1{\endgroup
1191 \calc@multiply@by@real{#1}\begingroup
1193 \def\calc@real@divide\real#1{\calc@ratio@evaluate{1pt}{#1pt}}
1198 % \subsection{$\max$ and $\min$ operations}
1200 % \begin{macro}{\maxof}
1201 % \begin{macro}{\minof}
1202 % \changes{v4.2}{2005/08/06}
1204 % With version 4.2, the $\max$ and $\min$ operators were
1205 % added to \texttt{calc}. The user functions for them are \cs{maxof} and
1206 % \cs{minof} respectively.
1207 % These macros are internally similar to \cs{widthof} etc.\ in that they
1208 % are unexpandable and easily recognizable by the scanner.
1210 \let\maxof\@@italiccorr
1211 \let\minof\@@italiccorr
1217 % \begin{macro}{\calc@Cskip}
1218 % \begin{macro}{\ifcalc@count@}
1219 % The $\max$ and $\min$ operations take two arguments so we need an extra
1220 % \<skip> register. We also add a switch for determining when to perform
1221 % a \<skip> or a \<count> assignment.
1224 \newif\ifcalc@count@
1228 % \begin{macro}{\calc@maxmin@addsub}
1229 % \begin{macro}{\calc@maxmin@generic}
1230 % \changes{v4.2}{2005/08/06}{Macros added}
1231 % When doing addition or subtraction with a $\max$ or $\min$ operator, we
1232 % first check if |\calc@A| is a \<count> register or not and then set the
1233 % switch. Then call the real function which sets |\calc@A| to the desired
1234 % value and continue as usual with |\calc@post@scan|.
1236 \def\calc@maxmin@addsub#1#2#3{\begingroup
1237 \ifx\calc@A\calc@Acount%
1242 \calc@maxmin@generic#1{#2}{#3}%
1247 % Check the switch and do either \<count> or \<skip> assignments. Note that
1248 % |\maxof| and |\minof| are not set to |>| and |<| until after the
1249 % assignments, which ensures we can nest them without problems. Then set
1250 % |\calc@A| to the correct one.
1252 \def\calc@maxmin@generic#1#2#3{%
1255 \calc@assign@count\calc@Ccount{#2}%
1256 \calc@assign@count\calc@Bcount{#3}%
1257 \def\minof{<}\def\maxof{>}%
1258 \global\calc@A\ifnum\calc@Ccount#1\calc@Bcount
1259 \calc@Ccount\else\calc@Bcount\fi
1261 \calc@assign@skip\calc@Cskip{#2}%
1262 \calc@assign@skip\calc@Bskip{#3}%
1263 \def\minof{<}\def\maxof{>}%
1264 \global\calc@A\ifdim\calc@Cskip#1\calc@Bskip
1265 \calc@Cskip\else\calc@Bskip\fi
1273 % \begin{macro}{\calc@maxmin@divmul}
1274 % \begin{macro}{\calc@maxmin@multiply}
1275 % \begin{macro}{\calc@maxmin@divide}
1276 % \changes{v4.2}{2005/08/06}{Macros added}
1277 % When doing division or multiplication we must be using \<count> registers
1278 % so we set the switch. Other than that it is almost business as usual when
1279 % multiplying or dividing. |#1| is the instruction to either multiply or
1280 % divide |\calc@B| by |\calc@A|, |#2| is either |\maxof| or |\minof| which
1281 % is waiting in the input stream and |#3| and |#4| are the calc expressions.
1282 % We end it all as usual by calling |\calc@post@scan|.
1284 \def\calc@maxmin@divmul#1#2#3#4{%
1285 \endgroup\begingroup
1288 \calc@maxmin@generic#2{#3}{#4}%
1289 \endgroup\begingroup
1293 % The two functions called when seeing a |*| or a |/|.
1295 \def\calc@maxmin@multiply{\calc@maxmin@divmul\calc@multiplyBbyA}
1296 \def\calc@maxmin@divide {\calc@maxmin@divmul\calc@divideBbyA}
1302 % \section{Reporting errors}
1303 % \begin{macro}{\calc@error}
1304 % \changes{v4.0d}{1997/11/08}
1305 % {Use \cs{PackageError} for error messages (DPC)}
1306 % \changes{v4.0e}{1997/11/11}
1308 % If |\calc@post@scan| reads a character that is not one of `\texttt{+}',
1309 % `\texttt{-}', `\texttt{*}', `\texttt{/}', or `\texttt{)}', an error
1310 % has occurred, and this is reported to the user. Violations in the
1311 % syntax of \<numeric>s will be detected and reported by \TeX.
1312 % \changes{v4.1a}{1998/06/07}
1313 % {Improved, I hope, error message: CAR}
1316 \PackageError{calc}%
1317 {`#1' invalid at this point}%
1318 {I expected to see one of: + - * / )}}
1322 % \section{Other additions}
1323 % \begin{macro}{\@settodim}
1324 % \changes{v4.2}{2005/08/06}
1325 % {Changed kernel macro}
1326 % \begin{macro}{\settototalheight}
1327 % \changes{v4.2}{2005/08/06}
1329 % The kernel macro \cs{@settodim} is changed so that it runs through a list
1330 % containing \cs{ht}, \cs{wd}, and \cs{dp} and than advance the length
1331 % one step at a time. We just have to use a scratch register in case the
1332 % user decides to put in a \cs{global} prefix on the length register.
1333 % A search on the internet confirmed that some people do that kind of thing.
1335 \def\@settodim#1#2#3{%
1336 \setbox\@tempboxa\hbox{{#3}}%
1338 \@tf@r\reserved@a #1\do{%
1339 \advance\dimen@ii\reserved@a\@tempboxa}%
1341 \setbox\@tempboxa\box\voidb@x}
1343 % Now the user level macro is straightforward.
1345 \def\settototalheight{\@settodim{\ht\dp}}
1350 % That's the end of the package.