3

I have defined the macro \getdocvar that retrieves a value from a property list given it is key. It has an optional argument which is a list of key-values. At the moment only the transform key is defined. I want to store in this key a macro to be applied to the value retrieved by \getdocvar.

   \keys_define:nn { docvar/get }
   {
     transform .tl_set:N = \l_docvar_tranformation_tl,
   }

The name of the macro is stored as a token list.

   \cs_new_protected:Npn \docvar_get_properties:n #1
   {
     \group_begin:
     \keys_set:nn { docvar/get } { #1 }
     \group_end:
     }

Now, if \getdocvar has no optional argument, the value of the prop is retrieved, but I have no idea how to apply a macro to #2 based on the name.

\DeclareDocumentCommand \getdocvar {o m}{
  \IfNoValueTF { #1 }
  {
    \prop_get:NVN \g_docvar_values_prop #2 \g_tmpa_tl
    \tl_use:N \g_tmpa_tl
  }
  {
    \docvar_get_properties:n {#1}
    %%% MISSIn CODE to cronstruct a macro from transform key
  }

Any hit to how this may be done.

The full sequence of using this macro is

\definedocvar{title}
\setdocvar{title}{This is my title}
\getdocvar{title} 
% This should print:
This is my title
\getdocvar[transform=MakeUppercase]{title}
% This should print:
\MakeUppercase{This is my title}
  • How is \getdocvar supposed to be called? – egreg Mar 31 '17 at 10:25
  • @egreg I added an example of use. – TeXtnik Mar 31 '17 at 12:18
  • The edit should be an altogether different question. – egreg Apr 3 '17 at 15:37
  • @egreg I put the edit in a new question tex.stackexchange.com/q/361976/2483 – TeXtnik Apr 3 '17 at 20:11
5

If I understand correctly your aim, this might do:

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn

\prop_new:N \g_docvar_values_prop
\tl_new:N \l__docvar_temp_tl

\NewDocumentCommand{\setdocvar}{mm}
 {
  \prop_gput:Nnn \g_docvar_values_prop { #1 } { #2 }
 }

\NewDocumentCommand{\getdocvar}{O{}m}
 {
  \group_begin:
  \keys_set:nn { docvar/get } { #1 }
  \prop_get:NnN \g_docvar_values_prop { #2 } \l__docvar_temp_tl
  \__docvar_transform:VV \l_docvar_transform_tl \l__docvar_temp_tl
  \group_end:
 }

\cs_new_protected:Nn \__docvar_transform:nn
 {
  #1{#2}
 }
\cs_generate_variant:Nn \__docvar_transform:nn { VV }

\keys_define:nn { docvar/get }
 {
  transform .tl_set:N = \l_docvar_transform_tl,
  transform .initial:n = \use:n,
 }

\ExplSyntaxOff

\setdocvar{title}{This is my title}

\begin{document}

\getdocvar{title} 

\getdocvar[transform=\MakeUppercase]{title}

\end{document}

The value to transform should be a single argument macro.

enter image description here

The following is easier, though:

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn

\prop_new:N \g_docvar_values_prop
\tl_new:N \l__docvar_temp_tl

\NewDocumentCommand{\setdocvar}{mm}
 {
  \prop_gput:Nnn \g_docvar_values_prop { #1 } { #2 }
 }

\NewDocumentCommand{\getdocvar}{O{\use:n}m}
 {
  \prop_get:NnN \g_docvar_values_prop { #2 } \l__docvar_temp_tl
  \exp_args:NV #1 \l__docvar_temp_tl
 }

\ExplSyntaxOff

\setdocvar{title}{This is my title}

\begin{document}

\getdocvar{title}

\getdocvar[\MakeUppercase]{title}

\end{document}
  • Thanks for your complete example. I only expected something about how to write the \__docvar_transform:nn function. Unfortunately, when I have adapted your solution to my existing code; I couldn't make it work. I have edited the question to have all the code. – TeXtnik Apr 3 '17 at 15:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.