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}
\addtoreflist{section}
\makeatletter
\newcounter{actual@counter}% Additional chapter counter
% 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.
expandafterto make sure the macro inside thecsnameexpands before thecsnameis created. If that doesn't work, could you add a full working example? – Roelof Spijker Nov 06 '11 at 20:20\getreftypeis not expandable, that is, it produces the necessary for typesetting the ref type, but not exactly the string (saychapter). However,\getreftypedoesn't seem to be defined by smartref. – egreg Nov 06 '11 at 20:23\csname sget \getreftype{#1} val\endcsname{\target@counter}{#1}is meaningless and\target@counteris unknown. – egreg Nov 06 '11 at 21:15\csnamewill expand its content fully, there is no\expandafterrequired. Only if the content doesn't expand finally to a text an error is raised. – Martin Scharrer Nov 06 '11 at 21:37