%!TEX lualatex

\documentclass[a4paper,french,11pt]{article}
\usepackage[margin=1.5cm]{geometry}
\usepackage{babel}
\usepackage[table,svgnames]{xcolor}
\usepackage{tikz}
\usepackage{pgf,pgffor}
\usepackage{listofitems}
\usepackage{xstring}
\usepackage{simplekv}
\usepackage{codehigh}
\usepackage{tabularray}
\setlength\parindent{0mm}

%divers
\makeatletter
	\newcommand{\transfalph}[1]{\@alph{#1}}
	\newcommand{\transfAlph}[1]{\@Alph{#1}}
\makeatother

\begin{document}

\defKV[PLTIKZSUDOM]{%
	couleurtexte=\def\PLSMcoultexte{#1},%
	epaisseur=\def\PLSMepf{#1},%
	epaisseurg=\def\PLSMepg{#1},%
	unite=\def\PLSMunite{#1},%
	couleurcase=\def\PLSMcoulcase{#1},%
	nbcol=\def\PLSMnbcol{#1},%
	nbsubcol=\def\PLSMnbsubcol{#1},%
	nblig=\def\PLSMnblig{#1},%
	nbsublig=\def\PLSMnbsublig{#1},%
	police=\def\PLSMfonte{#1},%
	policeleg=\def\PLSMfonteleg{#1},%
	listelegv=\def\PLSMlistelegv{#1},%
	listelegh=\def\PLSMlistelegh{#1}
}

\setKVdefault[PLTIKZSUDOM]{%
	epaisseurg=1.5pt,%
	epaisseur=0.5pt,%
	unite=1cm,%
	couleurcase=LightBlue!50,%
	couleurtexte=blue,%
	nbcol=9,%
	nbsubcol=3,%
	nblig=9,%
	nbsublig=3,
	police=\normalfont\normalsize,%
	policeleg=\small\sffamily,%
	legendes=true,%
	listelegv=ABCDEFGHIJKLMNOPQRSTUVWXYZ,%
	listelegh=abcdefghijklmnopqrstuvwxyz
}

\NewDocumentEnvironment{PLsudomaths}{ O{} m }
	{
	\useKVdefault[PLTIKZSUDOM]
	\setKV[PLTIKZSUDOM]{#1}% on paramètres les nouvelles clés et on les simplifie
	%calculs intermédiaires
	\def\larcolinter{\inteval{\PLSMnbcol/\PLSMnbsubcol}}
	\def\larliginter{\inteval{\PLSMnblig/\PLSMnbsublig}}
	%lecture liste
	\setsepchar[.]{§./}%
	\readlist*\SPGrilleSudoMaths{#2}
	%débt envtik
	\begin{tikzpicture}[x=\PLSMunite,y=\PLSMunite,line join=miter]
		%cases
		\foreach \i in {1,2,...,\PLSMnblig}{%
			\foreach \j in {1,2,...,\PLSMnbcol}{%
				\itemtomacro\SPGrilleSudoMaths[\i,\j]\SMcase
				\IfSubStr{\SMcase}{*}%si on veut colorier via *
				{%
					\StrDel{\SMcase}{*}[\SMcaseb]%
					\draw[draw=none,fill=\PLSMcoulcase] ({\j-1},{1-\i}) rectangle++ (1,-1) node[\PLSMcoultexte,font=\PLSMfonte,midway] {\SMcaseb} ;%
				}
				{%
					\draw ({\j-0.5},{0.5-\i}) node[\PLSMcoultexte,font=\PLSMfonte] {\SMcase} ;%
				}
			}
		}
		%grilles
		\draw[line width=\PLSMepg] (0,0) rectangle ({\PLSMnbcol},{-\PLSMnblig}) ;
		\draw[line width=\PLSMepf,xstep=1,ystep=1] (0,0) grid ({\PLSMnbcol},{-\PLSMnblig}) ;
		\draw[line width=\PLSMepg,xstep=\larcolinter,ystep=\larliginter] (0,0) grid ({\PLSMnbcol},{-\PLSMnblig}) ;
		%légendes
		\ifboolKV[PLTIKZSUDOM]{legendes}
			{%
				\foreach \i in {1,2,...,\PLSMnbcol}{\draw ({\i-0.5},0.45) node[font=\PLSMfonteleg] {\strut\StrChar{\PLSMlistelegh}{\i}} ;}
				\foreach \j in {1,2,...,\PLSMnblig}{\draw (-0.35,{0.45-\j}) node[font=\PLSMfonteleg] {\StrChar{\PLSMlistelegv}{\j}} ;}
			}{}
	}
	{
	\end{tikzpicture}
	}


{\footnotesize \begin{codehigh}[language=latex/latex3]
\def\grilleSuMa{%
    1*////////§%
    //4////*//§%
    //////7//§%
    //5//////§%
    //1///*///§%
    ///6/////§%
    ///2/////§%
    ///7/////§%
    //////9//%
}

\begin{PLsudomaths}[unite=0.8cm]{\grilleSuMa}
\end{PLsudomaths}
\end{codehigh}}

\def\grilleSuMa{%
	1*////////§%
	//4////*//§%
	//////7//§%
	//5//////§%
	//1///*///§%
	///6/////§%
	///2/////§%
	///7/////§%
	//////9//%
}

\begin{PLsudomaths}[unite=0.8cm]{\grilleSuMa}
\end{PLsudomaths}

\bigskip

{\footnotesize \begin{codehigh}[language=latex/latex3]
\def\grilleSuMaB{%
    (a)*/(b)*///(c)*/(d)*§%
    (e)*///(f)*/(g)*/(h)*§%
    //(i)*///(j)*§%
    //(k)*//(l)*/(m)*§%
    (n)*//(o)*///(p)*§%
    ///(q)*//§%
}

\begin{PLsudomaths}[%
        unite=0.75cm,nbcol=6,nbsubcol=2,nblig=6,nbsublig=3,%
        police=\small\bfseries\ttfamily,couleurtexte=red,%
        couleurcase=yellow!50,legendes=false]{\grilleSuMaB}
    \draw[ForestGreen,very thick,<-] (3.5,-4.5) to[bend right] ++ (4,-1) node[right] {code inside} ;
\end{PLsudomaths}
\end{codehigh}}

\def\grilleSuMaB{%
	(a)*/(b)*///(c)*/(d)*§%
	(e)*///(f)*/(g)*/(h)*§%
	//(i)*///(j)*§%
	//(k)*//(l)*/(m)*§%
	(n)*//(o)*///(p)*§%
	///(q)*//§%
}

\begin{PLsudomaths}[%
	unite=0.75cm,nbcol=6,nbsubcol=2,nblig=6,nbsublig=3,%
	police=\small\bfseries\ttfamily,couleurtexte=red,%
	couleurcase=yellow!50,legendes=false]{\grilleSuMaB}
\draw[ForestGreen,very thick,<-] (3.5,-4.5) to[bend right] ++ (4,-1) node[right] {code inside} ;
\end{PLsudomaths}

\pagebreak

\def\grilleSuMaC{%
	*///////§%
	/*//////§%
	//*/////§%
	///*////§%
	////*///§%
	/////*//§%
	//////*/§%
	///////*§%
	*///////§%
	/*//////%
}

\begin{PLsudomaths}[%
	unite=0.75cm,nbcol=8,nbsubcol=2,nblig=10,nbsublig=5,%
	police=\small\bfseries\ttfamily,couleurtexte=ForestGreen,%
	couleurcase=yellow!50,listelegv=WXCVBQSDFG,listelegh=96321478]{\grilleSuMaC}
\end{PLsudomaths}

\end{document}