Debian Codegraph @

texmf-dist/doc/latex/xfor/sample.tex

raw
1 %%
2 %% This is file `sample.tex',
3 %% generated with the docstrip utility.
4 %%
5 %% The original source files were:
6 %%
7 %% xfor.dtx (with options: `sample.tex,package')
8 %%
9 %% xfor.dtx
10 %% Copyright 2009 Nicola Talbot
11 %%
12 %% This work may be distributed and/or modified under the
13 %% conditions of the LaTeX Project Public License, either version 1.3
14 %% of this license of (at your option) any later version.
15 %% The latest version of this license is in
16 %% http://www.latex-project.org/lppl.txt
17 %% and version 1.3 or later is part of all distributions of LaTeX
18 %% version 2005/12/01 or later.
19 %%
20 %% This work has the LPPL maintenance status `maintained'.
21 %%
22 %% The Current Maintainer of this work is Nicola Talbot.
23 %%
24 %% This work consists of the files xfor.dtx and xfor.ins and the derived files xfor.sty, sample.tex.
25 %%
26 %% \CharacterTable
27 %% {Upper-case \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
28 %% Lower-case \a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z
29 %% Digits \0\1\2\3\4\5\6\7\8\9
30 %% Exclamation \! Double quote \" Hash (number) \#
31 %% Dollar \$ Percent \% Ampersand \&
32 %% Acute accent \' Left paren \( Right paren \)
33 %% Asterisk \* Plus \+ Comma \,
34 %% Minus \- Point \. Solidus \/
35 %% Colon \: Semicolon \; Less than \<
36 %% Equals \= Greater than \> Question mark \?
37 %% Commercial at \@ Left bracket \[ Backslash \\
38 %% Right bracket \] Circumflex \^ Underscore \_
39 %% Grave accent \` Left brace \{ Vertical bar \|
40 %% Right brace \} Tilde \~}
41 \listfiles
42 \documentclass{article}
43
44 \usepackage{xfor}
45
46 \makeatletter
47
48
49 \newtoks\tmptok
50
51 \newcommand{\insertinto}[2]{%
52 \def\nlst{}%
53 \@for\n:=#2\do{%
54 \expandafter\tmptok\expandafter{\nlst}%
55 \ifnum\n>#1\relax
56 \edef\newstuff{\number#1,\n}%
57 % end for loop at the end of this iteration
58 \@endfortrue
59 \else
60 \edef\newstuff{\n}%
61 \fi
62 \ifx\nlst\@empty
63 \edef\nlst{\newstuff}%
64 \else
65 \edef\nlst{\the\tmptok,\newstuff}%
66 \fi
67 }%
68 \if@endfor
69 % loop may have been terminated during final iteration, in
70 % which case \@forremainder is empty.
71 \ifx\@forremainder\@empty
72 % do nothing
73 \else
74 % loop prematurely ended, append remainder of original list
75 % to new list
76 \expandafter\tmptok\expandafter{\nlst}%
77 \edef\nlst{\the\tmptok,\@forremainder}%
78 \fi
79 \else
80 % wasn't prematurely terminated, so new value hasn't been added.
81 % Add now.
82 \expandafter\tmptok\expandafter{\nlst}%
83 \ifx\nlst\@empty
84 \edef\nlst{\number#1}%
85 \else
86 \edef\nlst{\the\tmptok,\number#1}%
87 \fi
88 \fi
89 \global\let#2=\nlst
90 }
91
92
93 \newcommand{\insertionsort}[1]{%
94 \def\sortedlist{}%
95 \@for\val:=#1\do{{\insertinto{\val}{\sortedlist}}}%
96 \let#1=\sortedlist}
97
98 \makeatother
99
100 \begin{document}
101 Unsorted list:
102 \def\mylist{4,2,7,1,10,11,20,15}\mylist.
103
104 \insertionsort{\mylist}%
105 Sorted list: \mylist.
106
107 Iterate through the list (next element in parentheses):
108 \makeatletter
109 \@for\n:=\mylist\do{%
110 \n
111 \ifx\@xfor@nextelement\@nnil
112 % last iteration
113 \else
114 (\@xfor@nextelement);
115 \fi
116 }.
117 \end{document}
118 \endinput
119 %%
120 %% End of file `sample.tex'.