I would like to define a command \mycommand such that \mycommand{X} produces
\Xfirst\Xsecond\label{Xlabel}, where \Xfirst and \Xsecond are already defined commands. Of course, \mycommand{Y} should produce \Yfirst\Ysecond\label{Ylabel}, and \mycommand{Z} should produce what you expected, and so on. For instance, if I have
\newcommand{\Xfirst}{A}
\newcommand{\Xsecond}{xx}
\newcommand{\Yfirst}{B}
\newcommand{\Ysecond}{yy}
then using the command \mycommand{X} must be equivalent to writing Axx\label{Xlabel}, while writing \mycommand{Y} must be equivalent to writing Byy\label{Ylabel}. I think I can do it with (a lot of) ifs. Is there a more elegant form? How? Package hyperref should work well with the generated label.
An example following the suggestion of Ryan Reich (wrong link with hyperref).
\documentclass{article}
\usepackage{hyperref}
\newcommand{\Xfirst}{A}
\newcommand{\Xsecond}{xx}
\newcommand{\Yfirst}{B}
\newcommand{\Ysecond}{yy}
\newcommand\mycommand[1]{%
\csname #1first\endcsname
\csname #1second\endcsname
\label{#1label}%
}
\begin{document}
\section{Section}
\mycommand{X}
\pageref{Ylabel} % does not point to page 3
\newpage
Nothing.
\newpage
\mycommand{Y}
\pageref{Xlabel}
\end{document}
Another attempt, using hypertarget and hyperlink. It seems to work.
\documentclass{article}
\usepackage{hyperref}
\newcommand{\Xfirst}{A}
\newcommand{\Xsecond}{xx}
\newcommand{\Yfirst}{B}
\newcommand{\Ysecond}{yy}
\newcommand\mycommand[1]{%
\csname #1first\endcsname
\csname #1second\endcsname
\hypertarget{#1label}{}%
}
\begin{document}
\section{Section}
\label{section1}
\mycommand{X}
\hyperlink{Ylabel}{l1Y}
\newpage
\section{Another section}
\label{section2}
\hyperlink{Xlabel}{l2X}
\hyperlink{Ylabel}{l2Y}
\newpage
\mycommand{Y}
\hyperlink{Xlabel}{l3X}
See Section 1 here~\ref{section1}.
See Section 2 here~\ref{section2}.
\end{document}
A third attempt, with \phantomsection. It also seems to work. This is the better solution.
\documentclass{article}
\usepackage{hyperref}
\newcommand{\Xfirst}{A}
\newcommand{\Xsecond}{xx}
\newcommand{\Yfirst}{B}
\newcommand{\Ysecond}{yy}
\newcommand\mycommand[1]{%
\csname #1first\endcsname
\csname #1second\endcsname
\phantomsection
\label{#1label}{}%
}
\begin{document}
\Huge
\section{Section}
\label{section1}
\mycommand{X}
X\pageref{Xlabel}
Y\pageref{Ylabel}
\newpage
\section{Another section}
\label{section2}
X\pageref{Xlabel}
Y\pageref{Ylabel}
\newpage
\mycommand{Y}
X\pageref{Xlabel}
Y\pageref{Ylabel}\par
See Section 1 here~\ref{section1}.\par
See Section 2 here~\ref{section2}.
\end{document}

\hypertargetis much worse than my proposal with\phantomsection, since it does not automatically print either the section number or the page number. What are you actually trying to do? – Ryan Reich Jan 17 '12 at 16:14\phanthomsectionwould not work together with\ref, so I tried another solution usinghypertarget. That solution works, although it requieres some fine tunning yet. Then, I decided to try\phanthomsectionanyway, and it also works. I agree in that the solution with\phanthomsectionis much better, once you know it works. – ASdeL Jan 17 '12 at 18:50\phantomsectionand then\label, the label will point to the phantom section rather than to the real previous section. That's what you want if you're using\pageref, but if you use\refon such a label, it will give you a link to the location of the phantom, which is probably not what you want. – Ryan Reich Jan 18 '12 at 0:19