diff options
Diffstat (limited to 'Master/texmf-dist/source/latex/xfor/xfor.dtx')
-rw-r--r-- | Master/texmf-dist/source/latex/xfor/xfor.dtx | 283 |
1 files changed, 198 insertions, 85 deletions
diff --git a/Master/texmf-dist/source/latex/xfor/xfor.dtx b/Master/texmf-dist/source/latex/xfor/xfor.dtx index c49c8f2de61..9c5986dd6d2 100644 --- a/Master/texmf-dist/source/latex/xfor/xfor.dtx +++ b/Master/texmf-dist/source/latex/xfor/xfor.dtx @@ -1,12 +1,13 @@ %\iffalse % xfor.dtx generated using makedtx version 0.94b (c) Nicola Talbot % Command line args: -% -src "xfor.sty=>xfor.sty" -% -doc "manual.tex" +% -src "xfor.sty\Z=>xfor.sty" +% -src "sample.tex\Z=>sample.tex" +% -doc "xfor-manual.tex" % -author "Nicola Talbot" % -dir "source" % xfor -% Created on 2007/8/21 17:28 +% Created on 2009/1/26 14:47 %\fi %\iffalse %<*package> @@ -29,7 +30,7 @@ %\fi % \iffalse % Doc-Source file to use with LaTeX2e -% Copyright (C) 2007 Nicola Talbot, all rights reserved. +% Copyright (C) 2009 Nicola Talbot, all rights reserved. % \fi % \iffalse %<*driver> @@ -41,10 +42,10 @@ bookmarks, hyperindex=false, pdfauthor={Nicola L.C. Talbot}, - pdftitle={xfor: redefinition of '@for}, + pdftitle={xfor: redefinition of \string\@for}, pdfkeywords={LaTeX,package development}]{hyperref} -\CheckSum{117} +\CheckSum{197} \PageIndex \RecordChanges \newcommand*{\sty}[1]{\textsf{#1}} @@ -56,7 +57,7 @@ %\fi %\MakeShortVerb{"} % -% \title{xfor v1.03: Reimplementation of \cs{@for} to allow +% \title{xfor v1.04: Reimplementation of \cs{@for} to allow %premature termination of the loop} % \author{Nicola L.C. Talbot\\[10pt] %School of Computing Sciences\\ @@ -65,7 +66,7 @@ %NR4 7TJ. United Kingdom.\\ %\url{http://theoval.cmp.uea.ac.uk/~nlct/}} % -%\date{21st August 2007} +%\date{25th January 2009} %\maketitle %\tableofcontents % @@ -125,12 +126,13 @@ %which uses this method: %\begin{verbatim} % \newcommand{\insertinto}[2]{% -% \def\nlst{}% +% \def\nlst{}% new list initially empty % \@for\n:=#2\do{% % % store new list in \toks@ % \expandafter\toks@\expandafter{\nlst}% % % test current value against new value % \ifnum\n>#1\relax +% % new value needs to be inserted before current value % \edef\newstuff{\number#1,\n}% % % end for loop at the end of this iteration % \@endfortrue @@ -158,7 +160,7 @@ % \fi % \else % % wasn't prematurely terminated, so new value hasn't been added -% % add now. +% % so add now. % \expandafter\toks@\expandafter{\nlst}% % \ifx\nlst\@empty % \edef\nlst{\number#1}% @@ -232,7 +234,12 @@ %\ifx\@xfor@nextelement\@nnil \else ;\fi} %\makeatother % -% \StopEventually{\clearpage\phantomsection +%\section{Acknowledgements} +% +%Many thanks to Morten~H\o gholm for providing code to improve +%efficiency. +% +% \StopEventually{\phantomsection %\addcontentsline{toc}{section}{Change History} %\PrintChanges\phantomsection %\addcontentsline{toc}{section}{Index} @@ -246,123 +253,229 @@ %<*xfor.sty> % \end{macrocode} %\fi -% Declare package: +% Note that the internal macros used by \cs{@for} have changed +% in version 1.04. +% % \begin{macrocode} \NeedsTeXFormat{LaTeX2e} -\ProvidesPackage{xfor}[2007/08/21 v1.03 (NLCT)] +\ProvidesPackage{xfor}[2009/01/25 v1.04 (NLCT)] % \end{macrocode} -% Define a switch to determine if the for loop should be -% terminated: %\begin{macro}{\if@endfor} +% Define a switch to determine if the for loop should be +% stopped prematurely. % \begin{macrocode} \newif\if@endfor % \end{macrocode} %\end{macro} -% Redefine \cs{@for}, so that it resets \cs{if@endfor} -% and \cs{@forremainder}: +%\begin{macro}{\@gobbleseven} +%\changes{1.04}{2009 January 25}{added by Morten H\o gholm} +% Ignore seven arguments. +% \begin{macrocode} +\long\def\@gobbleseven#1#2#3#4#5#6#7{} +% \end{macrocode} +%\end{macro} %\begin{macro}{\@for} +% \cs{@for}\meta{cmd}":="\meta{list}\cs{do}\marg{body} +% test if the list is empty and then re-arrange +%\changes{1.04}{2009 January 25}{Modified by Morten H\o gholm to +% improve efficiency} % \begin{macrocode} -\long\def\@for#1:=#2\do#3{% -\@endforfalse -\def\@forremainder{}% -\expandafter\def\expandafter\@fortmp\expandafter{#2}% -\ifx\@fortmp\@empty -\else - \expandafter\@forloop#2,\@nil,\@nil\@@#1{#3}% -\fi +\long\def\@for#1:=#2\do{% +% \end{macrocode} +% Initialise +% \begin{macrocode} + \@endforfalse + \def\@forremainder{}% + \expandafter\def\expandafter\@fortmp\expandafter{#2}% +% \end{macrocode} +% If list is empty do nothing +% \begin{macrocode} + \ifx\@fortmp\@empty + \expandafter\@gobbleseven + \fi + \expandafter\@@for\expandafter#1\expandafter{#2}% } % \end{macrocode} %\end{macro} -% Redefine \cs{@fornoop} to be a long command: -%\begin{macro}{\@fornoop} +% +%\begin{macro}{\@@for} +% Initialise for loop +%\changes{1.04}{2009 January 25}{Added by Morten H\o gholm} % \begin{macrocode} -\long\def\@fornoop#1\@@#2#3{} +\long\def\@@for#1#2#3{% + \@forloop#1{#3}#2,\@nil,\@nil,% + \@xfor@endmarker% magic end marker +} % \end{macrocode} %\end{macro} -% Redefine \cs{@forloop} to check for \cs{if@endfor}: -%\begin{macro}{\@forloop} +% +%\begin{macro}{\@xfornoop} +%\changes{1.04}{2009 January 25}{second and third arguments dropped (MH)} +%\changes{1.04}{2009 January 25}{\cs{@fornoop} replaced by +% \cs{@xfornoop} to prevent conflict with \cs{@tfor}} +% Read up until magic end marker. % \begin{macrocode} -\long\def\@forloop#1,#2,#3\@@#4#5{% -\def#4{#1}% -\ifx#4\@nnil -\else - \@xfor@storenext#2,#3\@@% - #5% - \if@endfor - \@iforgatherrest#2,#3% - \else - \def#4{#2}% - \ifx#4\@nnil - \else - \@xfor@storenext#3,\@nil\@@% - #5% - \if@endfor - \@iforgatherrest#3% - \else - \@iforloop#3\@@#4{#5}% - \fi - \fi - \fi -\fi -} +\long\def\@xfornoop#1\@xfor@endmarker{} % \end{macrocode} %\end{macro} -%\changes{1.02}{2007 August 16}{Added \cs{@xfor@storenext}}% -%\begin{macro}{\@xfor@storenext} -% Stores first argument in \cs{@xfor@nextelement}. +%\begin{macro}{\@forloop} +%\cs{@forloop}\marg{var}\marg{action} +%\changes{1.04}{2009 January 25}{Modified by Morten H\o gholm to +% improve efficiency} +% \begin{macrocode} +\long\def\@forloop#1#2#3,#4,{% + \def#1{#3}% + \ifx#1\@nnil +% \end{macrocode} +% Grab the \cs{@xfor@endmarker} at the very end % \begin{macrocode} -\def\@xfor@storenext#1,#2\@@{% -\def\@xfor@nextelement{#1}% + \expandafter\@xfornoop + \fi +% \end{macrocode} +% Removed |\@xfor@storenext#4,\@nil|. Instead store next element +% in \cs{@xfor@nextelement}. +% \begin{macrocode} + \def\@xfor@nextelement{#4}% + #2% + \if@endfor + \expandafter\@iforgatherrest + \fi + \@forloop#1{#2}{#4},% } % \end{macrocode} %\end{macro} % -%\begin{macro}{\@forgatherrest} -% Get remainder of list (stores in \cs{@forremainder}): +%\begin{macro}{\@iforgatherrest} +% Gather the remainder (and store in \cs{@forremainder}) %\changes{1.01}{2007 July 11}{made long}% +%\changes{1.04}{2009 January 25}{argument syntax changed (MH)} % \begin{macrocode} -\long\def\@forgatherrest#1,\@nil,\@nil{\def\@forremainder{#1}} +\long\def\@iforgatherrest \@forloop#1#2#3,#4\@xfor@endmarker{% + \def\@fortmp{#3}% + \ifx\@fortmp\@nnil + \def\@forremainder{}% + \else + \@forgatherrest{#3},#4\@xfor@endmarker% + \fi +} % \end{macrocode} %\end{macro} % -%\begin{macro}{\@iforgatherrest} -% As above, but there may not be anything before \cs{@nil}: +%\begin{macro}{\@forgatherrest} +% Get remainder of list (stored in \cs{@forremainder}): %\changes{1.01}{2007 July 11}{made long}% +%\changes{1.04}{2009 January 25}{argument syntax changed (MH)} % \begin{macrocode} -\long\def\@iforgatherrest#1\@nil,\@nil{% -\def\@fortmp{#1}% -\ifx\@fortmp\@empty - \def\@forremainder{}% -\else - \@forgatherrest#1\@nil,\@nil -\fi +\long\def\@forgatherrest#1,\@nil,\@nil,\@xfor@endmarker{% + \def\@forremainder{#1}% } % \end{macrocode} %\end{macro} -% Redefine \cs{@iforloop} to check \cs{if@endfor}: -%\begin{macro}{\@iforloop} +% +%\changes{1.02}{2007 August 16}{Added \cs{@xfor@storenext}}% +%\changes{1.04}{2009 January 25}{Removed \cs{\@xfor@storenext}}% +%\changes{1.04}{2009 January 25}{removed \cs{@iforloop}} + +%\iffalse +% \begin{macrocode} +%</xfor.sty> +% \end{macrocode} +%\fi +%\iffalse % \begin{macrocode} -\long\def\@iforloop#1,#2\@@#3#4{% -\def#3{#1}% -\ifx#3\@nnil - \let\@ifornext\@fornoop +%<*sample.tex> +% \end{macrocode} +%\fi +%\section{Sample Document (sample.tex)} +% \begin{macrocode} +\listfiles +\documentclass{article} + +\usepackage{xfor} + +\makeatletter + +% \insertinto{new value}{list} + +\newtoks\tmptok + +\newcommand{\insertinto}[2]{% +\def\nlst{}% +\@for\n:=#2\do{% +% store new list in \tmptok +\expandafter\tmptok\expandafter{\nlst}% +% test current value against new value +\ifnum\n>#1\relax + \edef\newstuff{\number#1,\n}% + % end for loop at the end of this iteration + \@endfortrue +\else + \edef\newstuff{\n}% +\fi +% append new stuff to new list +\ifx\nlst\@empty + \edef\nlst{\newstuff}% \else - \@xfor@storenext#2,\@nil\@@% - #4\relax - \if@endfor - \@iforgatherrest#2\relax - \let\@ifornext\@fornoop + \edef\nlst{\the\tmptok,\newstuff}% +\fi +}% +% check to see if for loop was prematurely terminated +\if@endfor + % loop may have been terminated during final iteration, in + % which case \@forremainder is empty. + \ifx\@forremainder\@empty + % do nothing \else - \let\@ifornext\@iforloop + % loop prematurely ended, append remainder of original list + % to new list + \expandafter\tmptok\expandafter{\nlst}% + \edef\nlst{\the\tmptok,\@forremainder}% \fi +\else + % wasn't prematurely terminated, so new value hasn't been added. + % Add now. + \expandafter\tmptok\expandafter{\nlst}% + \ifx\nlst\@empty + \edef\nlst{\number#1}% + \else + \edef\nlst{\the\tmptok,\number#1}% + \fi \fi -\@ifornext#2\@@#3{#4}% +\global\let#2=\nlst } + +% \insertionsort{list} +% replaces list with sorted list + +\newcommand{\insertionsort}[1]{% +\def\sortedlist{}% +\@for\val:=#1\do{{\insertinto{\val}{\sortedlist}}}% +\let#1=\sortedlist} + +\makeatother + +\begin{document} +Unsorted list: +\def\mylist{4,2,7,1,10,11,20,15}\mylist. + +\insertionsort{\mylist}% +Sorted list: \mylist. + +Iterate through the list (next element in parentheses): +\makeatletter +\@for\n:=\mylist\do{% +\n +\ifx\@xfor@nextelement\@nnil + % last iteration +\else + (\@xfor@nextelement); +\fi +}. +\end{document} % \end{macrocode} -%\end{macro} %\iffalse % \begin{macrocode} -%</xfor.sty> +%</sample.tex> % \end{macrocode} %\fi %\Finale |