diff options
author | Karl Berry <karl@freefriends.org> | 2007-07-16 16:42:56 +0000 |
---|---|---|
committer | Karl Berry <karl@freefriends.org> | 2007-07-16 16:42:56 +0000 |
commit | ddc4d77ba98166cf86d502db2514f63216c9f134 (patch) | |
tree | 9d3405493dc35487b2d7a90ae96ec028215cac6a /Master/texmf-dist/doc/latex/xfor/sample.tex | |
parent | 33c85af6b9b145413f145c2f42ab33edebe0db80 (diff) |
new latex package xfor, 28jun07
git-svn-id: svn://tug.org/texlive/trunk@4626 c570f23f-e606-0410-a88d-b1316a301751
Diffstat (limited to 'Master/texmf-dist/doc/latex/xfor/sample.tex')
-rw-r--r-- | Master/texmf-dist/doc/latex/xfor/sample.tex | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/Master/texmf-dist/doc/latex/xfor/sample.tex b/Master/texmf-dist/doc/latex/xfor/sample.tex new file mode 100644 index 00000000000..b1027185b18 --- /dev/null +++ b/Master/texmf-dist/doc/latex/xfor/sample.tex @@ -0,0 +1,72 @@ +\errorcontextlines=100 +\documentclass{article} + +\usepackage{xfor} + +\makeatletter +% \insertinto{new value}{list} +\newcommand{\insertinto}[2]{% +\def\nlst{}% +\@for\n:=#2\do{% +% store new list in \toks@ +\expandafter\toks@\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 + \edef\nlst{\the\toks@,\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 + % loop prematurely ended, append remainder of original list + % to new list + \expandafter\toks@\expandafter{\nlst}% + \edef\nlst{\the\toks@,\@forremainder}% + \fi +\else + % wasn't prematurely terminated, so new value hasn't been added + % add now. + \expandafter\toks@\expandafter{\nlst}% + \ifx\nlst\@empty + \edef\nlst{\number#1}% + \else + \edef\nlst{\the\toks@,\number#1}% + \fi +\fi +\global\let#2=\nlst +} + +\newcommand{\insertionsort}[1]{% +\def\sortedlist{}% +\@for\val:=#1\do{{\insertinto{\val}{\sortedlist}}}% +\let#1=\sortedlist} +\makeatother + +\begin{document} +\def\mylist{1,2,5,9,12,15,18,20}% +\def\newval{11}% +Original list: \mylist. New value: \newval. + +\insertinto{\newval}{\mylist} +New list: \mylist. + +Unsorted list: +\def\mylist{4,2,7,1,10,11,20,15}\mylist. + +\insertionsort{\mylist}% +Sorted list: \mylist. +\end{document} |