3

I am writing a LaTeX package to pretty-print amino acid residues and numbers with optional symbols. I have created a lua programme that outputs according to a customisable format, \StyleOption{}, using a lua table (the 'sl', 'as' meaning short lower, abbreviation sentence-case etc.) that works as intended (there are many more residues like ala_abbrev, tyr_abbrev etc but they were pruned for this question):

enter image description here

\documentclass{article}

\usepackage{luacode}

\begin{luacode*}
his_abbrev = {['sl'] = "h", ['su'] = "H", ['al'] = "his", ['as'] = "His", ['au'] = "HIS"}
ile_abbrev = {['sl'] = "i", ['su'] = "I", ['al'] = "ile", ['as'] = "Ile", ['au'] = "ILE"}
lys_abbrev = {['sl'] = "k", ['su'] = "K", ['al'] = "lys", ['as'] = "Lys", ['au'] = "LYS"}

amino_acid_names = {
    his_abbrev, ile_abbrev, lys_abbrev,
}

function amino_acid_name(residue, style)
  for _, table in ipairs (amino_acid_names) do
    for key, val in pairs (table) do
      if tostring (val) == residue then
        tex.print (table[style])
      end
    end
  end
end
\end{luacode*}

\newcommand\StyleOption{as}

\newcommand\residueString[2]{\directlua{
  amino_acid_name ("\luaescapestring{#1}", "\luaescapestring{#2}")}}

\NewDocumentCommand{\aail}{o m m}{%
\IfValueTF{#1}{%
  \residueString{#2}{\StyleOption}%
    \textsuperscript{$\scriptstyle\mkern-1mu#1\mkern-1.5mu$} $\mkern-2mu#3$%
  }{%
    \residueString{#2}{\StyleOption} $\mkern1.5mu#3$%
  }%
}

\begin{document}

\aail{i}{434}

\aail[*]{lys}{112}

\aail{HIS}{88}

\end{document}

My issue arises from trying to convert the above into LaTeX3 format. I use LuaLaTeX extensively so I am happy for the package to need lua but others might not. I have went through How to define/iterate over nested property lists in LaTeX3 but I am not sure how to iterate over a prop containing props (\l_abbrev_all_prop) and whilst the LaTeX3 Interfaces documentation probably provides the solution, it is intimidating especially when you don't know what to search for.

Question: How can I get the following LaTeX3 code to print \ResidueString according to the key defined in \StyleOption similar to the lua code please?

\documentclass{article}

\ExplSyntaxOn

\NewDocumentCommand{\StyleOption}{}{as} % for the key to \l_abbrev_his_prop etc

\prop_new:N \l_abbrev_his_prop
\prop_new:N \l_abbrev_ile_prop
\prop_new:N \l_abbrev_lys_prop
\prop_new:N \l_abbrev_all_prop

% to save space in Q, just putting them all on one line
\prop_set_from_keyval:Nn \l_abbrev_his_prop {sl = h, su = H, al = his, as = His, au = HIS}
\prop_set_from_keyval:Nn \l_abbrev_ile_prop {sl = i, su = I, al = ile, as = Ile, au = ILE}
\prop_set_from_keyval:Nn \l_abbrev_lys_prop {sl = k, su = K, al = lys, as = Lys, au = LYS}
\prop_set_from_keyval:Nn \l_abbrev_all_prop
    {
    his = \l_abbrev_his_prop,
    ile = \l_abbrev_ile_prop,
    lys = \l_abbrev_lys_prop
    }

% \cs_new:Npn \residueString:nn #1#2
%     {
%         \prop_map_function:NN \l_abbrev_all_prop #1#2 % What goes here?
%     }

\NewDocumentCommand { \aail } { o m m }
    {
    \IfValueTF{#1}{%
    \residueString{ #2 }{ \StyleOption }%
        \textsuperscript{$\scriptstyle\mkern-1mu#1\mkern-1.5mu$} $\mkern-2mu#3$%
    }{%
        \residueString{ #2 }{ \StyleOption } $\mkern1.5mu#3$%
    }%
    }

\ExplSyntaxOff

\begin{document}

\aail{i}{434}

\aail[*]{lys}{112}

\aail{HIS}{88}

\end{document}

Note: this will all be customisable including adding custom residues, kern distances, sub/super-script, float caption options set in package options/\SetResidueOptions{key=val} etc but this is just a minimal example.

1 Answer 1

2

enter image description here

A fairly literal translation of the Lua:

\documentclass{article}

\ExplSyntaxOn

\NewExpandableDocumentCommand{\StyleOption}{}{as} % for the key to \l_abbrev_his_prop etc

\prop_new:N \l_abbrev_his_prop
\prop_new:N \l_abbrev_ile_prop
\prop_new:N \l_abbrev_lys_prop
\prop_new:N \l_abbrev_all_prop

% to save space in Q, just putting them all on one line
\prop_set_from_keyval:Nn \l_abbrev_his_prop {sl = h, su = H, al = his, as = His, au = HIS}
\prop_set_from_keyval:Nn \l_abbrev_ile_prop {sl = i, su = I, al = ile, as = Ile, au = ILE}
\prop_set_from_keyval:Nn \l_abbrev_lys_prop {sl = k, su = K, al = lys, as = Lys, au = LYS}
\prop_set_from_keyval:Nn \l_abbrev_all_prop
    {
    his = \l_abbrev_his_prop,
    ile = \l_abbrev_ile_prop,
    lys = \l_abbrev_lys_prop
    }

 \cs_new:Npn \residueString:nn #1#2
     {
        \tl_set:Nx\l_residue{#1}
        \tl_set:Nx\l_style{#2}
         \prop_map_function:NN \l_abbrev_all_prop \__abbrev_aux:nn
     }

\cs_generate_variant:Nn\prop_item:Nn{Ne}
   
\cs_new:Npn \__abbrev_aux:nn #1#2
     {
         \prop_map_inline:Nn #2 {
    \tl_if_eq:NnT 
      \l_residue
       {##2}
       {\prop_item:Ne#2{\l_style}}
     }
}

   


   

\NewDocumentCommand { \aail } { o m m }
    {
    \IfValueTF{#1}{%
    \residueString:nn{ #2 }{ \StyleOption }%
        \textsuperscript{$\scriptstyle\mkern-1mu#1\mkern-1.5mu$}~  $\mkern-2mu#3$%
    }{%
        \residueString:nn{ #2 }{ \StyleOption } ~ $\mkern1.5mu#3$%
    }%
    }

\ExplSyntaxOff

\begin{document}

\aail{i}{434}

\aail[*]{lys}{112}

\aail{HIS}{88}

\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .