%% 
%% This is file `phy-braket.sty',
%% the `braket' module of `physics2' package.
%% 
%% ******************************************************
%% * This work may be distributed and/or modified under *
%% * the conditions of the LaTeX Project Public License *
%% *                                                    *
%% *     http://www.latex-project.org/lppl.txt          *
%% *                                                    *
%% * either version 1.3c of this license or any later   *
%% * version.                                           *
%% ******************************************************
%% 
%% This work has the LPPL maintenance status `maintained'.
%% 
%% The Current Maintainer of this work is Zhang Tingxuan.
%% 
\ProvidesFile{phy-braket.sty}
  [2023/03/29 `braket' module of `physics2.sty']
% Here we need to get the definition of \phy@abopen and \phy@abclose.
\phy@requiremodule{ab}
\ifdefined\phy@abb@bkv
  \PackageWarning{physics2}{You cannot load `ab.braket' and `braket'
    modules together.\MessageBreak Only `braket' module works now.}
\fi
% \bra
\DeclareDocumentCommand\bra{ s o m }{%
  \IfBooleanTF{#1}%
    {\mathopen\langle#3\mathclose\vert}%
    {\IfValueTF{#2}%
      {\csname#2l\endcsname\langle#3\csname#2r\endcsname\vert}%
      {\phy@abopen\langle#3\phy@abclose\vert}%
    }%
}
% \ket
\DeclareDocumentCommand\ket{ s o m }{%
  \IfBooleanTF{#1}%
    {\mathopen\vert#3\mathclose\rangle}%
    {\IfValueTF{#2}%
      {\csname#2l\endcsname\vert#3\csname#2r\endcsname\rangle}%
      {\phy@abopen\vert#3\phy@abclose\rangle}%
    }%
}
% \braket
\DeclareDocumentCommand\braket{ s O{} }{%
  \IfBooleanTF{#1}%
    {%
      \gdef\@phy@bk@argnum{ii}%
      \phy@bk@doopt{#2}%
      \gdef\@phy@bk@l{mathopen}%
      \gdef\@phy@bk@m{mathord}%
      \gdef\@phy@bk@r{mathclose}%
    }%
    {%
      \gdef\@phy@bk@argnum{ii}%
      \gdef\@phy@bk@l{phy@abopen}%
      \gdef\@phy@bk@m{middle}%
      \gdef\@phy@bk@r{phy@abclose}%
      \phy@bk@doopt{#2}%
    }%
  \csname phy@bk@in@\@phy@bk@argnum\endcsname%
}
% The internal version of \braket
\def\phy@bk@in@i#1{%
  \csname\@phy@bk@l\endcsname\langle{#1}%
  \csname\@phy@bk@r\endcsname\rangle}
\def\phy@bk@in@ii#1#2{%
  \csname\@phy@bk@l\endcsname\langle{#1}%
  \csname\@phy@bk@m\endcsname\vert{#2}%
  \csname\@phy@bk@r\endcsname\rangle}
\def\phy@bk@in@iii#1#2#3{%
  \csname\@phy@bk@l\endcsname\langle{#1}%
  \csname\@phy@bk@m\endcsname\vert{#2}%
  \csname\@phy@bk@m\endcsname\vert{#3}%
  \csname\@phy@bk@r\endcsname\rangle}
% Parse the optional argument of \braket. This will add 3 entries to hash table.
\def\@phy@bk@do@pt#1,{\ifx#1\relax\@empty\else
  \edef\reserved@a{\zap@space#1 \@empty}%
  \ifx\reserved@a\@empty\else
    \ifcsname phy@del\expandafter\string\csname\reserved@a\endcsname\endcsname
      \xdef\@phy@bk@l{\reserved@a l}%
      \xdef\@phy@bk@m{\reserved@a}% but not m (m stands for \mathrel)
      \xdef\@phy@bk@r{\reserved@a r}%
    \else
      \ifnum\reserved@a>3%
        \PackageError{physics2}{\string\braket\space can only take 3
          mandatory arguments at most}{Check if you had written a number
          more than 3 in the [optional] argument.}%
      \fi
      \xdef\@phy@bk@argnum{\romannumeral\reserved@a}%
    \fi
  \fi
\expandafter\@phy@bk@do@pt\fi}
\def\phy@bk@doopt#1{\@phy@bk@do@pt#1,\relax,}
% \ketbra
\DeclareDocumentCommand\ketbra{ s o m O{} m }{%
  \IfBooleanTF{#1}%
    {\mathopen\vert#3\mathclose\rangle#4\mathopen\langle#5\mathclose\vert}%
    {\IfValueTF{#2}%
      {\csname#2l\endcsname\vert#3\csname#2r\endcsname\rangle#4%
        \csname#2l\endcsname\langle#5\csname#2r\endcsname\vert}%
      {\begingroup
        \phy@abopen\vert\mathopen{\phy@mathvphantom{#5}}#3\phy@abclose\rangle#4%
        \phy@abopen\langle#5\mathclose{\phy@mathvphantom{#3}}\phy@abclose\vert
      \endgroup}%
    }%
}
\endinput
%% End of file `phy-braket.sty'.
