1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
|
\documentclass[a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[frenchb]{babel}
\usepackage{tikz}
\usepackage{pstricks}
\makeatletter
\newtoks\arg@tok
\newtoks\def@tok
\newif\ifspace@found@
\newcount\cnt@variable
\def\add@to@tok#1#2{#1\expandafter{\the#1#2}}
\def\expadd@to@tok#1#2{%
#1\expandafter\expandafter\expandafter
{\expandafter\the\expandafter#1#2}}
\def\snif@next@tok{\futurelet\next@tok\analyse@variable@tok}
\def\parse@variables#1{%
\cnt@variable\z@
\arg@tok{}\def@tok{}%
\space@found@false
\snif@next@tok#1\relax}
\def\analyse@variable@tok{%
\ifx\next@tok\relax
\advance\cnt@variable\@ne
\arg@tok\expandafter{\expandafter\def\expandafter\read@item\the\arg@tok,##}%
\expadd@to@tok\arg@tok{\number\cnt@variable\relax}%
\expadd@to@tok\def@tok{\expandafter\def\expandafter\list@item\expandafter{\expandafter##\number\cnt@variable\relax}}%
\expandafter\the\expandafter\arg@tok\expandafter{\the\def@tok}% définit la macro \read@item
\let\next@action\@gobble% mange le \relax
\else
\ifspace@found@
\space@found@false
\add@to@tok\arg@tok{ }% ajoute l'espace auparavant détecté
\fi
\ifx\@sptoken\next@tok
\space@found@true% se rappeler qu'un espace a été trouvé
\def\next@action{\let\next@tok= }% mange l'espace
\afterassignment\snif@next@tok
\else
\let\next@action\eatnext@variable@tok
\fi
\fi
\next@action
}
\def\eatnext@variable@tok#1{%
\ifcat\relax\noexpand#1
\advance\cnt@variable\@ne
\expadd@to@tok\arg@tok{\expandafter##\number\cnt@variable}%
\expadd@to@tok\def@tok{\expandafter\def\expandafter#1\expandafter{\expandafter##\number\cnt@variable}}%
\else
\add@to@tok\arg@tok{#1}%
\fi
\snif@next@tok
}
\def\doforeach#1in{%
\parse@variables{#1}%
\@ifnextchar\bgroup{\doforeach@i\iftrue}{\doforeach@i\iffalse}%
}
\def\doforeach@i#1#2{%
#1% \iftrue ou \iffalse
\def\list@item{#2,\relax}%
\else
\expandafter\def\expandafter\list@item\expandafter{#2,\relax}%
\fi
\afterassignment\parse@itemlist
\def\loop@code
}
\def\parse@itemlist{%
\if\relax\expandafter\noexpand\list@item
\else
\expandafter\read@item\list@item
\loop@code
\expandafter\parse@itemlist
\fi
}
\makeatother
\begin{document}
\doforeach (\x:\y) in {(3:4),(7:9),(15:3),(1:8)}{$x$ vaut \x, $y$ vaut \y\ et leur somme est \number\numexpr\x+\y\relax\endgraf}
\bigskip
%même que ci dessus mais avec pattern différent
\doforeach \x;\y in {3;4,7;9,15;3,1;8}{$x$ vaut \x, $y$ vaut \y\ et leur somme est \number\numexpr\x+\y\relax\endgraf}
\bigskip
%on peut mettre la liste dans une séquence de contrôle
\def\mylist{1-4-8,2-6-7,3-9-4}
\doforeach \uu-\vv-\ww in \mylist {Le triplet est \uu, \vv, \ww.\endgraf}
\bigskip
Voici un dessin fait avec tikz :
\begin{tikzpicture}
\draw[help lines] (0,0) grid (3,3);
\doforeach \x;\y(\dotcolor) in {1;1(red),2;0(blue),3;2(green),2.5;2.5(yellow)}{\fill[\dotcolor] (\x,\y) circle (2pt);}
\end{tikzpicture}
\bigskip
% ATTENTION : compiler avec latex->dvi->ps->pdf pour le code ci dessous :
Voici un dessin fait avec pstricks :
\psset{unit=1cm,gridlabels=0,subgriddiv=0,gridcolor=gray,gridwidth=0.6pt,dotstyle=*}
\begin{pspicture}(0,0)(3,3)
\psgrid(0,0)(3,3)
\doforeach \x;\y(\dotcolor) in {1;1(red),2;0(blue),3;2(green),2.5;2.5(yellow)}{\psdots[linecolor=\dotcolor](\x,\y)}%
\end{pspicture}
\end{document} |
Partager