Perhaps there are better ways to split the My content\label{tag} stuff such that \hypertarget does not choke on \label: Use \def\splitcmd#1\label#2 and a similar command that splits the with undelimited arguments (actually, \label is the argument delimiter here).
This uses a dummy counter and to refer to the text, use \nameref instead.
\documentclass{article}
\usepackage{hyperref}
\makeatletter
% Two helper commands --> get the text before \label
\def\splitcmd#1\label#2{%
#1%
}
% Get the label tag
\def\splitcmdother#1\label#2{%
#2%
}
\newcounter{localtagcntr}
\newcommand\mytargetcommand[1]{%
\edef\local@tag{\splitcmdother#1}%
\edef\my@text{\splitcmd#1}%
\refstepcounter{localtagcntr}%
\def\@currentlabelname{\my@text}% Change the label content name
\label{\local@tag}% Set the label
\hypertarget{\local@tag}{\my@text}%
}
\makeatother
\begin{document}
Foostuff
\clearpage
\mytargetcommand{My content\label{tag}}
\ref{tag}
\clearpage
\hyperlink{tag}{Stuff}
\nameref{tag}
\end{document}
\ref{tag}will not giveMy contenthowever! – May 31 '16 at 13:29\labelcommand while keeping the content and the label separate...\mytargetcommand{Text\label{tag}}->\hypertarget{tag}{Text}– Daniel May 31 '16 at 13:41\labelcommand from the gui (LyX). Is the problem that\labelis inside of\mytargetcommand? I guess I could put it just after the command if that would help. Like\mytargetcommand{Text}\label{tag}->\hypertarget{tag}{Text}– Daniel May 31 '16 at 13:51#1\label– May 31 '16 at 13:56\mytargetcommand{Text}{\label{tag}}->\hypertarget{tag}{Text}– Daniel May 31 '16 at 14:00