General\ref with macro as parameter

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
stridder
Posts: 6
Joined: Sun Mar 13, 2016 9:43 am

\ref with macro as parameter

Post by stridder »

Hello,
I would like to write a macro (using Latex3/expl3) that can automatically refer to say plot using several key-value parameters. Here is what I currently have:

Code: Select all

\keys_define:nn{label}{
	Re .tl_set:N = \l_label_Re_tl, 
	DOF .tl_set:N = \l_label_DOF_tl,
	statsName .tl_set:N = \l_label_statsName_tl,
}


\NewDocumentCommand{\getProjOneLabel}{m}{
	\keys_set:nn{label}{#1}
	\newcommand{\Stat}{\StrLeft{\l_label_statsName_tl}{3}} %1st 3 letters of
	fig:{\Stat}Re\l_label_Re_tl DOF\l_label_DOF_tl
}

when I test it with:

Code: Select all

label=\getProjOneLabel{statsName=Strouhal,DOF=4M, Re=50}
It shows:
label=fig:StrRe50DOF4M
which is correct. But when I use it for real referencing like this:

Code: Select all

\ref{\getProjOneLabel{statsName=Strouhal,DOF=4M, Re=50}}
instead of reference in the output this shown:
??fig:StrRe50DOF4M

I think this has smth to do with order of expansion, but I don't understand expansion process fully yet. Tried putting \expandafter many times, still fails. Could smbd please explain step by step what goes wrong here?
When I hardcode label as

Code: Select all

\ref{fig:StrRe50DOF4M}
it does work.

The goal is to write a macro say '\plotref' that will execute

Code: Select all

\ref{\getProjOneLabel{statsName=Strouhal,DOF=4M, Re=50}}
where all RHS values will be passed as key-valued parameters.
I am new to Latex3 and even to regular Latex/Tex, the more details the better.
Thank you very much.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

\ref with macro as parameter

Post by cgnieder »

Your macro is not expandable. Any time you make assignments like setting options or defining macros you have unexpandable operations. In \label and \ref I'd make sure only to use expandable tokens.

Unfortunately you're example was far from complete, so…

Code: Select all

\documentclass{article}
\usepackage{expl3,xparse}

\ExplSyntaxOn
\tl_new:N \l_my_label_Re_tl
\tl_new:N \l_my_label_DOF_tl
\tl_new:N \l_my_label_statsName_tl
\tl_new:N \l_my_label_stat_tl

\keys_define:nn {label}
  {
    Re        .tl_set:N = \l_my_label_Re_tl ,
    DOF       .tl_set:N = \l_my_label_DOF_tl ,
    statsName .tl_set:N = \l_my_label_statsName_tl
  }

\cs_new_protected:Npn \my_set_project_one_label:n #1
  {
    \group_begin:
      \keys_set:nn {label} {#1}
      \tl_set:Nx \l_my_label_stat_tl % this is *not* expandable!
        {
          \tl_item:Nn \l_my_label_statsName_tl {1}
          \tl_item:Nn \l_my_label_statsName_tl {2}
          \tl_item:Nn \l_my_label_statsName_tl {3}
        }
       \label {
         fig : \tl_use:N \l_my_label_stat_tl
         Re \tl_use:N \l_my_label_Re_tl
         DOF \tl_use:N \l_my_label_DOF_tl
       }
    \group_end:
  }

\cs_new_protected:Npn \my_ref_project_one_label:n #1
  {
    \group_begin:
      \keys_set:nn {label} {#1} % this is *not* expandable!
      \tl_set:Nx \l_my_label_stat_tl % this is *not* expandable!
        {
          \tl_item:Nn \l_my_label_statsName_tl {1}
          \tl_item:Nn \l_my_label_statsName_tl {2}
          \tl_item:Nn \l_my_label_statsName_tl {3}
        }
       \ref {
         fig : \tl_use:N \l_my_label_stat_tl
         Re \tl_use:N \l_my_label_Re_tl
         DOF \tl_use:N \l_my_label_DOF_tl
       }
    \group_end:
  }

\NewDocumentCommand {\setProjOneLabel} {m}
  { \my_set_project_one_label:n {#1} }

\NewDocumentCommand {\refProjOneLabel} {m}
  { \my_ref_project_one_label:n {#1} }

\begin{document}

\setProjOneLabel{statsName=Strouhal,DOF=4M, Re=50}

\refProjOneLabel{statsName=Strouhal,DOF=4M, Re=50}

\end{document}
Regards
site moderator & package author
Post Reply