%%%
% Ecritures d'unit\'es
%%%
\def\filedateEcrireUnites{2024/08/04}%
\def\fileversionEcrireUnites{0.1}%
\message{-- \filedateEcrireUnites\space v\fileversionEcrireUnites}%
%
\setKVdefault[Unites]{m=false,km=false,hm=false,ha=false,dam=false,a=false,dm=false,cm=true,mm=false,um=false,nm=false,g=true,t=false,q=false,kg=false,hg=false,dag=false,dg=false,cg=false,mg=false,ug=false,ng=false,mmin=false,kmh=true,kms=false,ms=false,mh=false,kgm=false,gcm=true,L=true,kL=false,hL=false,daL=false,dL=false,cL=false,mL=false,l=true,kl=false,hl=false,dal=false,dl=false,cl=false,ml=false,Go=true,Mo=false,ko=false,To=false,o=false,kWh=true,C=true,K=false,F=false,Nombre,US=false,UK=false,CFA=false,CFP=false,CHF=false,Euro=false,Precision=2}%
\defKV[Unites]{Dots=\setKV[Unites]{Nombre=false}}%

\newlength{\PfCLgUnites}

%D'apres https://tex.stackexchange.com/questions/38905/time-of-the-day-or-time-period-using-the-package-siunitx
\ExplSyntaxOn
\NewDocumentCommand \Temps {o o > { \SplitArgument { 5 } { ; } } m }
{%
  \useKVdefault[Unites]%
  \setKV[Unites]{#1}
  \group_begin:%
  \IfNoValueF{#2}
  { \keys_set:nn { siunitx } {#2} }
  \siunitx_hms_output:nnn #3
  \group_end:
}
\cs_new_protected:Npn \siunitx_hms_output:nnn #1#2#3#4#5#6
{%
  \IfNoValueF {#1}%
  {%
    \tl_if_blank:nF {#1}%
    {%
      \ifboolKV[Unites]{Nombre}{%
        \SI{#1}{\annee\xintifboolexpr{#1>1}{s}{}}%
      }{%
      \setlength{\PfCLgUnites}{\useKV[Unites]{Dots}}%
      \pointilles[\PfCLgUnites]~\si{\annee(s)}}%
      \IfNoValueF {#2}{~}%
    }%
  }%
  \IfNoValueF {#2}%
  {%
    \tl_if_blank:nF {#2}%
    {%
      \ifboolKV[Unites]{Nombre}{%
        \SI{#2}{\mois}%
      }{%
      \setlength{\PfCLgUnites}{\useKV[Unites]{Dots}}%
      \pointilles[\PfCLgUnites]~\si{\mois}}%
      \IfNoValueF{#3}{~}%
    }%
  }%
  \IfNoValueF {#3}%
  {%
    \tl_if_blank:nF {#3}%
    {%
      \ifboolKV[Unites]{Nombre}{%
         \SI{#3}{\jour}%
      }{%
      \setlength{\PfCLgUnites}{\useKV[Unites]{Dots}}%
      \pointilles[\PfCLgUnites]~\si{\jour}}%      
      \IfNoValueF{#4}{~}%
    }%
  }%
  \IfNoValueF {#4}%
  {%
    \tl_if_blank:nF {#4}%
    {%
      \ifboolKV[Unites]{Nombre}{%
        \SI{#4}{\hour}%
      }{%
      \setlength{\PfCLgUnites}{\useKV[Unites]{Dots}}%
      \pointilles[\PfCLgUnites]~\si{\hour}}%
      \IfNoValueF{#5}{~}%
    }%
  }%
  \IfNoValueF {#5}%
  {%
    \tl_if_blank:nF {#5}%
    {%
      \ifboolKV[Unites]{Nombre}{%
        \SI{#5}{\minute}%
      }{%
      \setlength{\PfCLgUnites}{\useKV[Unites]{Dots}}%
      \pointilles[\PfCLgUnites]~\si{\minute}}%
      \IfNoValueF{#6}{~}%
    }%
  }%
  \IfNoValueF {#6}%
  {%
    \tl_if_blank:nF {#6} {%
      \ifboolKV[Unites]{Nombre}{%
        \SI{#6}{\second}}{%
        \setlength{\PfCLgUnites}{\useKV[Unites]{Dots}}%
        \pointilles[\PfCLgUnites]~\si{\second}}%
    }%
  }%
}%

\NewDocumentCommand \Horaire {o o > { \SplitArgument { 2 } { ; } } m }
{%
  \useKVdefault[Unites]%
  \setKV[Unites]{#1}
  \group_begin:%
  \IfNoValueF{#2}
  { \keys_set:nn { siunitx } {#2} }
  \siunitx_horaire_output:nnn #3
  \group_end:
}
\cs_new_protected:Npn \siunitx_horaire_output:nnn #1#2#3
{%
  \IfNoValueF {#1}%
  {%
    \tl_if_blank:nF {#1}%
    {%
      \ifboolKV[Unites]{Nombre}{%
        \SI{#1}{\hour}%
      }{%
      \setlength{\PfCLgUnites}{\useKV[Unites]{Dots}}%
      \pointilles[\PfCLgUnites]~\si{\hour}}%
      \IfNoValueF{#2}{~}%
    }%
  }%
  \IfNoValueF {#2}%
  {%
    \tl_if_blank:nF {#2}%
    {%
      \ifboolKV[Unites]{Nombre}{%
        \SI[minimum-integer-digits=2]{#2}{\minute}%
      }{%
      \setlength{\PfCLgUnites}{\useKV[Unites]{Dots}}%
      \pointilles[\PfCLgUnites]~\si{\minute}}%
      \IfNoValueF{#3}{~}%
    }%
  }%
  \IfNoValueF {#3}%
  {%
    \tl_if_blank:nF {#3} {%
      \ifboolKV[Unites]{Nombre}{%
        \SI{#3}{\second}}{%
        \setlength{\PfCLgUnites}{\useKV[Unites]{Dots}}%
        \pointilles[\PfCLgUnites]~\si{\second}}%
    }%
  }%
}%
\ExplSyntaxOff

\NewDocumentCommand\Temp{o m}{%
  \useKVdefault[Unites]%
  \setKV[Unites]{#1}%
  \ifboolKV[Unites]{F}{%
    \DeclareSIUnit{\Tempo}{\fahrenheit}%
  }{%
    \ifboolKV[Unites]{K}{%
      \DeclareSIUnit{\Tempo}{\kelvin}%
    }{%
      \DeclareSIUnit{\Tempo}{\celsius}%
    }%
  }%
  \ifboolKV[Unites]{Nombre}{%
    \SI{#2}{\Tempo}%
  }{%
    \setlength{\PfCLgUnites}{\useKV[Unites]{Dots}}%
    \pointilles[\PfCLgUnites]~\si{\Tempo}%
  }%
}%

\NewDocumentCommand\Conso{o m}{%
  \useKVdefault[Unites]%
  \setKV[Unites]{#1}%
  \ifboolKV[Unites]{Nombre}{%
    \SI{#2}{\kWh}%
  }{%
  \setlength{\PfCLgUnites}{\useKV[Unites]{Dots}}%
  \pointilles[\PfCLgUnites]~\si{\kWh}%
  }%
}%

\makeatletter
\NewDocumentCommand\Prix{o m}{%
  \useKVdefault[Unites]%
  \setKV[Unites]{#1}%
  \ifboolKV[Unites]{CHF}{%
    \DeclareSIUnit{\Tempo}{\PfCCHF}%
  }{%
    \ifboolKV[Unites]{CFA}{%
      \DeclareSIUnit{\Tempo}{\PfCCFA}%
    }{%
      \ifboolKV[Unites]{CFP}{%
        \DeclareSIUnit{\Tempo}{\PfCCFP}%
      }{%
        \ifboolKV[Unites]{US}{%
          \DeclareSIUnit{\Tempo}{\Dollar}%
        }{%
          \ifboolKV[Unites]{UK}{%
            \DeclareSIUnit{\Tempo}{\LivreSterling}%
          }{%
            \ifboolKV[Unites]{Euro}{%
              \DeclareSIUnit{\Tempo}{\EuRo}%
            }{%
              \if@CHF%
                \setKV[Unites]{CHF}%
              \fi%
              \DeclareSIUnit{\Tempo}{\DeviseDefaut}%
            }%
          }%
        }%
      }%
    }%
  }%
  \ifboolKV[Unites]{Nombre}{%
    \IfInteger{#2}{%
      \ifboolKV[Unites]{CHF}{%
        \si{\PfCCHF}~\num{#2}.-%
      }{%
        \SI{#2}{\Tempo}%
      }%
    }{%
      \SI[round-mode=places,round-precision=\useKV[Unites]{Precision}]{#2}{\Tempo}%
    }%
  }{%
    \setlength{\PfCLgUnites}{\useKV[Unites]{Dots}}%
    \pointilles[\PfCLgUnites]~\si{\Tempo}%
  }%
}%
\makeatother

%\NewDocumentCommand\Prixold{o m}{%
%  \useKVdefault[Unites]%
%  \setKV[Unites]{#1}%
%  \ifboolKV[Unites]{US}{%
%    \DeclareSIUnit{\Tempo}{\Dollar}%
%  }{%
%    \ifboolKV[Unites]{UK}{%
%      \DeclareSIUnit{\Tempo}{\LivreSterling}%
%    }{%
%      \DeclareSIUnit{\Tempo}{\EuRo}%
%    }%
%  }%
%  \ifboolKV[Unites]{Nombre}{%
%    \IfInteger{#2}{%
%      \SI{#2}{\Tempo}%
%    }{%
%      \SI[round-mode=places,round-precision=\useKV[Unites]{Precision}]{#2}{\Tempo}%
%    }%
%  }{%
%    \setlength{\PfCLgUnites}{\useKV[Unites]{Dots}}%
%    \pointilles[\PfCLgUnites]~\si{\Tempo}%
%  }%
%}%

\NewDocumentCommand\Octet{o m}{%
  \useKVdefault[Unites]%
  \setKV[Unites]{#1}%
  \ifboolKV[Unites]{o}{%
    \DeclareSIUnit{\Tempo}{\octet}%
  }{%
    \ifboolKV[Unites]{ko}{%
      \DeclareSIUnit{\Tempo}{\kilo\octet}%
    }{%
      \ifboolKV[Unites]{Mo}{%
        \DeclareSIUnit{\Tempo}{\mega\octet}%
      }{%
        \ifboolKV[Unites]{To}{%
          \DeclareSIUnit{\Tempo}{\tera\octet}%
        }{%
          \DeclareSIUnit{\Tempo}{\giga\octet}%
        }%
      }%
    }%
  }%
  \ifboolKV[Unites]{Nombre}{%
    \SI{#2}{\Tempo}%
  }{%
    \setlength{\PfCLgUnites}{\useKV[Unites]{Dots}}%
    \pointilles[\PfCLgUnites]~\si{\Tempo}%
  }%
}%

\NewDocumentCommand\Lg{s o m}{%
  \useKVdefault[Unites]%
  \setKV[Unites]{#2}%
  \ifboolKV[Unites]{nm}{%
    \DeclareSIUnit{\Tempo}{\nano\meter}%
  }{%
    \ifboolKV[Unites]{um}{%
      \DeclareSIUnit{\Tempo}{\micro\meter}%
    }{%
      \ifboolKV[Unites]{km}{%
        \DeclareSIUnit{\Tempo}{\kilo\meter}%
      }{%
        \ifboolKV[Unites]{hm}{%
          \DeclareSIUnit{\Tempo}{\hecto\meter}%
        }{%
          \ifboolKV[Unites]{dam}{%
            \DeclareSIUnit{\Tempo}{\deca\meter}%
          }{%
            \ifboolKV[Unites]{m}{%
              \DeclareSIUnit{\Tempo}{\meter}%
            }{%
              \ifboolKV[Unites]{dm}{%
                \DeclareSIUnit{\Tempo}{\deci\meter}%
              }{%
                \ifboolKV[Unites]{mm}{%
                  \DeclareSIUnit{\Tempo}{\milli\meter}%
                }{%
                  \DeclareSIUnit{\Tempo}{\centi\meter}%
                }%
              }%
            }%
          }%
        }%
      }%
    }%
  }%
  \IfBooleanTF{#1}{%
    \ifboolKV[Unites]{Nombre}{%
      \SI[parse-numbers=false]{#3}{\Tempo}%
    }{%
      \setlength{\PfCLgUnites}{\useKV[Unites]{Dots}}%
      \pointilles[\PfCLgUnites]~\si{\Tempo}%
    }%
  }{%
    \ifboolKV[Unites]{Nombre}{%
      \SI{#3}{\Tempo}%
    }{%
      \setlength{\PfCLgUnites}{\useKV[Unites]{Dots}}%
      \pointilles[\PfCLgUnites]~\si{\Tempo}%
    }%
  }%
}%

\NewDocumentCommand\Masse{s o m}{%
  \useKVdefault[Unites]%
  \setKV[Unites]{#2}%
  \ifboolKV[Unites]{ng}{%
    \DeclareSIUnit{\Tempo}{\nano\gram}%
  }{\ifboolKV[Unites]{ug}{%
      \DeclareSIUnit{\Tempo}{\micro\gram}%
    }{\ifboolKV[Unites]{t}{%
        \DeclareSIUnit{\Tempo}{\tonne}%
      }{\ifboolKV[Unites]{q}{%
          \DeclareSIUnit{\Tempo}{\quintal}%
        }{%
          \ifboolKV[Unites]{kg}{%
            \DeclareSIUnit{\Tempo}{\kilo\gram}%
          }{\ifboolKV[Unites]{hg}{%
              \DeclareSIUnit{\Tempo}{\hecto\gram}%
            }{\ifboolKV[Unites]{dag}{%
                \DeclareSIUnit{\Tempo}{\deca\gram}%
              }{\ifboolKV[Unites]{dg}{%
                  \DeclareSIUnit{\Tempo}{\deci\gram}%
                }{\ifboolKV[Unites]{cg}{%
                    \DeclareSIUnit{\Tempo}{\centi\gram}%
                  }{\ifboolKV[Unites]{mg}{%
                      \DeclareSIUnit{\Tempo}{\milli\gram}%
                    }{%
                      \DeclareSIUnit{\Tempo}{\gram}%
                    }%
                  }%
                }%
              }%
            }%
          }%
        }%
      }%
    }%
  }%
  \IfBooleanTF{#1}{%
    \ifboolKV[Unites]{Nombre}{%
    \SI[parse-numbers=false]{#3}{\Tempo}%
  }{%
    \setlength{\PfCLgUnites}{\useKV[Unites]{Dots}}%
    \pointilles[\PfCLgUnites]~\si{\Tempo}%
  }%
  }{%
    \ifboolKV[Unites]{Nombre}{%
    \SI{#3}{\Tempo}%
  }{%
    \setlength{\PfCLgUnites}{\useKV[Unites]{Dots}}%
    \pointilles[\PfCLgUnites]~\si{\Tempo}%
  }%
  }%
}%

\NewDocumentCommand\Capa{s o m}{%
  \useKVdefault[Unites]%
  \setKV[Unites]{#2}%
  \ifboolKV[Unites]{kL}{%
    \DeclareSIUnit{\Tempo}{\kilo\liter}%
  }{\ifboolKV[Unites]{hL}{%
      \DeclareSIUnit{\Tempo}{\hecto\liter}%
    }{\ifboolKV[Unites]{daL}{%
        \DeclareSIUnit{\Tempo}{\deca\liter}%
      }{\ifboolKV[Unites]{dL}{%
          \DeclareSIUnit{\Tempo}{\deci\liter}%
        }{\ifboolKV[Unites]{cL}{%
            \DeclareSIUnit{\Tempo}{\centi\liter}%
          }{\ifboolKV[Unites]{mL}{%
              \DeclareSIUnit{\Tempo}{\milli\liter}%
            }{%
              \DeclareSIUnit{\Tempo}{\liter}%
            }%
          }%
        }%
      }%
    }%
  }%
  \IfBooleanTF{#1}{%
    \ifboolKV[Unites]{Nombre}{%
      \SI[parse-numbers=false]{#3}{\Tempo}%
    }{%
      \setlength{\PfCLgUnites}{\useKV[Unites]{Dots}}%
      \pointilles[\PfCLgUnites]~\si{\Tempo}%
    }%
  }{%
  \ifboolKV[Unites]{Nombre}{%
    \SI{#3}{\Tempo}%
  }{%
    \setlength{\PfCLgUnites}{\useKV[Unites]{Dots}}%
    \pointilles[\PfCLgUnites]~\si{\Tempo}%
  }%
  }%
}%

\NewDocumentCommand\Aire{s o m}{%
  \useKVdefault[Unites]%
  \setKV[Unites]{#2}%
  \ifboolKV[Unites]{ha}{%
    \DeclareSIUnit{\Tempo}{\hectare}%
  }{%
    \ifboolKV[Unites]{a}{%
      \DeclareSIUnit{\Tempo}{\are}%
    }{%
      \ifboolKV[Unites]{nm}{%
        \DeclareSIUnit{\Tempo}{\square\nano\meter}%
      }{%
        \ifboolKV[Unites]{um}{%
          \DeclareSIUnit{\Tempo}{\square\micro\meter}%
        }{%
          \ifboolKV[Unites]{km}{%
            \DeclareSIUnit{\Tempo}{\square\kilo\meter}%
          }{%
            \ifboolKV[Unites]{hm}{%
              \DeclareSIUnit{\Tempo}{\square\hecto\meter}%
            }{%
              \ifboolKV[Unites]{dam}{%
                \DeclareSIUnit{\Tempo}{\square\deca\meter}%
              }{%
                \ifboolKV[Unites]{m}{%
                  \DeclareSIUnit{\Tempo}{\square\meter}%
                }{%
                  \ifboolKV[Unites]{dm}{%
                    \DeclareSIUnit{\Tempo}{\square\deci\meter}%
                  }{%
                    \ifboolKV[Unites]{mm}{%
                      \DeclareSIUnit{\Tempo}{\square\milli\meter}%
                    }{%
                      \DeclareSIUnit{\Tempo}{\square\centi\meter}%
                    }%
                  }%
                }%
              }%
            }%
          }%
        }%
      }%
    }%
  }%
  \IfBooleanTF{#1}{%
    \ifboolKV[Unites]{Nombre}{%
      \SI[parse-numbers=false]{#3}{\Tempo}%
    }{%
      \setlength{\PfCLgUnites}{\useKV[Unites]{Dots}}%
      \pointilles[\PfCLgUnites]~\si{\Tempo}%
    }%
  }{%
    \ifboolKV[Unites]{Nombre}{%
      \SI{#3}{\Tempo}%
    }{%
      \setlength{\PfCLgUnites}{\useKV[Unites]{Dots}}%
      \pointilles[\PfCLgUnites]~\si{\Tempo}%
    }%
  }%
}%

\NewDocumentCommand\Vol{s o m}{%
  \useKVdefault[Unites]%
  \setKV[Unites]{#2}%
  \ifboolKV[Unites]{nm}{%
    \DeclareSIUnit{\Tempo}{\nano\meter}%
  }{%
    \ifboolKV[Unites]{um}{%
      \DeclareSIUnit{\Tempo}{\micro\meter}%
    }{%
      \ifboolKV[Unites]{km}{%
        \DeclareSIUnit{\Tempo}{\kilo\meter}%
      }{%
        \ifboolKV[Unites]{hm}{%
          \DeclareSIUnit{\Tempo}{\hecto\meter}%
        }{%
          \ifboolKV[Unites]{dam}{%
            \DeclareSIUnit{\Tempo}{\deca\meter}%
          }{%
            \ifboolKV[Unites]{m}{%
              \DeclareSIUnit{\Tempo}{\meter}%
            }{%
              \ifboolKV[Unites]{dm}{%
                \DeclareSIUnit{\Tempo}{\deci\meter}%
              }{%
                \ifboolKV[Unites]{mm}{%
                  \DeclareSIUnit{\Tempo}{\milli\meter}%
                }{%
                  \DeclareSIUnit{\Tempo}{\centi\meter}%
                }%
              }%
            }%
          }%
        }%
      }%
    }%
  }%
  \IfBooleanTF{#1}{%
    \ifboolKV[Unites]{Nombre}{%
      \SI[parse-numbers=false]{#3}{\cubic\Tempo}%
    }{%
      \setlength{\PfCLgUnites}{\useKV[Unites]{Dots}}%
      \pointilles[\PfCLgUnites]~\si{\cubic\Tempo}%
    }%
  }{%
    \ifboolKV[Unites]{Nombre}{%
      \SI{#3}{\cubic\Tempo}%
    }{%
      \setlength{\PfCLgUnites}{\useKV[Unites]{Dots}}%
      \pointilles[\PfCLgUnites]~\si{\cubic\Tempo}%
    }%
  }%
}%

\NewDocumentCommand\Vitesse{o m}{%
  \useKVdefault[Unites]%
  \setKV[Unites]{#1}%
  \ifboolKV[Unites]{mmin}{%
    \DeclareSIUnit{\Tempo}{\meter\per\minute}%
  }{%
    \ifboolKV[Unites]{mh}{%
      \DeclareSIUnit{\Tempo}{\meter\per\hour}%
    }{%
      \ifboolKV[Unites]{ms}{%
        \DeclareSIUnit{\Tempo}{\meter\per\second}%
      }{%
        \ifboolKV[Unites]{kms}{%
          \DeclareSIUnit{\Tempo}{\kilo\meter\per\second}%
        }{%
          \DeclareSIUnit{\Tempo}{\kilo\meter\per\hour}%
        }%
      }%
    }%
  }%
  \ifboolKV[Unites]{Nombre}{%
    \SI[per-mode=symbol]{#2}{\Tempo}%
  }{%
    \setlength{\PfCLgUnites}{\useKV[Unites]{Dots}}%
    \pointilles[\PfCLgUnites]~\si[per-mode=symbol]{\Tempo}%
  }%
}%

\NewDocumentCommand\MasseVol{o m}{%
  \useKVdefault[Unites]%
  \setKV[Unites]{#1}%
  \ifboolKV[Unites]{kgm}{%
    \DeclareSIUnit{\Tempo}{\kilo\gram\per\cubic\meter}%
  }{%
    \DeclareSIUnit{\Tempo}{\gram\per\cubic\centi\meter}%
  }%
  \ifboolKV[Unites]{Nombre}{%
    \SI[per-mode=symbol]{#2}{\Tempo}%
  }{%
    \setlength{\PfCLgUnites}{\useKV[Unites]{Dots}}%
    \pointilles[\PfCLgUnites]~\si[per-mode=symbol]{\Tempo}%
  }%
}%