TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 ;)



\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
        \csname r@#2\endcsname{}{}{}{}\@nil#1\@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"

% \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.
\def\@@@getautorefname#1{% = \HyPsd@@autorefname without \space

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

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


  % here, \csOne should contain "1" and \csTwo should contain "2"

(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.

share|improve this question
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
up vote 1 down vote accepted

If the packages are loaded in the following order


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

  \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:


without any other code.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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