# Call macro whose name depends on macro

Sorry about the title, I really couldn't find a better one.

I'm facing the following dilemma: I'm using the smartref package, in particular I'm using its \sgetXval macros. The thing is that I'm using the code kindly provided in this previous question of mine to determine whether a label points to (say) a section or a chapter, and I'd like to call either \sgetsectionval or \sgetchapterval accordingly.

I've tried with:

\csname sget\getreftype{#1}val\endcsname{\target@counter}{#1}


but I get Missing \endcsname inserted errors sistematically.

Bottom line: I'd like a way of constructing a macro name from an arbitrary expression and then call said constructed macro name.

I could post more code, but it's gonna get messy and I think this should suffice to convey the question proper, do let me know otherwise :)

Edit: MNWE ;)

\documentclass{article}
\usepackage{hyperref}
\usepackage{smartref}

\makeatletter

% Helper macro to extract the type (section,subsection...) or the type name
% out of the label reference. Works with hyperref only.
% Argument #1 is a macro of form \def\...#1...\@nil{...}
% Argument #2 is the label reference, e.g. "sect:test"
\newcommand*\@getautoref[2]{% \HyPsd@@@autoref from hyperref, modified
\expandafter\ifx\csname r@#2\endcsname\relax
??%
\else
\expandafter\expandafter\expandafter\@@getautoref
\csname r@#2\endcsname{}{}{}{}\@nil#1\@nil
\fi
}
\def\@@getautoref#1#2#3#4#5\@nil#6\@nil{%
#6#4.\@nil}% Argument #4 = type and number, e.g. "section.1" or "subsection.1.2"

% \getreftype results in the type name, e.g. "section" or "figure".
% It remove a star, if existent, i.e. "section*" will become "section"
\newcommand\getreftype{%
\@getautoref\@@getreftype}
\def\@getreftype#1.#2\@nil{#1}
\def\@@getreftype#1.#2\@nil{\@@@getreftype#1*\@nil}
\def\@@@getreftype#1*#2\@nil{#1}

% \getautorefname results in the type prose name (plus space character),
% e.g. "section" in English or "Abschnitt" in German
% (like \autoref, but without number).
% Since the \space is hard coded inside \HyPsd@@autorefname we use our
% own version called \@getautorefname instead.
% It will work with labels to \section* etc., too.
\newcommand*\getautorefname{%
\@getautoref\@@getautorefname}
\def\@getautorefname#1.#2\@nil{\@@@getautorefname{#1}}
\def\@@getautorefname#1.#2\@nil{%
\expandafter\@@@getautorefname\expandafter{\@@@getreftype#1*\@nil}}
\def\@@@getautorefname#1{% = \HyPsd@@autorefname without \space
\ltx@IfUndefined{#1autorefname}%
{\ltx@IfUndefined{#1name}{}{\csname#1name\endcsname}}%
{\csname#1autorefname\endcsname}}
\makeatother

\newcommand{\getrefval}[2]{%
\csname sget\getreftype{#1}val\endcsname{#2}{#1}% Retrieve chapter counter from reference
}

\begin{document}
\section{An example section\label{sec:anExampleSection}}
\section{Another example section\label{sec:anotherExampleSection}}

\getrefval{sec:anExampleSection}{\csOne}
\getrefval{sec:anotherExampleSection}{\csTwo}

% here, \csOne should contain "1" and \csTwo should contain "2"
\end{document}


(this is a somewhat modified version of the code in the linked question, I didn't need the extra flexibility of having a starred and non-starred version of the macros defined therein, so I just deleted the corresponding code).

I get the error when trying to use \getrefval.

-
You probably just need an expandafter to make sure the macro inside the csname expands before the csname is created. If that doesn't work, could you add a full working example? – Roelof Spijker Nov 6 '11 at 20:20
A MWE (well, non working) example would be better; but it sounds like \getreftype is not expandable, that is, it produces the necessary for typesetting the ref type, but not exactly the string (say chapter). However, \getreftype doesn't seem to be defined by smartref. – egreg Nov 6 '11 at 20:23
@egreg: Added MNWE :) – mpr Nov 6 '11 at 21:07
It's not complete: \csname sget \getreftype{#1} val\endcsname{\target@counter}{#1} is meaningless and \target@counter is unknown. – egreg Nov 6 '11 at 21:15
@egreg: very sorry about that, too much cut & paste and too little sleep. Corrected (I hope). – mpr Nov 6 '11 at 21:22

If the packages are loaded in the following order

\usepackage{hyperref}
\usepackage{smartref}


then the code does what's supposed to do. However the definition should be

\newcommand{\getrefval}[2]{%
\csname sget\getreftype{#1}val\endcsname{#2}{#1}% Retrieve chapter counter from reference
}


without the spaces you were putting.

Moreover, \addtoreflist{section} has to be issued.

However, the same result can be obtained by loading the refcount package:

\usepackage{refcount}
\newcommand{\getrefval}[2]{%
\edef#2{\getrefnumber{#1}}}


without any other code.

-