# Order of texorpdfstring in headings

I'm not sure whether the order of \texorpdfstring inside headings matters or if some part of the newcommands/NewDocumentCommands are messing with my MWE:

% !TeX program = lualatex
\documentclass{scrreprt}

\usepackage{lipsum}
\usepackage{xparse}

plainpages=false,%
pdfpagelabels=true%
]{hyperref}

\usepackage[acronym]{glossaries-extra}

\setabbreviationstyle[acronym]{long-short} % Stil des Abkürzungen im Text

\newacronym{ac:test}{TEST}{Only an acronym for testing purposes}

\NewDocumentCommand{\GT}{m m}{
\S\,#1~#2%
}

\NewDocumentCommand{\TESTGLS}{s m}{%TEST
\IfBooleanTF{#1}{%
\GT{#2}{%
\texorpdfstring{\glsfmtshort{ac:test}}{TEST}%
}%
}{%
\GT{#2}{%
\glsxtrshort{ac:test}%
}%
}%
}

\newcommand{\TEST}[1]{
\texorpdfstring{\S~#1~\glsfmtshort{ac:test}}{\S~#1~TEST}}

\begin{document}

\chapter{Different Tests}

\gls{ac:test}: \lipsum[1]

\section{\texorpdfstring{\S~1~\glsfmtshort{ac:test}}{\S~1~TEST}}

\gls{ac:test}: \lipsum[2]

\section{\TEST{2}}

\gls{ac:test}: \lipsum[3]

\section{\TESTGLS{3}}

\gls{ac:test}: \lipsum[4]

\section{\TESTGLS*{4}}

\gls{ac:test}: \lipsum[5]

\end{document}


In the first two section headings everything is fine, but in the last two (that contain my own commands) I get a warning Token not allowed in a PDF string (Unicode):(hyperref) removing '\TESTGLS' in both cases. Looking at the log-file unfortunately didn't reveal any further information for me.

• If you hide \texorpdfstring inside robust commands (defined e.g. with \NewDocumentCommand) hyperref will never see it and it does nothing. You always need something expandable at the outer level: May 4 at 21:56
• @UlrikeFischer Is there no chance to have it expand inside these robut commands? May 5 at 5:10

A command defined with \NewDocumentCommand is protected and doesn't change inside an \xdef. When writing the bookmarks hyperref will either throw it away or handle it with a definition stored with \pdfstringdefDisableCommands, but it will not look inside such a definition and so every \texorpdfstring there is useless.

If you want it to have some effect the outer definition must be expandable:

\documentclass{article}
\usepackage{hyperref}

\NewExpandableDocumentCommand\TEST{sm}
{\texorpdfstring
{\IfBooleanTF{#1}{\TESTtext*{#2}}{\TESTtext{#2}}}
{\TESTbookmarks{#2}}}

\NewDocumentCommand\TESTtext{sm}
{something complicated \IfBooleanT{#1}{with star and }#2}

\newcommand\TESTbookmarks[1]{simple #1}

\begin{document}