% Author        : C. Pierquet
% licence       : Released under the LaTeX Project Public License v1.3c or later, see http://www.latex-project.org/lppl.txt

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{tkz-interval}[2025/10/12 Brackets, with tikz, v 1.0a]
%1.0a  Initial version

%====packages
\usepackage{tikz}
\usepackage{simplekv}

%====lengths
\newlength\tkzintervfixedheight
\newlength\tkzintervvarheight
\newlength\tkzintervwidth
\newlength\tkzintervsep
\newlength\tkzintervhoriz

%====default values
\AtBeginDocument{\settoheight\tkzintervfixedheight{\hbox{x}}}
\setlength\tkzintervhoriz{4pt}
\setlength\tkzintervwidth{0.7pt}
\def\tkzintervxoffset{0.75}
\def\tkzintervspacebefore{\,}
\def\tkzintervspaceafter{\,}

%====internal macros
\ExplSyntaxOn

\NewDocumentCommand{\inttkzintisin}{ m m m m }{
  \tl_set:Nx \l_tmpa_tl {#2}
  \str_if_in:NnTF { \l_tmpa_tl } {#1} {#3} {#4}
}

\NewDocumentCommand\inttkzinttestandsplit{ m m m m }
{
	%#1 = substr
	%#2 = str to test
	%#3 = left macro (or #2 if not found)
	%#4 = right macro (or #2 if not found)
	\tl_set:Nx \l_tmpa_tl { #2 }
	\tl_if_in:NnTF \l_tmpa_tl { #1 }
	{
		\seq_set_split:Nne \l_tmpa_seq { #1 } { #2 }
		\tl_set:Nx #3 { \seq_item:Nn \l_tmpa_seq {1} }
		\tl_set:Nx #4 { \seq_item:Nn \l_tmpa_seq {2} }
	}
	{
		\tl_gset:Nx #3 {#2}
		\tl_gset:Nx #4 {#2}
	}
}

\ExplSyntaxOff

%====local keys
\defKV[tkzinterv]{%
  vsep=\setlength\tkzintervsep{#1},%
  overlap=\def\tkzintervoverlap{#1},%
  overlap factor=\def\tkzintervoverlapfactor{#1},%
  hsep=\def\tkzintervxoffset{#1},%
  space before=\def\tkzintervspacebefore{#1},%
  space after=\def\tkzintervspaceafter{#1},%
  left color=\tikzset{tkzinterv lcolor/.style={#1}},%
  right color=\tikzset{tkzinterv rcolor/.style={#1}}
}
\setKVdefault[tkzinterv]{%
  vsep=0.125em,%
  overlap=lr,%
  overlap factor=0.5,%
  FF=true,%
  OO=false,%
  OF=false,%
  FO=false,%
  open left=false,%
  open right=false,%
  open=false,%
  hsep=0.75,%
  space before={\,},%
  space after={\,},%
  left color={},%
  right color={},%
  nobeforeafter=false
}

%====single bracket v1
\newcommand\inttkzintervlbracketo{%
  \draw[line width=\tkzintervscale\tkzintervwidth,tkzinterv lcolor] ([xshift={-\fpeval{(1+\tkzintervxoffset)*\tkzintervscale}\tkzintervhoriz}]MYNODE.south west) -| ([xshift={-\fpeval{\tkzintervxoffset*\tkzintervscale}\tkzintervhoriz}]MYNODE.north west) --++ (-\tkzintervscale\tkzintervhoriz,0) ;
}

\newcommand\inttkzintervlbracketf{%
  \draw[line width=\tkzintervscale\tkzintervwidth,tkzinterv lcolor] ([xshift={\fpeval{(1-\tkzintervxoffset)*\tkzintervscale}\tkzintervhoriz}]MYNODE.south west) -| ([xshift={-\fpeval{\tkzintervxoffset*\tkzintervscale}\tkzintervhoriz}]MYNODE.north west) --++ (\tkzintervscale\tkzintervhoriz,0) ;
}

\newcommand\inttkzintervrbracketo{%
  \draw[line width=\tkzintervscale\tkzintervwidth,tkzinterv rcolor] ([xshift={\fpeval{(1+\tkzintervxoffset)*\tkzintervscale}\tkzintervhoriz}]MYNODE.south east) -| ([xshift={\fpeval{\tkzintervxoffset*\tkzintervscale}\tkzintervhoriz}]MYNODE.north east) --++ (\tkzintervscale\tkzintervhoriz,0) ;
}

\newcommand\inttkzintervrbracketf{%
  \draw[line width=\tkzintervscale\tkzintervwidth,tkzinterv rcolor] ([xshift={\fpeval{(\tkzintervxoffset-1)*\tkzintervscale}\tkzintervhoriz}]MYNODE.south east) -| ([xshift={\fpeval{\tkzintervxoffset*\tkzintervscale}\tkzintervhoriz}]MYNODE.north east) --++ (-\tkzintervscale\tkzintervhoriz,0) ;
}

%====main macro (english type)
\NewDocumentCommand\tkzinterval{O{}m}{%
  %keys & booleans
  \restoreKV[tkzinterv]%
  \setKV[tkzinterv]{#1}%
  %nobeforeafter
  \ifboolKV[tkzinterv]{nobeforeafter}%
    {%
      \def\tkzintervspacebefore{}%
      \def\tkzintervspaceafter{}%
    }%
    {}%
  \ifboolKV[tkzinterv]{open}{\setKV[tkzinterv]{FF=false}}{}%
  \ifboolKV[tkzinterv]{open right}{\setKV[tkzinterv]{FF=false}}{}%
  \ifboolKV[tkzinterv]{open left}{\setKV[tkzinterv]{FF=false}}{}%
  %lengths / scale
  \settoheight\tkzintervvarheight{\hbox{x}}%
  \xdef\tkzintervscale{\fpeval{\tkzintervvarheight/\tkzintervfixedheight}}%
  %overlap factor
  \inttkzinttestandsplit{/}{\tkzintervoverlapfactor}{\tkzintervoverlapfactorl}{\tkzintervoverlapfactorr}%
  %before spacing
  \ifboolKV[tkzinterv]{FF}%
    {\tkzintervspacebefore}%
    {}%
  \ifboolKV[tkzinterv]{open}%
    {\inttkzintisin{l}{\tkzintervoverlap}{\kern-\fpeval{(\tkzintervoverlapfactorl)*\tkzintervscale}\tkzintervhoriz\relax}{\tkzintervspacebefore}}%
    {}%
  \ifboolKV[tkzinterv]{open right}%
    {\tkzintervspacebefore}%
    {}%
  \ifboolKV[tkzinterv]{open left}%
    {\inttkzintisin{l}{\tkzintervoverlap}{\kern-\fpeval{(\tkzintervoverlapfactorl)*\tkzintervscale}\tkzintervhoriz\relax}{\tkzintervspacebefore}}%
    {}%
  %picture
  \begin{tikzpicture}[baseline=(MYNODE.base),line cap=butt]
    \tikzset{tkzinterv main/.style={%
       inner xsep=0pt,%
       outer sep=0pt,%
       inner ysep=\dimexpr\tkzintervscale\tkzintervsep+\fpeval{0.5*\tkzintervscale}\tkzintervwidth\relax}
    }%v2
    \node[tkzinterv main] (MYNODE) {\ensuremath{#2}} ;
    \ifboolKV[tkzinterv]{FF}{\inttkzintervlbracketf\inttkzintervrbracketf}{}%
    \ifboolKV[tkzinterv]{open}{\inttkzintervlbracketo\inttkzintervrbracketo}{}%
    \ifboolKV[tkzinterv]{open right}{\inttkzintervlbracketf\inttkzintervrbracketo}{}%
    \ifboolKV[tkzinterv]{open left}{\inttkzintervlbracketo\inttkzintervrbracketf}{}%
  \end{tikzpicture}%
  %after spacing
  \ifboolKV[tkzinterv]{FF}{\tkzintervspaceafter}{}%
  \ifboolKV[tkzinterv]{open}{\inttkzintisin{r}{\tkzintervoverlap}{\kern-\fpeval{(\tkzintervoverlapfactorr)*\tkzintervscale}\tkzintervhoriz\relax}{\tkzintervspaceafter}}{}%
  \ifboolKV[tkzinterv]{open right}{\inttkzintisin{r}{\tkzintervoverlap}{\kern-\fpeval{(\tkzintervoverlapfactorr)*\tkzintervscale}\tkzintervhoriz\relax}{\tkzintervspaceafter}}{}%
  \ifboolKV[tkzinterv]{open left}{\tkzintervspaceafter}{}%
}

%====main macro (french type)
\NewDocumentCommand\tkzintervalle{O{}m}{%
  %keys & booleans
  \restoreKV[tkzinterv]%
  \setKV[tkzinterv]{#1}%
  %nobeforeafter
  \ifboolKV[tkzinterv]{nobeforeafter}%
    {%
      \def\tkzintervspacebefore{}%
      \def\tkzintervspaceafter{}%
    }%
    {}%
  \ifboolKV[tkzinterv]{OO}{\setKV[tkzinterv]{FF=false}}{}%
  \ifboolKV[tkzinterv]{FO}{\setKV[tkzinterv]{FF=false}}{}%
  \ifboolKV[tkzinterv]{OF}{\setKV[tkzinterv]{FF=false}}{}%
  %lengths / scale
  \settoheight\tkzintervvarheight{\hbox{x}}%
  \xdef\tkzintervscale{\fpeval{\tkzintervvarheight/\tkzintervfixedheight}}%
  %overlap factor
  \inttkzinttestandsplit{/}{\tkzintervoverlapfactor}{\tkzintervoverlapfactorl}{\tkzintervoverlapfactorr}%
  %before spacing
  \ifboolKV[tkzinterv]{FF}%
    {\tkzintervspacebefore}%
    {}%
  \ifboolKV[tkzinterv]{OO}%
    {\inttkzintisin{l}{\tkzintervoverlap}{\kern-\fpeval{(\tkzintervoverlapfactorl)*\tkzintervscale}\tkzintervhoriz\relax}{\tkzintervspacebefore}}%
    {}%
  \ifboolKV[tkzinterv]{FO}%
    {\tkzintervspacebefore}%
    {}%
  \ifboolKV[tkzinterv]{OF}%
    {\inttkzintisin{l}{\tkzintervoverlap}{\kern-\fpeval{(\tkzintervoverlapfactorl)*\tkzintervscale}\tkzintervhoriz\relax}{\tkzintervspacebefore}}%
    {}%
  %picture
  \begin{tikzpicture}[baseline=(MYNODE.base),line cap=butt]
    \tikzset{tkzinterv main/.style={%
       inner xsep=0pt,%
       outer sep=0pt,%
       inner ysep=\dimexpr\tkzintervsep+\fpeval{0.5*\tkzintervscale}\tkzintervwidth\relax}
    }%v2
    \node[tkzinterv main] (MYNODE) {\ensuremath{#2}} ;
    \ifboolKV[tkzinterv]{FF}{\inttkzintervlbracketf\inttkzintervrbracketf}{}%
    \ifboolKV[tkzinterv]{OO}{\inttkzintervlbracketo\inttkzintervrbracketo}{}%
    \ifboolKV[tkzinterv]{FO}{\inttkzintervlbracketf\inttkzintervrbracketo}{}%
    \ifboolKV[tkzinterv]{OF}{\inttkzintervlbracketo\inttkzintervrbracketf}{}%
  \end{tikzpicture}%
  %after spacing
  \ifboolKV[tkzinterv]{FF}{\tkzintervspaceafter}{}%
  \ifboolKV[tkzinterv]{OO}{\inttkzintisin{r}{\tkzintervoverlap}{\kern-\fpeval{(\tkzintervoverlapfactorr)*\tkzintervscale}\tkzintervhoriz\relax}{\tkzintervspaceafter}}{}%
  \ifboolKV[tkzinterv]{FO}{\inttkzintisin{r}{\tkzintervoverlap}{\kern-\fpeval{(\tkzintervoverlapfactorr)*\tkzintervscale}\tkzintervhoriz\relax}{\tkzintervspaceafter}}{}%
  \ifboolKV[tkzinterv]{OF}{\tkzintervspaceafter}{}%
}

\endinput