% \iffalse meta-comment
%
% File: siunitx-v3.dtx Copyright (C) 2016-2017 Joseph Wright
%
% It may be distributed and/or modified under the conditions of the
% LaTeX Project Public License (LPPL), either version 1.3c of this
% license or (at your option) any later version.  The latest version
% of this license is in the file
%
%    https://www.latex-project.org/lppl.txt
%
% This file is part of the "siunitx bundle" (The Work in LPPL)
% and all files in that bundle must be distributed together.
%
% The released version of this bundle is available from CTAN.
%
% -----------------------------------------------------------------------
%
% The development version of the bundle can be found at
%
%    https://github.com/josephwright/siunitx
%
% for those people who are interested.
%
% -----------------------------------------------------------------------
%
%<*driver>
\documentclass{l3doc}
% The next line is needed so that \GetFileInfo will be able to pick up
% version data
\usepackage{siunitx}
\begin{document}
  \DocInput{\jobname.dtx}
\end{document}
%</driver>
% \fi
%
% \GetFileInfo{siunitx.sty}
%
% \title{^^A
%   \pkg{siunitx} -- Overall set up^^A
%   \thanks{This file describes \fileversion,
%     last revised \filedate.}^^A
% }
%
% \author{^^A
%  Joseph Wright^^A
%  \thanks{^^A
%    E-mail:
%    \href{mailto:joseph.wright@morningstar2.co.uk}
%      {joseph.wright@morningstar2.co.uk}^^A
%   }^^A
% }
%
% \date{Released \filedate}
%
% \maketitle
%
% \begin{documentation}
%
% \end{documentation}
%
% \begin{implementation}
%
% \section{\pkg{siunitx} implementation}
%
% Start the \pkg{DocStrip} guards.
%    \begin{macrocode}
%<*package>
%    \end{macrocode}
%
% Identify the internal prefix (\LaTeX3 \pkg{DocStrip} convention).
%    \begin{macrocode}
%<@@=siunitx>
%    \end{macrocode}
%
% \subsection{Initial set up}
%
% Load only the essential support (\pkg{expl3}) \enquote{up-front}.
%    \begin{macrocode}
\RequirePackage{expl3}
%    \end{macrocode}
%
% Make sure that the version of \pkg{l3kernel} in use is sufficiently new.
% This will also trap any problems with \pkg{l3packages} (as the two are now
% tied together, version-wise).
%    \begin{macrocode}
\@ifpackagelater {expl3}{2015/11/15}
  {}
  {%
    \PackageError{siunitx} {Support package expl3 too old}
      {%
        You need to update your installation of the bundles 'l3kernel' and
        'l3packages'.\MessageBreak
        Loading~siunitx~will~abort!%
      }%
    \endinput
  }%
%    \end{macrocode}
%
% Identify the package and give the over all version information.
%    \begin{macrocode}
\ProvidesExplPackage {siunitx} {2017/11/26} {3.0alpha}
  {A comprehensive (SI) units package}
%    \end{macrocode}
%
% \subsection{Safety checks}
%
% \begin{macro}{\@@_load_check:}
%   There are a number of packages that are incompatible with \pkg{siunitx}
%   as they cover the same concepts and in some cases define the same command
%   names. These are all tested at the point of loading to try to trap
%   issues, and a couple are also tested later as it's possible for them to
%   load without an obvious error if \pkg{siunitx} was loaded first.
%
%   The testing here is done in a group so that the tests do not add
%   anything to the hash table.
%    \begin{macrocode}
\msg_new:nnnn { siunitx } { incompatible-package }
  { Package~'#1'~incompatible. }
  { The~#1~package~and~siunitx~are~incompatible. }
\cs_new_protected:Npn \@@_load_check:n #1
  {
    \group_begin:
      \@ifpackageloaded {#1}
        {
          \group_end:
          \msg_error:nnx { siunitx } { incompatible-package } {#1}
        }
        { \group_end: }
  }
\clist_map_function:nN
  { SIunits , sistyle , unitsdef , fancyunits }
  \@@_load_check:n
\AtBeginDocument
  {
    \clist_map_function:nN { SIunits , sistyle }
      \@@_load_check:n
  }
%    \end{macrocode}
%\end{macro}
%
% \subsection{Top-level scratch space}
%
% \begin{macro}{\l_@@_tmp_tl}
%   Scratch space for the interfaces.
%    \begin{macrocode}
\tl_new:N \l_@@_tmp_tl
%    \end{macrocode}
% \end{macro}
%
% \subsection{User interfaces}
%
% The user interfaces are defined in terms of documented code-level ones.
% This is all done here, and will appear in the \file{.sty} file before the
% relevant code. Things could be re-arranged by \pkg{DocStrip} but there is no
% advantage.
%
% User level interfaces are all created by \pkg{xparse}
%    \begin{macrocode}
\RequirePackage { xparse }
%    \end{macrocode}
%
% \subsubsection{Preamble commands}
%
% \begin{macro}
%   {
%     \DeclareSIPower     ,
%     \DeclareSIPrefix    ,
%     \DeclareSIQualifier ,
%     \DeclareSIUnit
%   }
%   Pass data to the code layer.
%    \begin{macrocode}
\NewDocumentCommand \DeclareSIPower { +m +m m }
  {
    \siunitx_declare_power:NNn #1 #2 {#3}
  }
\NewDocumentCommand \DeclareSIPrefix { +m m m }
  {
    \siunitx_declare_prefix:Nnn #1 {#2} {#3}
  }
\NewDocumentCommand \DeclareSIQualifier { +m m }
  {
    \siunitx_declare_qualifier:Nn #1 {#2}
  }
\NewDocumentCommand \DeclareSIUnit { O { } +m m }
  {
    \siunitx_declare_unit:Nn #2 {#3}
  }
%    \end{macrocode}
% \end{macro}
%
% \subsubsection{Document commands}
%
% \begin{macro}{\qty}
%    \begin{macrocode}
\NewDocumentCommand \qty { O { } m m }
  {
    \leavevmode
    \group_begin:
      \keys_set:nn { siunitx } {#1}
      \siunitx_number_format:nN {#2} \l_@@_tmp_tl
      \siunitx_print:nV { number } \l_@@_tmp_tl
      \, \nobreak % TEMP
      \siunitx_unit_format:nN {#3} \l_@@_tmp_tl
      \siunitx_print:nV { unit } \l_@@_tmp_tl
    \group_end:
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\num, \unit}
%   All of a standard form: start a paragraph (if required), set local
%   key values, do the formatting, print the result.
%    \begin{macrocode}
\NewDocumentCommand \num { O { } m }
  {
    \leavevmode
    \group_begin:
      \keys_set:nn { siunitx } {#1}
      \siunitx_number_format:nN {#2} \l_@@_tmp_tl
      \siunitx_print:nV { number } \l_@@_tmp_tl
    \group_end:
  }
\NewDocumentCommand \unit { O { } m }
  {
    \leavevmode
    \group_begin:
      \keys_set:nn { siunitx } {#1}
      \siunitx_unit_format:nN {#2} \l_@@_tmp_tl
      \siunitx_print:nV { unit } \l_@@_tmp_tl
    \group_end:
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\sisetup}
%   A very thin wrapper.
%    \begin{macrocode}
\NewDocumentCommand \sisetup { m }
  { \keys_set:nn { siunitx } {#1} }
%    \end{macrocode}
% \end{macro}
%
%
% \subsubsection{Table column}
%
% User interfaces in tabular constructs are provided using the mechanisms from
% the \pkg{array} package.
%    \begin{macrocode}
\RequirePackage { array }
%    \end{macrocode}
%
% There is a slight problem with the \pkg{cellspace} package: it also uses
% \texttt{S} for a column type  Here, \texttt{S} seems to make more sense for
% \pkg{siunitx}, with \texttt{C} used for \pkg{cellspace}. To enable this to
% work well, the column rewriting code happens \cs{AtBeginDocument}. The
% \texttt{S} column is deleted from \cs{NC@list} so that no warning appears.
%    \begin{macrocode}
\AtBeginDocument
  {
    \@ifpackageloaded { cellspace }
      {
        \newcolumntype { C } [ 1 ]
          { > { \bcolumn #1 \@nil } #1 < { \ecolumn } }
        \cs_set:Npn \@@_tmp:w #1 \NC@do S #2 \q_stop
          { \NC@list { #1 #2 } }
        \exp_after:wN \@@_tmp:w \tex_the:D \NC@list \q_stop
        \cs_undefine:N \NC@find@S
        \msg_warning:nnn { siunitx } { moved-cellspace-column } { C }
        \ifcellspace@m
          \def \env@matrix
            {
              \hskip -\arraycolsep
              \let \@ifnextchar \new@ifnextchar
              \array
                {
                  * { \c@MaxMatrixCols }
                    { > { \bcolumn c \@nil $ } c < { $ \ecolumn } } @ { }
                }
            }
        \fi
      }
      { }
  }
\msg_new:nnn { siunitx } { moved-cellspace-column }
  { Column~type~for~cellspace~package~moved~to~'#1'. }
%    \end{macrocode}
%
% \begin{macro}{\@@_declare_column:Nnn}
%   Creating numerical columns requires that these are declared before anything
%   else in \cs{NC@list}: this is necessary to work with optional arguments.
%   This means a bit of manual effort after the simple declaration of a new
%   column type. The token assigned to the column type is not fixed as this
%   allows the same code to be used in compatibility with version~$2$.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_declare_column:Nnn #1#2#3
  {
    \newcolumntype {#1} { }
    \cs_set_protected:Npn \@@_tmp:w \NC@do ##1##2 \NC@do #1
      { \NC@list { \NC@do ##1 \NC@do #1 ##2 } }
    \exp_after:wN \@@_tmp:w \tex_the:D \NC@list
    \exp_args:NNc \renewcommand * { NC@rewrite@ #1 } [ 1 ] [ ]
      {
        \@temptokena \expandafter
          {
            \the \@temptokena
            > {#2} c < {#3}
          }
        \NC@find
      }
  }
%    \end{macrocode}
%   When \pkg{mdwtab} is loaded the syntax required is slightly different.
%    \begin{macrocode}
\AtBeginDocument
  {
    \@ifpackageloaded { mdwtab }
      {
        \cs_set_protected:Npn \@@_declare_column:Nnn #1#2#3
          {
            \newcolumntype {#1} [ 1 ] [ ]
              { > {#2} c < {#3} }
           }
      }
      { }
  }
\AtBeginDocument
  {
    \@@_declare_column:Nnn n
      {
        \keys_set:nn { siunitx } {#1}
        \siunitx_cell_begin:
      }
      { \siunitx_cell_end: }
  }
%    \end{macrocode}
% \end{macro}
%
%    \begin{macrocode}
%</package>
%    \end{macrocode}
%
% \end{implementation}
%
% \PrintIndex
