1

I want to make a command that defines a \hypertarget with the target label (first argument of \hypertarget) by the contained \label and the target caption (second argument of \hypertarget) by its content.

\documentclass{article}

\usepackage{hyperref}
\newcommand\mytargetcommand[1]{?}

\begin{document}
\mytargetcommand{My content\label{tag}}
\ref{tag}
\end{document}

So this would output My content twice where the second references to the first.

Werner
  • 603,163
Daniel
  • 1,787
  • I think, you need a different strategy here –  May 31 '16 at 13:22
  • @Christian Hupfer Yes, I thought I could use the the label redefinition but it did not work. Hope there is a way to do this as well. :) – Daniel May 31 '16 at 13:24
  • \ref{tag} will not give My content however! –  May 31 '16 at 13:29
  • Perhaps you're looking rather for something like this: http://tex.stackexchange.com/a/271064/31729 –  May 31 '16 at 13:33
  • @Christian Hupfer No, I am really looking for a way to use the ordinary \label command while keeping the content and the label separate... \mytargetcommand{Text\label{tag}} -> \hypertarget{tag}{Text} – Daniel May 31 '16 at 13:41
  • Why don't you use two parameters, text and label, directly? – wilx May 31 '16 at 13:48
  • @wilx I need to use the \label command from the gui (LyX). Is the problem that \label is 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
  • @Daniel: It's in fact a little bit tricky to cope with #1\label –  May 31 '16 at 13:56
  • @Christian Hupfer That is surprising that one cannot access both at the same time. Would a two argument command help? \mytargetcommand{Text}{\label{tag}} -> \hypertarget{tag}{Text} – Daniel May 31 '16 at 14:00
  • Maybe something with @currentlabel. Sorry, this exceeds my LaTeX knowledge. (I guess that's why I am asking.) – Daniel May 31 '16 at 14:02
  • @Daniel: See the possible answer, but I am not sure, it will work always –  May 31 '16 at 14:04

1 Answers1

2

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}
  • I guess you were first:-) – David Carlisle May 31 '16 at 14:13
  • Thanks. Okay, so one problem is that it depends on where the \label is inserted. If it is inserted not at the end of the argument of mytargetcommand it will not work. Also it does not work when mytargetcommand is used in, for example, labels of descriptions. These are unfortunately quite some drawback. – Daniel May 31 '16 at 14:20
  • It is still puzzling to me why one cannot just get a handle to the label in the current scope of the \mytargetcommand. – Daniel May 31 '16 at 14:21
  • @Daniel: The drawback is coming from a wrong design -- your design, actually, sorry! –  May 31 '16 at 14:25
  • @Christian Hupfer. It would be helpful to know what exactly makes it wrong in LaTeX. Are labels not supposed to be inside commands? – Daniel May 31 '16 at 14:28
  • @Daniel: One issue is, that there should be counter that is to be labelled, e.g. a section or a figure etc. This is not the case here. The other issue is just the splitting of the text, into text and label stuff. hypertarget chokes on the \label inside the text, mainly on expansion, I think –  May 31 '16 at 14:33
  • @Christian Hupfer: I see. Yes, these weird design restraints are coming from the GUI. I was hoping that it's current functionality was sufficient given the rich features of LaTeX. But I guess the GUI+LaTeX cannot handle what I was after. By the way, what I am after is a command that creates the references as I explained. But it is not important that it uses hypertarget. But I guess that is not the problem. – Daniel May 31 '16 at 14:36
  • @Daniel: Therefore I never use GUIs for LaTeX -- they can't reproduce any feature provided by TeX. –  May 31 '16 at 14:38
  • @Christian Hupfer: One can always use plain latex within the gui, so that is no principled reason against GUIs. – Daniel May 31 '16 at 14:39
  • @Daniel: Sure. I've learned LaTeX without such GUIs, however, so I don't need them. They restrict too much, in my opinion –  May 31 '16 at 14:50
  • @Christian Hupfer: Again, I don't see the restriction. I just find GUIs helpful when writing, so I don't have to look at the commands all the time. – Daniel May 31 '16 at 17:24