%%
%% This is file `letterswitharrows.sty',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% letterswitharrows.dtx  (with options: `package')
%% 
%% File: letterswitharrows.dtx
%% Copyright 2019-2020 J. Maximilian Teegen
%%
%% This work may be distributed and/or modified under the
%% conditions of the LaTeX Project Public License, either version 1.3
%% of this license or (at your option) any later version.
%% The latest version of this license is in
%% http://www.latex-project.org/lppl.txt
%% and version 1.3 or later is part of all distributions of LaTeX
%% version 2005/12/01 or later.
%%
%% This work has the LPPL maintenance status "maintained".
%%
%% The Current Maintainer of this work is J. M. Teegen.
%%
%% This work consists of the file letterswitharrows.dtx and its
%% derived files letterswitharrows.sty and letterswitharrows.pdf.















\NeedsTeXFormat{LaTeX2e}
\RequirePackage{expl3}
\ProvidesExplPackage {letterswitharrows} {2020-02-04} {} {Draw arrows over math letters.}
\RequirePackage{xparse,l3keys2e,mathtools}

\msg_new:nnn {letterswitharrows} {pdf-only} {Only~pdf~output~is~supported.}
\AtBeginDocument{
  \sys_if_output_pdf:F {
    \msg_warning:nn {letterswitharrows} {pdf-only}
  }
}
\cs_new:Nn \__jmt_lwa_arrow_draw_special:nnn % length, font size, sign
{
  \sys_if_output_pdf:TF {
    \tex_special:D {pdf:~
      q~
      1~J~1~j~
      1~0~0~\dim_to_decimal:n{#3#2pt/10}~0~0~cm~
      .3~w~
      q~
      \dim_to_decimal:n{#3#2pt/10}~0~0~1~0~0~cm~
      1~0~0~1~-1~0~cm~
      0~1~m~
      .25~0~1~0~1~0~c~
      1~0~.25~0~0~-1~c~
      S~
      Q~
      0~0~m~
      -1~0~0~1~0~0~cm~
      \dim_to_decimal:n{#3#1}~0~l~S~
      Q
    }
  } {
    \tex_special:D {"~
      1~setlinecap~1~setlinejoin~
      1~0~0~\dim_to_decimal:n{#3#2pt/10}~0~0~6~array~astore~concat~
      .3~setlinewidth~
      gsave~
      \dim_to_decimal:n{#3#2pt/10}~0~0~1~0~0~6~array~astore~concat~
      1~0~0~1~-1~0~6~array~astore~concat~
      0~1~moveto~
      .25~0~1~0~1~0~curveto~
      1~0~.25~0~0~-1~curveto~
      stroke~
      grestore~
      0~0~moveto~
      -1~0~0~1~0~0~6~array~astore~concat~
      \dim_to_decimal:n{#3#1}~0~lineto~stroke
    }
  }
}


\cs_new:Nn \__jmt_lwa_arrow_draw_pgf:nnn {
  \begin{pgfpicture}
    \pgfsetlinewidth{#2pt/30}
    \pgfsetarrowsstart
      {Computer~Modern~Rightarrow[width=#2pt*2/10,length=#2pt/10,sharp]}
    % \pgfsetarrowsstart{\tl_use:N \g__jmt_lwa_pgf_arrow_style_tl}
    \pgfpathmoveto{\pgfpointorigin}
    \pgfpathlineto{\pgfpoint{-#3#1}{0cm}}
    \pgfusepath{stroke}
    \pgfresetboundingbox
  \end{pgfpicture}
}

\cs_new_eq:NN \__jmt_lwa_arrow_draw:nnn \use_none:nnn

\cs_new:Nn \__jmt_lwa_arrow_right:nn {
  \skip_horizontal:n {#1}
  % \rule[\dimexpr -#2pt/6\relax]{#1}{\dimexpr #2pt/3\relax}
  \__jmt_lwa_arrow_draw:nnn {#1} {#2} {}
}

\cs_new:Nn \__jmt_lwa_arrow_left:nn {
  \__jmt_lwa_arrow_draw:nnn {#1} {#2} {-}
  \skip_horizontal:n {#1}
  % \rule[\dimexpr -#2pt/6\relax]{#1}{\dimexpr #2pt/3\relax}
}

\cs_new:Npn \__jmt_lwa_arrow_overset_style:Nnncnnn #1#2#3#4#5#6#7 {
  \hbox_set:Nn \l_tmpa_box {$\m@th#1#3$}
  \dim_set:Nn \l_tmpa_dim {#2 pt/10}
  \vbox:n {
    \tex_lineskiplimit:D = \maxdimen
    \tex_baselineskip:D = 0pt
    \tex_lineskip:D = \dim_eval:n {\l_tmpa_dim * 3/2 + #7}
    \tex_halign:D { ## \tex_cr:D
      \skip_horizontal:n {\l_tmpa_dim / 2}
      $
        \m@th
        #1
        \tex_mskip:D \muskip_eval:n {#5}
        \use:c {#4} {\dim_eval:n{\box_wd:N \l_tmpa_box * #6}} {#2}
      $
      \tex_cr:D
      \box_use_drop:N \l_tmpa_box
      \tex_cr:D
    }
  }
}

\cs_new:Nn \__jmt_lwa_arrow_overset:nnnnn { % content, direction, xoffset, scale, yoffset
  \mathchoice {
    \__jmt_lwa_arrow_overset_style:Nnncnnn
      \displaystyle {\tf@size} {#1} {__jmt_lwa_arrow_#2:nn} {#3} {#4} {#5}
  } {
    \__jmt_lwa_arrow_overset_style:Nnncnnn
      \textstyle {\tf@size} {#1} {__jmt_lwa_arrow_#2:nn} {#3} {#4} {#5}
  } {
    \__jmt_lwa_arrow_overset_style:Nnncnnn
      \scriptstyle {\sf@size} {#1} {__jmt_lwa_arrow_#2:nn} {#3} {#4} {#5}
  } {
    \__jmt_lwa_arrow_overset_style:Nnncnnn
      \scriptscriptstyle {\ssf@size} {#1} {__jmt_lwa_arrow_#2:nn} {#3} {#4} {#5}
  }
}

\cs_new_protected:Npn \__jmt_lwa_arrow_overset:w {
  \c_group_begin_token
  \__jmt_lwa_arrow_overset_aux:w
}

\cs_new:Nn \__jmt_lwa_bool_convert:n {
    \IfBooleanTF {#1} {\c_true_bool} {\c_false_bool}
}

\exp_args:NNx \NewDocumentCommand \__jmt_lwa_arrow_overset_aux:w
  {s O{0mu} O{1} O{0ex} m t' e{\char_generate:nn {95}{8}} t'} {
  \__jmt_lwa_arrow_overset:nnnnn
    {
      #5
      \exp_args:Nf\bool_if:nT{\__jmt_lwa_bool_convert:n{#6} || \__jmt_lwa_bool_convert:n{#8}} {
          \c_math_superscript_token {
            \scriptscriptstyle\IfBooleanT{#6}{\prime}\IfBooleanT{#8}{\prime}
        }
      } % TODO: Better positioning etc?
      \exp_args:Nf\IfValueT{\use:n#7} {
        \c_math_subscript_token {
          \mathrlap{#7}
        }
      }
    }
    {\IfBooleanTF{#1}{left}{right}}
    {#2} {#3} {#4}

  \exp_args:Nf\IfValueTF{\use:n#7}{
    % TODO: Better way to do this? This is all kinds of wrong.
    \hphantom{\c_math_subscript_token{#7}}
  } {}
  \c_group_end_token
}
\cs_set_eq:NN \arrowoverset \__jmt_lwa_arrow_overset:w
\AtBeginDocument{
  \@ifpackageloaded{hyperref}{
    \pdfstringdefDisableCommands{
      % Why does this only work with Expandable?
      \DeclareExpandableDocumentCommand \__jmt_lwa_arrow_overset:w {s o o o m} {
      \ifpdfstringunicode
        {#5 \IfBooleanTF{#1}{\unichar{"20D6}}{\unichar{"20D7}}}
        {#5}
      }
    }
  }{}
}

\bool_new:N \g__jmt_lwa_tweak_shortcuts_bool
\prop_new:N \g__jmt_lwa_selected_presets_prop
\keys_define:nn {letterswitharrows} {
  mode .choice:,
  mode / special .code:n = {
    \cs_set_eq:NN \__jmt_lwa_arrow_draw:nnn \__jmt_lwa_arrow_draw_special:nnn
  },
  mode / pgf .code:n = {
    \RequirePackage{pgf}
    \ExplSyntaxOff\usepgflibrary{arrows.meta}\ExplSyntaxOn
    \cs_set_eq:NN \__jmt_lwa_arrow_draw:nnn \__jmt_lwa_arrow_draw_pgf:nnn
  },
  mode .initial:n = {special},
  pgf .meta:n = {mode = pgf},
  presets .multichoices:nn = {abc, ABC, cAcBcC, vec-cev} {
    \int_compare:nNnTF \l_keys_choice_int = 1 {
      \prop_gclear:N \g__jmt_lwa_selected_presets_prop
    } {}
    \prop_gput:NVn \g__jmt_lwa_selected_presets_prop \l_keys_choice_tl {}
  },
  presets .initial:n = {abc, ABC, cAcBcC},
  tweaks .bool_set:N = \g__jmt_lwa_tweak_shortcuts_bool,
  tweaks .initial:n = {true},
}
\ProcessKeysPackageOptions{letterswitharrows}

\prop_if_in:NnTF \g__jmt_lwa_selected_presets_prop {abc} {
  \int_step_inline:nnn {1} {26} {
    \int_compare:nNnTF {#1} = {22} {
      \cs_new:cpx {vright} {
        \exp_not:N\__jmt_lwa_arrow_overset:w{v}
      }
      \cs_new:cpx {vleft} {
        \exp_not:N\__jmt_lwa_arrow_overset:w*{v}
      }
    } {
      \cs_new:cpx {v\int_to_alph:n{#1}} {
        \exp_not:N\__jmt_lwa_arrow_overset:w{\int_to_alph:n{#1}}
      }
      \cs_new:cpx {\int_to_alph:n{#1}v} {
        \exp_not:N\__jmt_lwa_arrow_overset:w*{\int_to_alph:n{#1}}
      }
    }
  }
} {}
\prop_if_in:NnTF \g__jmt_lwa_selected_presets_prop {ABC} {
  \int_step_inline:nnn {1} {26} {
    \cs_new:cpx {v\int_to_Alph:n{#1}} {
      \exp_not:N\__jmt_lwa_arrow_overset:w{\int_to_Alph:n{#1}}
    }
    \cs_new:cpx {\int_to_Alph:n{#1}v} {
      \exp_not:N\__jmt_lwa_arrow_overset:w*{\int_to_Alph:n{#1}}
    }
  }
} {}
\prop_if_in:NnTF \g__jmt_lwa_selected_presets_prop {cAcBcC} {
  \int_step_inline:nnn {1} {26} {
    \cs_new:cpx {vc\int_to_Alph:n{#1}} {
      \exp_not:N\__jmt_lwa_arrow_overset:w{\exp_not:N\mathcal{\int_to_Alph:n{#1}}}
    }
    \cs_new:cpx {c\int_to_Alph:n{#1}v} {
      \exp_not:N\__jmt_lwa_arrow_overset:w*{\exp_not:N\mathcal{\int_to_Alph:n{#1}}}
    }
  }
} {}
\prop_if_in:NnTF \g__jmt_lwa_selected_presets_prop {vec-cev} {
  \RenewDocumentCommand \vec {m} {
    \__jmt_lwa_arrow_overset:w {#1} \scan_stop:
  }
  \DeclareDocumentCommand \cev {m} {
    \__jmt_lwa_arrow_overset:w* {#1} \scan_stop:
  }
} {}
\bool_if:NTF \g__jmt_lwa_tweak_shortcuts_bool {
    \prop_if_in:NnTF \g__jmt_lwa_selected_presets_prop {ABC} {
        \int_step_inline:nnn {1} {26} {
            \cs_set:cpx {v\int_to_Alph:n{#1}} {
                \exp_not:N\__jmt_lwa_arrow_overset:w[2.5mu][8/10]{\int_to_Alph:n{#1}}
            }
            \cs_set:cpx {\int_to_Alph:n{#1}v} {
                \exp_not:N\__jmt_lwa_arrow_overset:w*[2.5mu][7/10]{\int_to_Alph:n{#1}}
            }
        }
        \cs_set:cpn {vS} {
            \__jmt_lwa_arrow_overset:w[3mu][7/10]{S}
        }
        \cs_set:cpn {vT} {
            \__jmt_lwa_arrow_overset:w[2mu][8/10]{T}
        }
        \cs_set:cpn {Tv} {
            \__jmt_lwa_arrow_overset:w*[1mu][8/10]{T}
        }
        \cs_set:cpn {vU} {
            \__jmt_lwa_arrow_overset:w[2mu][7/10]{U}
        }
        \cs_set:cpn {Uv} {
            \__jmt_lwa_arrow_overset:w*[2mu][7/10]{U}
        }
        \cs_set:cpn {vV} {
            \__jmt_lwa_arrow_overset:w[2.5mu][7/10]{V}
        }
        \cs_set:cpn {Vv} {
            \__jmt_lwa_arrow_overset:w*[2mu][7/10]{V}
        }
        \cs_set:cpn {vX} {
            \__jmt_lwa_arrow_overset:w[3mu][7/10]{X}
        }
        \cs_set:cpn {vY} {
            \__jmt_lwa_arrow_overset:w[2mu][8/10]{Y}
        }
        \cs_set:cpn {Yv} {
            \__jmt_lwa_arrow_overset:w*[2mu][7/10]{Y}
        }
    } {}
    \prop_if_in:NnTF \g__jmt_lwa_selected_presets_prop {cAcBcC} {
        \int_step_inline:nnn {1} {26} {
            \cs_set:cpx {vc\int_to_Alph:n{#1}} {
                \exp_not:N\__jmt_lwa_arrow_overset:w[1mu][9/10]{\exp_not:N\mathcal{\int_to_Alph:n{#1}}}
            }
            \cs_set:cpx {c\int_to_Alph:n{#1}v} {
                \exp_not:N\__jmt_lwa_arrow_overset:w*[1.5mu][8/10]{\exp_not:N\mathcal{\int_to_Alph:n{#1}}}
            }
        }
    } {}
    \prop_if_in:NnTF \g__jmt_lwa_selected_presets_prop {abc} {
        \cs_new:cpn {vell} {
            \__jmt_lwa_arrow_overset:w{\ell}
        }
        \cs_new:cpn {ellv} {
            \__jmt_lwa_arrow_overset:w{\ell}
        }
    } {}
} {}

\endinput
%%
%% End of file `letterswitharrows.sty'.
