2

I do not know how to search for this issue as backwards slash or \ in questions has not gleaned anything useful. How can I set a key-value and then use a font size value without having to use a command in the package options please, e.g.

\usepackage[superscript-fontsize=large]{aminosymb}

instead of

\usepackage[superscript-fontsize=\large]{aminosymb}

I would prefer for the package I am writing to not have to put the backwards slash in the package options although this is always a backup. For instance, my MWE produces this:

enter image description here

using this command:

\NewDocumentCommand { \aasp } { m m }
  {
    \residueString:nn { #1 } { \l_aminosymb_residue_style_tl }
    \textsuperscript{$\l_superscript_font_size_tl\mkern-1mu#2\mkern-1.5mu$}
  }

I do not know if there is a LaTeX3 command to convert tokens into a command, does anyone know how this can be done please? MWE:

\begin{filecontents}[overwrite]{aminosymb.sty}
\RequirePackage{l3keys2e}
\ProvidesExplPackage
  {aminosymb}
  {today}
  {version}
  {Typesetting amino acid residues with notation and numbers}

\ExplSyntaxOn

\tl_new:N \l_aminosymb_residue_style_tl \tl_new:N \l_superscript_font_size_tl

\keys_define:nn { AminoSymb / Package } { style .tl_set:N = \l_aminosymb_residue_style_tl, style .initial:n = abbrev-firstcap, superscript-fontsize .tl_set:N = \l_superscript_font_size_tl, superscript-fontsize .initial:n = scriptstyle, }

\ProcessKeysOptions { AminoSymb / Package }

\prop_new:N \l_abbrev_ala_prop \prop_new:N \l_abbrev_all_prop

\prop_set_from_keyval:Nn \l_abbrev_ala_prop { initial-lower=a, abbrev-firstcap = Ala, }

\prop_set_from_keyval:Nn \l_abbrev_all_prop { ala = \l_abbrev_ala_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 { \aasp } { m m } { \residueString:nn { #1 } { \l_aminosymb_residue_style_tl } \textsuperscript{$\l_superscript_font_size_tl\mkern-1mu#2\mkern-1.5mu$} }

\ExplSyntaxOff \end{filecontents}

\documentclass{article}

\usepackage[superscript-fontsize=large]{aminosymb}

\begin{document}

\aasp{a}{323}

\end{document}

Apologies if this is a duplicate, I do not know how to search for this issue due to the nature of the \ command.

JamesT
  • 3,169
  • 3
    Consider not using package options at all, but instead a setup-macro that can be used after the package is loaded. – Skillmon Oct 11 '23 at 05:41
  • 1
    Please note that you're violating the naming conventions of expl3. See my edited answer for a version that fixes it. All your functions should contain the module name, same is true for all your variables. – Skillmon Oct 11 '23 at 05:54
  • @Skillmon I appreciate the feedback, LaTeX3 is some learning curve but will be worth it, thanks for the help! Will fix it in my main document now – JamesT Oct 11 '23 at 05:56
  • 1
    In summary, a variable should be called \<scope>_<module>_<description>_<type> or if it's private \<scope>__<module>_<description>_<type>. And a function should always be called \<module>_<description>:<args> or if it's private \__<module>_<description>:<args>. – Skillmon Oct 11 '23 at 05:56

1 Answers1

4

You can use \cs_if_exist_use:c, if you want to throw an error if it's not available you can as well use \cs_if_exist_use:cF.

The following silently ignores an unknown command. Also I fixed the way you set the font command (has to be used outside of the maths context). And I also put \exp_stop_f: after your \mkern so that they don't affect #2.

Additionally I removed the hard dependency on l3keys2e, as you can use the built-in \ProcessKeyOptions in recent kernels, and only need l3keys2e for older LaTeX versions.

\begin{filecontents}[overwrite]{aminosymb.sty}
\ProvidesExplPackage
  {aminosymb}
  {today}
  {version}
  {Typesetting amino acid residues with notation and numbers}

\ExplSyntaxOn

\tl_new:N \l_aminosymb_residue_style_tl \tl_new:N \l_aminosymb_superscript_font_size_tl

\keys_define:nn { AminoSymb / Package } { style .tl_set:N = \l_aminosymb_residue_style_tl, style .initial:n = abbrev-firstcap, superscript-fontsize .tl_set:N = \l_aminosymb_superscript_font_size_tl, superscript-fontsize .initial:n = scriptstyle, }

\cs_if_exist:NTF \ProcessKeyOptions { \ProcessKeyOptions [ AminoSymb / Package ] } { \RequirePackage{l3keys2e} \ProcessKeysOptions { AminoSymb / Package } }

\prop_new:N \l_aminosymb_abbrev_ala_prop \prop_new:N \l_aminosymb_abbrev_all_prop

\prop_set_from_keyval:Nn \l_aminosymb_abbrev_ala_prop { initial-lower=a, abbrev-firstcap = Ala, }

\prop_set_from_keyval:Nn \l_aminosymb_abbrev_all_prop { ala = \l_aminosymb_abbrev_ala_prop, }

\cs_new:Npn \aminosymb_residueString:nn #1#2 { \tl_set:Nx\l_aminosymb_residue_tl{#1} \tl_set:Nx\l_aminosymb_style_tl{#2} \prop_map_function:NN \l_aminosymb_abbrev_all_prop __aminosymb_abbrev_aux:nn }

\cs_generate_variant:Nn\prop_item:Nn{Ne}

\cs_new:Npn __aminosymb_abbrev_aux:nn #1#2 { \prop_map_inline:Nn #2 { \tl_if_eq:NnT \l_aminosymb_residue_tl {##2} {\prop_item:Ne#2{\l_aminosymb_style_tl}} } }

\NewDocumentCommand { \aasp } { m m } { \aminosymb_residueString:nn { #1 } { \l_aminosymb_residue_style_tl } \textsuperscript { \cs_if_exist_use:c { \l_aminosymb_superscript_font_size_tl } $ \mkern-1mu\exp_stop_f: #2 \mkern-1.5mu\exp_stop_f: $ } }

\ExplSyntaxOff \end{filecontents}

\documentclass{article}

\usepackage[superscript-fontsize=large]{aminosymb}

\begin{document}

\aasp{a}{323}

\end{document}

Skillmon
  • 60,462