I want to \hypertarget some of my sections. I know I could \label them or hypertarget right before them, but this is part of a larger framework and I don't want to treat the case where the target happens to be a section title differently.
Here is my minimal example, it works with \section* but it doesn't with \section. Why?
\documentclass[a4paper,12pt]{article}
\usepackage[pdfstartview=FitH]{hyperref}
\usepackage{bookmark}
\begin{document}
\section*{one\hypertarget{sec:one}{}}
\bookmark[dest=sec:one]{sec one}
\section{two\hypertarget{sec:two}{}}
\bookmark[dest=sec:two]{sec two}
\end{document}
ps: fantastic, neither hypertarget nor section are valid tags. somebody please insert something useful in there.
edit: incorparating \texorpdfstring the above minimal example works, but as ususal it was too minimal, my production code doesnt compile. i created a minimal example closer to the real use, it creates a correct file but with a lot of error messages, the terrible anonymous undefined control sequence on the line the macro is used. how can i debug this error message? can someone tell me what is wrong with the code?
\documentclass[a4paper,12pt]{article}
\usepackage{amsmath}
\usepackage[pdfstartview=FitH]{hyperref}
\usepackage[atend]{bookmark}
\newcounter{nops}
\makeatletter
\newcommand{\nopimpl}{\stepcounter{nops}\Hy@raisedlink{\hypertarget{nop\arabic{nops}}{}}\begingroup\edef\x{\endgroup\noexpand\BookmarkAtEnd{\noexpand\bookmark[dest=nop\arabic{nops}]{page \arabic{nops}}}}\x}
\newcommand{\nop}{\texorpdfstring{\iffirstchoice@\nopimpl\fi}{}}
\makeatother
\begin{document}
\section{one\nop}
\section{two\nop}
\end{document}
edit2: thanks again. now those errors are gone, but in the real file there is a toc, and the toc items get their own target. how can i prevent that?
also how come robustness is only a matter of declaration? i thought it should be a property of the code that is to be executed. if i can simply declare stuff robust, why doesnt latex make everything robust?
\documentclass[a4paper,12pt]{article}
\usepackage{amsmath}
\usepackage[pdfstartview=FitH]{hyperref}
\usepackage[atend]{bookmark}
\newcounter{nops}
\makeatletter
\DeclareRobustCommand{\nopimpl}{\stepcounter{nops}\Hy@raisedlink{\hypertarget{nop\arabic{nops}}{}}\begingroup\edef\x{\endgroup\noexpand\BookmarkAtEnd{\noexpand\bookmark[dest=nop\arabic{nops}]{page \arabic{nops}}}}\x}
\DeclareRobustCommand{\nop}{\texorpdfstring{\iffirstchoice@\nopimpl\fi}{}}
\makeatother
\begin{document}
\tableofcontents
\section{one\nop}
\section{two\nop}
\end{document}
\DeclareRobustCommand{\foo}{bar}actually defines\footo be\protect\foo<space>and defines\foo<space>to{bar}. A special trick is used so that the space is allowed in that internal macro, but is taken as normal space if written to the.tocor.auxfile. I know, its a little complicated. Some macro should be expandable, so they should not be robust. Moving arguments are the exception not the rule and making all macro robust would be an overhead. – Martin Scharrer♦ May 5 '11 at 10:26