<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">% Author     : C. Pierquet
% licence    : Released under the LaTeX Project Public License v1.3c or later, see http://www.latex-project.org/lppl.txtf

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{tkz-bernoulli}[2023/11/05 0.1.3 Draw Bernoulli tree with tikz]

%====HISTORIQUE
% v 0.1.3	Retour en arriÃ¨re pour la clÃ©, qui n'est pas utile en fait...
% v 0.1.2	Ajout d'une clÃ© pour forcer l'affichage d'un indice pour les succÃ¨s/Ã©checs (prudence quand mÃªme... epÃ©rimental)
% v 0.1.1	Correction de bugs + ajout d'une commande pour une Ã©preuve de Bernoulli
% v 0.1.0	Version initiale [fr] et [en]

%====BASE
\RequirePackage{tikz,pgffor}
\RequirePackage{xstring}
\RequirePackage{simplekv}
\RequirePackage{xintexpr,xintbinhex}
\usetikzlibrary{calc}

%====SCHÃ‰MA DE BERNOULLI [FR]
%styles par dÃ©faut
\tikzstyle{BernBranche} = [semithick]
\tikzstyle{BernRacine} = [inner sep=2pt]
\tikzstyle{BernNoeudS} = [inner sep=2pt]
\tikzstyle{BernNoeudE} = [inner sep=2pt]
\tikzstyle{BernProbaS} = [fill=white,midway,font=\footnotesize,inner sep=2pt]
\tikzstyle{BernProbaE} = [fill=white,midway,font=\footnotesize,inner sep=2pt]
\tikzstyle{BernNotice} = [inner sep=2pt,text=teal,right=1em]

\NewDocumentCommand\tkzSchemBernStyleDefaut{ }{%
	\tikzstyle{BernBranche} = [semithick]
	\tikzstyle{BernRacine} = [inner sep=2pt]
	\tikzstyle{BernNoeudS} = [inner sep=2pt]
	\tikzstyle{BernNoeudE} = [inner sep=2pt]
	\tikzstyle{BernProbaS} = [fill=white,midway,font=\footnotesize,inner sep=2pt]
	\tikzstyle{BernProbaE} = [fill=white,midway,font=\footnotesize,inner sep=2pt]
	\tikzstyle{BernNotice} = [inner sep=2pt,text=teal,right=1em]
}

%clÃ©s
\defKV[ArbreBernoulli]{%
	EspNiv=\def\ArBinIN{#1},%
	EspFeuil=\def\ArBinIF{#1},%
	Evts=\def\ArBinEvts{#1},%
	Probas=\def\ArBinProbas{#1},%
	Racine=\def\ArBinRacine{#1},%
	Var=\def\ArBinVar{#1},%
	N=\def\ArBinN{#1}
}

\setKVdefault[ArbreBernoulli]{%
	EspNiv=2.25,%
	EspFeuil=1,%
	Evts={$S$/$E$},%
	Probas={$p$/$1-p$},%
	Grille=false,%
	AffProbas=true,%
	Racine=$\Omega$,%
	Aide=false,%
	Notice=false,%
	Var=X,%
	N=3
}

\NewDocumentCommand\tkzEpreuvBernoulli{ s O{} D&lt;&gt;{} }{%
	\useKVdefault[ArbreBernoulli]%
		\setKV[ArbreBernoulli]{#2}%
		\saveexpandmode\expandarg
		\StrCut{\ArBinEvts}{/}{\ArBinSucces}{\ArBinEchec}%
		\StrCut{\ArBinProbas}{/}{\ArBinProbSucces}{\ArBinProbEchec}%
		\restoreexpandmode
		\IfBooleanT{#1}%
			{%
				\begin{tikzpicture}[#3]
			}%
		\IfStrEq{\ArBinRacine}{false}%
			{%
				\coordinate (A0-1) at (0,0) ;%
				\coordinate (B0-1) at (0,0) ;%
			}%
			{%
				\coordinate (A0-1) at (0,0) ;%
				\node[BernRacine] (B0-1) at (A0-1) {\ArBinRacine} ;%
			}%
		\coordinate (A1-1) at ($(A0-1)+({\ArBinIN},{0.5*\ArBinIF})$) ;
		\coordinate (A1-2) at ($(A0-1)+({\ArBinIN},{-0.5*\ArBinIF})$) ;
		\node[BernNoeudS] (B1-1) at (A1-1) {\ArBinSucces} ;%succÃ¨s
		\node[BernNoeudE] (B1-2) at (A1-2) {\ArBinEchec} ;%Ã©chec
		\ifboolKV[ArbreBernoulli]{AffProbas}%
			{%
				\draw[BernBranche] (B0-1) -- (B1-1) node[BernProbaS] {\ArBinProbSucces} ;
				\draw[BernBranche] (B0-1) -- (B1-2) node[BernProbaE] {\ArBinProbEchec} ;
			}%
			{%
				\draw[BernBranche] (B0-1) -- (B1-1) ;
				\draw[BernBranche] (B0-1) -- (B1-2) ;
			}%
		\IfBooleanT{#1}%
			{%
				\end{tikzpicture}%
			}%
}

\NewDocumentCommand\tkzSchemBernoulli{ s O{} D&lt;&gt;{} }{%
	\useKVdefault[ArbreBernoulli]%
	\setKV[ArbreBernoulli]{#2}%
	\saveexpandmode\expandarg
	\StrCut{\ArBinEvts}{/}{\ArBinSucces}{\ArBinEchec}%
	\StrCut{\ArBinProbas}{/}{\ArBinProbSucces}{\ArBinProbEchec}%
	\restoreexpandmode
	\IfBooleanT{#1}%
		{%
			\begin{tikzpicture}[#3]
		}%
	%grille
	\ifboolKV[ArbreBernoulli]{Grille}%
		{%
			\filldraw[red!25] (0,0) circle[radius=2pt] ;
			\draw[very thin,lightgray] (0,{-0.5*\ArBinIF*2^(\ArBinN)}) grid ({\ArBinN*\ArBinIN},{0.5*\ArBinIF*2^(\ArBinN)}) ;%
		}%
		{}%
	%Racine
	\IfStrEq{\ArBinRacine}{false}%
		{%
			\coordinate (A0-1) at (0,0) ;%
			\coordinate (B0-1) at (0,0) ;%
		}%
		{%
			\coordinate (A0-1) at (0,0) ;%
			\node[BernRacine] (B0-1) at (A0-1) {\ArBinRacine} ;%
		}%
	%Aide Racine
	\ifboolKV[ArbreBernoulli]{Aide}%
		{\draw[teal] (B0-1) node[below=4pt,font=\tiny\ttfamily] {B0-1} ;}%
		{}%
	%gÃ©nÃ©rations des nÅ“uds du premier chemin
	\foreach \i in {1,...,\ArBinN}{%
		\def\j{\inteval{\i-1}}%
		\def\ArBinTmpNiv{\fpeval{2**(\ArBinN-\i)}}%
		\def\ArBinTmpNds{\fpeval{2**(\i)}}%
		\coordinate (A\i-1) at ($(A\j-1)+({\ArBinIN},{0.5*\ArBinIF*\ArBinTmpNiv})$) ;
		\foreach \k in {2,...,\ArBinTmpNds}{%
			\def\l{\inteval{\k-1}}%
			\coordinate (A\i-\k) at ($(A\i-\l)+(0,{-\ArBinIF*\ArBinTmpNiv})$) ;
		}%
	}%
	%test des nÅ“uds OK ^^
	\foreach \i in {1,...,\ArBinN}{%
		\def\ArBinTmpNds{\fpeval{2**(\i)}}%
		\foreach \j in {1,...,\ArBinTmpNds}{%
			\xintifboolexpr{\xintiiRem{\j}{2} != 0 }%
				{\node[BernNoeudS] (B\i-\j) at (A\i-\j) {\ArBinSucces} ;}%succÃ¨s
				{\node[BernNoeudE] (B\i-\j) at (A\i-\j) {\ArBinEchec} ;}%Ã©chec
			\ifboolKV[ArbreBernoulli]{Aide}%
				{\draw[teal] (B\i-\j) node[below=4pt,font=\tiny\ttfamily] {B\i-\j} ;}%
				{}%
			}%
		}%
	%test des flÃ¨ches...
	\foreach \i in {0,...,\inteval{\ArBinN-1}}{%
		\def\ipu{\inteval{\i+1}}%
		\def\ArBinTmpNds{\fpeval{2**(\i)}}%
		\foreach \j in {1,...,\ArBinTmpNds}{%
			\def\ArBinTmpA{\inteval{2*\j-1}}%
			\def\ArBinTmpB{\inteval{2*\j}}%
			\ifboolKV[ArbreBernoulli]{AffProbas}%
				{%
					\draw[BernBranche] (B\i-\j) -- (B\ipu-\ArBinTmpA) node[BernProbaS] {\ArBinProbSucces} ;
					\draw[BernBranche] (B\i-\j) -- (B\ipu-\ArBinTmpB) node[BernProbaE] {\ArBinProbEchec} ;
				}%
				{%
					\draw[BernBranche] (B\i-\j) -- (B\ipu-\ArBinTmpA) ;
					\draw[BernBranche] (B\i-\j) -- (B\ipu-\ArBinTmpB) ;
				}%
			}%
		}%
	\ifboolKV[ArbreBernoulli]{Notice}%
		{%
			\foreach \i in {1,...,\fpeval{2**\ArBinN}}{%
				\def\itmp{\fpeval{2**\ArBinN-\i}}%
				\def\ArBenTmpBin{\xintDecToBin{\itmp}}%
				\draw (B\ArBinN-\i) node[BernNotice] {$\longrightarrow \ArBinVar=\StrCount{\ArBenTmpBin}{1}$} ;
				}%
		}%
		{}%
	\IfBooleanT{#1}%
		{%
			\end{tikzpicture}%
		}%
}

%====SCHÃ‰MA DE BERNOULLI [EN]
%styles par dÃ©faut [en]
\tikzstyle{BernEdge} = [semithick]
\tikzstyle{BernRoot} = [inner sep=2pt]
\tikzstyle{BernNodeS} = [inner sep=2pt]
\tikzstyle{BernNodeF} = [inner sep=2pt]
\tikzstyle{BernProbS} = [fill=white,midway,font=\footnotesize,inner sep=2pt]
\tikzstyle{BernProbF} = [fill=white,midway,font=\footnotesize,inner sep=2pt]
\tikzstyle{BernGuide} = [inner sep=2pt,text=teal,right=1em]

\NewDocumentCommand\tkzBernTreeStyleDefault{ }{%1=type,2=tikzstyle
	\tikzstyle{BernEdge} = [semithick]
	\tikzstyle{BernRoot} = [inner sep=2pt]
	\tikzstyle{BernNodeS} = [inner sep=2pt]
	\tikzstyle{BernNodeF} = [inner sep=2pt]
	\tikzstyle{BernProbS} = [fill=white,midway,font=\footnotesize,inner sep=2pt]
	\tikzstyle{BernProbF} = [fill=white,midway,font=\footnotesize,inner sep=2pt]
	\tikzstyle{BernGuide} = [inner sep=2pt,text=teal,right=1em]
}

\defKV[BernoulliTree]{%
	LevelSep=\def\ArBinIN{#1},%
	NodeSep=\def\ArBinIF{#1},%
	Events=\def\ArBinEvts{#1},%
	Probs=\def\ArBinProbas{#1},%
	Root=\def\ArBinRacine{#1},%
	Var=\def\ArBinVar{#1},%
	N=\def\ArBinN{#1}
}

\setKVdefault[BernoulliTree]{%
	LevelSep=2.25,%
	NodeSep=1,%
	Events={$S$/$F$},%
	Probs={$p$/$1-p$},%
	Grid=false,%
	ShowProbs=true,%
	Root=$\Omega$,%
	Help=false,%
	Guide=false,%
	Var=X,%
	N=3
}

\NewDocumentCommand\tkzBernoulliTrial{ s O{} D&lt;&gt;{} }{%
	\useKVdefault[BernoulliTree]%
		\setKV[BernoulliTree]{#2}%
		\saveexpandmode\expandarg
		\StrCut{\ArBinEvts}{/}{\ArBinSucces}{\ArBinEchec}%
		\StrCut{\ArBinProbas}{/}{\ArBinProbSucces}{\ArBinProbEchec}%
		\restoreexpandmode
		\IfBooleanT{#1}%
			{%
				\begin{tikzpicture}[#3]
			}%
		\IfStrEq{\ArBinRacine}{false}%
			{%
				\coordinate (A0-1) at (0,0) ;%
				\coordinate (B0-1) at (0,0) ;%
			}%
			{%
				\coordinate (A0-1) at (0,0) ;%
				\node[BernRoot] (B0-1) at (A0-1) {\ArBinRacine} ;%
			}%
		\coordinate (A1-1) at ($(A0-1)+({\ArBinIN},{0.5*\ArBinIF})$) ;
		\coordinate (A1-2) at ($(A0-1)+({\ArBinIN},{-0.5*\ArBinIF})$) ;
		\node[BernNodeS] (B1-1) at (A1-1) {\ArBinSucces} ;%succÃ¨s
		\node[BernNodeF] (B1-2) at (A1-2) {\ArBinEchec} ;%Ã©chec
		\ifboolKV[BernoulliTree]{ShowProbs}%
			{%
				\draw[BernEdge] (B0-1) -- (B1-1) node[BernProbS] {\ArBinProbSucces} ;
				\draw[BernEdge] (B0-1) -- (B1-2) node[BernProbF] {\ArBinProbEchec} ;
			}%
			{%
				\draw[BernEdge] (B0-1) -- (B1-1) ;
				\draw[BernEdge] (B0-1) -- (B1-2) ;
			}%
		\IfBooleanT{#1}%
			{%
				\end{tikzpicture}%
			}%
}

\NewDocumentCommand\tkzBernoulliTree{ s O{} D&lt;&gt;{} }{%
	\useKVdefault[BernoulliTree]%
	\setKV[BernoulliTree]{#2}%
	\saveexpandmode\expandarg
	\StrCut{\ArBinEvts}{/}{\ArBinSucces}{\ArBinEchec}%
	\StrCut{\ArBinProbas}{/}{\ArBinProbSucces}{\ArBinProbEchec}%
	\restoreexpandmode
	\IfBooleanT{#1}%
		{%
			\begin{tikzpicture}[#3]
		}%
	%grille
	\ifboolKV[BernoulliTree]{Grid}%
		{%
			\filldraw[red!25] (0,0) circle[radius=2pt] ;
			\draw[very thin,lightgray] (0,{-0.5*\ArBinIF*2^(\ArBinN)}) grid ({\ArBinN*\ArBinIN},{0.5*\ArBinIF*2^(\ArBinN)}) ;%
		}%
		{}%
	%Racine
	\IfStrEq{\ArBinRacine}{false}%
		{%
			\coordinate (A0-1) at (0,0) ;%
			\coordinate (B0-1) at (0,0) ;%
		}%
		{%
			\coordinate (A0-1) at (0,0) ;%
			\node[BernRoot] (B0-1) at (A0-1) {\ArBinRacine} ;%
		}%
	%Aide Racine
	\ifboolKV[BernoulliTree]{Help}%
		{\draw[teal] (B0-1) node[below=4pt,font=\tiny\ttfamily] {B0-1} ;}%
		{}%
	%gÃ©nÃ©rations des nÅ“uds du premier chemin
	\foreach \i in {1,...,\ArBinN}{%
		\def\j{\inteval{\i-1}}%
		\def\ArBinTmpNiv{\fpeval{2**(\ArBinN-\i)}}%
		\def\ArBinTmpNds{\fpeval{2**(\i)}}%
		\coordinate (A\i-1) at ($(A\j-1)+({\ArBinIN},{0.5*\ArBinIF*\ArBinTmpNiv})$) ;
		\foreach \k in {2,...,\ArBinTmpNds}{%
			\def\l{\inteval{\k-1}}%
			\coordinate (A\i-\k) at ($(A\i-\l)+(0,{-\ArBinIF*\ArBinTmpNiv})$) ;
		}%
	}%
	%test des nÅ“uds OK ^^
	\foreach \i in {1,...,\ArBinN}{%
		\def\ArBinTmpNds{\fpeval{2**(\i)}}%
		\foreach \j in {1,...,\ArBinTmpNds}{%
			\xintifboolexpr{\xintiiRem{\j}{2} != 0 }%
				{\node[BernNodeS] (B\i-\j) at (A\i-\j) {\ArBinSucces} ;}%succÃ¨s
				{\node[BernNodeF] (B\i-\j) at (A\i-\j) {\ArBinEchec} ;}%Ã©chec
			\ifboolKV[BernoulliTree]{Help}%
				{\draw[teal] (B\i-\j) node[below=4pt,font=\tiny\ttfamily] {B\i-\j} ;}%
				{}%
			}%
		}%
	%test des flÃ¨ches...
	\foreach \i in {0,...,\inteval{\ArBinN-1}}{%
		\def\ipu{\inteval{\i+1}}%
		\def\ArBinTmpNds{\fpeval{2**(\i)}}%
		\foreach \j in {1,...,\ArBinTmpNds}{%
			\def\ArBinTmpA{\inteval{2*\j-1}}%
			\def\ArBinTmpB{\inteval{2*\j}}%
			\ifboolKV[BernoulliTree]{ShowProbs}%
				{%
					\draw[BernEdge] (B\i-\j) -- (B\ipu-\ArBinTmpA) node[BernProbS] {\ArBinProbSucces} ;
					\draw[BernEdge] (B\i-\j) -- (B\ipu-\ArBinTmpB) node[BernProbF] {\ArBinProbEchec} ;
				}%
				{%
					\draw[BernEdge] (B\i-\j) -- (B\ipu-\ArBinTmpA) ;
					\draw[BernEdge] (B\i-\j) -- (B\ipu-\ArBinTmpB) ;
				}%
			}%
		}%
	\ifboolKV[BernoulliTree]{Guide}%
		{%
			\foreach \i in {1,...,\fpeval{2**\ArBinN}}{%
				\def\itmp{\fpeval{2**\ArBinN-\i}}%
				\def\ArBenTmpBin{\xintDecToBin{\itmp}}%
				\draw (B\ArBinN-\i) node[BernGuide] {$\longrightarrow \ArBinVar=\StrCount{\ArBenTmpBin}{1}$} ;
				}%
		}%
		{}%
	\IfBooleanT{#1}%
		{%
			\end{tikzpicture}%
		}%
}

\endinput</pre></body></html>