[pstricks] Cascading divisions (using PSTricks nodes and the arrayjob package)

Denis Girou Denis.Girou at idris.fr
Wed Apr 4 21:40:22 CEST 2001

  Some weeks ago, a student ask on one french TeX mailing list how to build
cascading divisions with LaTeX.

  I never use them, as probably everybody here!, but this is a good
programming example and not a very easy problem, if of course we want
a generic solution which compute automatically all the intermediate
divisions and position correctly all the numbers.

  I solve this problem using PSTricks nodes and the great `arrayjob' package
from Zhuhan Jiang, allowing also to break the lines for long series of
divisions. The main weakness that I see is that the bounding box can't be
computed and must be defined explicitely by an external "pspicture"
environment if needed, but this is easy to do.

  P.S. * Comparing to the version I sent on the GUTenberg mailing list,
I change the code to remove all LaTeX dependencies, so it is now a generic
macro usable with Plain and ConTeXt (even if some examples that I give here
use sometimes other LaTeX macros or environments), but the code is a little
more complicated.
       * Yes, the code is rather technical and probably not easy to
understand... Nevertheless, as all examples, it can give ideas to some people
to solve other problems.



\usepackage{arrayjob}           % For arrays management
\usepackage{calc}               % For computations on LaTeX lengths
\usepackage{multido}            % For loop structure
\usepackage{pstcol}             % PSTricks with the `color' interface
\usepackage{pst-key}            % PSTricks interface to `keyval'
\usepackage{pst-node}           % PSTricks nodes


% To remove a spurious blank in `arrayjob' 1.03
\def\one at VEC#1,#2\safty at mark{\def\temp at macro{#2}\temp at toks={#2}%
  \ifx\temp at macro\empty \one at VECtrue \else \one at VECfalse \fi
  \temp at count=#1\relax}

% To allow the expansion of the content of an array

\def\read at array[#1]#2#3{\temp at count=1
  \def\one at item##1#1##2\safty at mark{\def\temp at macro{##2}\temp at toks={##2}%
  \ifx\temp at macro\empty\else
    \def\temp at macro{\one at item##2\safty at mark}\fi
    {\temp at toks={##1}\def\temp@@macro{\array{#2}(\the\temp at count)=}%
     \expandafter\temp@@macro\expandafter{\the\temp at toks}}%
  \expandafter\edef\csname total@#2\endcsname{\the\temp at count}%
  \advance\temp at count1\temp at macro}%
% D.G. modification begin - Mar. 13, 2001
%  \one at item#3#1\safty at mark}%
    \expandafter\one at item#3#1\safty at mark%
    \one at item#3#1\safty at mark%
% D.G. modification end

% Line broken after these specified numbers of divisors
\define at key{psset}{CascadingDivisionBreaks}{%
\edef\CascadingDivision at Breaks{#1}}

% Inter column space for divisions
\newdimen\CascadingDivision at InterColumnSpace
\define at key{psset}{CascadingDivisionInterColumnSpace}{%
\pssetlength{\CascadingDivision at InterColumnSpace}{#1}}

% Inter row space for divisions
\newdimen\CascadingDivision at InterRowSpace
\define at key{psset}{CascadingDivisionInterRowSpace}{%
\pssetlength{\CascadingDivision at InterRowSpace}{#1}}

% Inter column and row space (set equal) for divisions
\define at key{psset}{CascadingDivisionInterColumnRowSpace}{%
\pssetlength{\CascadingDivision at InterColumnSpace}{#1}%
\pssetlength{\CascadingDivision at InterRowSpace}{#1}}

% Font to use for all the numbers
\define at key{psset}{CascadingDivisionFont}{%
\def\CascadingDivision at Font{#1}}

% Style to print the remainders of the divisions
\define at key{psset}{CascadingDivisionRemainderStyle}{%
\let\CascadingDivision at RemainderStyle#1}

% Default values: current font, no line break, 0.2 units between columns
%                 and rows, remainders in default text style

\SpecialCoor                    % To use PSTricks nodes as coordinates

\newarray\CD at Divisors           % Internal array to store the divisors
\newarray\CD at DivisorsByLine     % Internal array to store the divisors by line

\@ifnextchar[{\CascadingDivision at i}{\CascadingDivision at i[]}}

\def\CascadingDivision at i[#1]#2#3{{%
% #1 = number, #2 = list of divisors, separated by the & character,
\setkeys{psset}{#1}%            % Store local parameter values
\def\arraystretch{0}%           % No automatic spaces between rows in "tabular"
  \readarray{CD at DivisorsByLine}{\CascadingDivision at Breaks}% Store these numbers
\pst at cntc=\@ne                  % First index to use in this array
\pst at cntd=\z@                   % To count the divisions inside a line
\pnode{CD at Division}             % Initialize the starting node
\pnode{CD at DivisionInit}         % Idem (useful if we must break the line)
% Erase the preceding possible array values (to delete the array is not enough)
\multido{\iDivisor=\@ne+\@ne}{100}{\CD at Divisors(\iDivisor)={}}
\pst at cnta=#2                    % Initial number to divide
\readarray{CD at Divisors}{#3}     % Store the divisors in an array
\CascadingDivision at Font         % Font to use to print all the numbers
  \checkCD at Divisors(\iDivisor)% % Next divisor
  \ifemptydata                  % Do we reach the last divisor?
    \multidostop                % Yes: it is finished
    \advance\pst at cntd\@ne       % No: one more division
    \rput[lt](CD at Division){%
      % Matrix environment to show the 4 values
      \psmatrix[mcol=r,colsep=\CascadingDivision at InterColumnSpace,
                       rowsep=\CascadingDivision at InterRowSpace]
        \the\pst at cnta & \Rnode[vref=1.6ex]{CD at NodeA}{} &
                        [mcol=l]\Rnode[href=1]{CD at NodeB}{\cachedata} \\[-2ex]
                      & \Rnode{CD at NodeC}{} & \Rnode{CD at NodeD}{} \\[-2ex]
        \pst at cntg=\pst at cnta
        \global\divide\pst at cnta\cachedata
        \pst at cnth=\pst at cnta
        \multiply\pst at cnth\cachedata
        \advance\pst at cntg-\pst at cnth
        \CascadingDivision at RemainderStyle{\the\pst at cntg} & \pnode{CD at NodeE} &
          [mcol=l]\rnode[lt]{CD at Division}{%
                    \Rnode[href=1]{CD at NodeF}{\the\pst at cnta}}
        \psline(CD at NodeA)(CD at NodeE) % Vertical line
        % Horizontal line (it depend if denominator or numerator is the longest)
        \psline(CD at NodeC)(CD at NodeB|CD at NodeD)
        \psline(CD at NodeC)(CD at NodeF|CD at NodeD)
        % Look if we must break the line
        \checkCD at DivisorsByLine(\pst at cntc)%
        \pst at cnth=\pst at cntd
        \divide\pst at cnth\cachedata
        \multiply\pst at cnth\cachedata
        \pst at cntg=\pst at cntd
        \advance\pst at cntg-\pst at cnth
        \ifnum\pst at cntg=\z@     % Yes, we must break the line
          \global\pst at cntd=\z@  % As we will start a new line next division
          \global\advance\pst at cntc\@ne % Next number of divisors by line
          \checkCD at DivisorsByLine(\pst at cntc)%
            \global\advance\pst at cntc\m at ne% If it doesn't exist, kept last one
          % Insert dots for continuation sign, but not if last divisor!
          \pst at cnth=\iDivisor
          \advance\pst at cnth\@ne
          \checkCD at Divisors(\pst at cnth)% Will be the next divisor
          \ifemptydata          % No dots if last divisor!
            \hbox{~$\ldots$}% Insert dots
          \pnode(CD at DivisionInit|0,-0.5){CD at Division}% Breaks the line















More information about the PSTricks mailing list