[Tex/LaTex] hyperref error : ! Argument of \@providesfile has an extra }

hyperreflualuatex

Using a solution to adjust spacing in my document with LuaLaTeX, I run into this cryptic error, as soon as I use \label{} in a math environment:

(c:/texlive/2016/texmf-dist/tex/generic/oberdiek/se-pdfdoc.def
! Argument of \@providesfile has an extra }.
<inserted text> 
\par 
l.51 ...dblpnct{\hspace{0.08334em}}: PDFDocEncoding]

I've run across a `}' that doesn't seem to match anything.
For example, `\def\a#1{...}' and `\a}' would produce
this error. If you simply proceed now, the `\par' that
I've just inserted will cause me to report a runaway
argument that might be the root of the problem. But if
your `}' was spurious, just type `2' and it will go away.

Runaway argument?
{se-pdfdoc.def}{\@firstoftwo {\@hspacer }}\def \reserved@b {\@hspace \ETC.
! Paragraph ended before \@providesfile was complete.
<to be read again> 
\par 
l.51 ...dblpnct{\hspace{0.08334em}}: PDFDocEncoding]

I suspect you've forgotten a `}', causing me to apply this
control sequence to too much text. How can we recover?
My plan is to forget the whole thing and hope for the best.

File: se-pdfdoc.def 2016/05/16 v1.11 stringenc\dblpnct {\protect \let \reserved@
d =*\def \par }: PDFDocEncoding
! Argument of \@providesfile has an extra }.
<inserted text> 
\par 
l.51 ...dblpnct{\hspace{0.08334em}}: PDFDocEncoding]

I've run across a `}' that doesn't seem to match anything.
For example, `\def\a#1{...}' and `\a}' would produce
this error. If you simply proceed now, the `\par' that
I've just inserted will cause me to report a runaway
argument that might be the root of the problem. But if
your `}' was spurious, just type `2' and it will go away.

Runaway argument?
{se-pdfdoc.def}{\@firstoftwo {\@hspacer }}\def \reserved@b {\@hspace \ETC.
! Paragraph ended before \@providesfile was complete.
<to be read again> 
\par 
l.51 ...dblpnct{\hspace{0.08334em}}: PDFDocEncoding]

I suspect you've forgotten a `}', causing me to apply this
control sequence to too much text. How can we recover?
My plan is to forget the whole thing and hope for the best.

) [1
Non-PDF special ignored!

MWE:

\documentclass{scrbook}

\usepackage[pdfencoding=auto,pdfpagelabels=true,psdextra]{hyperref}   % PROBLEMATIC PACKAGE
\pdfstringdefDisableCommands{\def\dblpnct#1{}}   % TRIED TO FIX IT, BUT FAILED

\usepackage{luacode}
\usepackage{etoolbox}
\newrobustcmd\dblpnct[1]{%
  \ifincsname\else
    \ifmmode\else
      \ifhmode
        \unskip
      \fi
      \nolinebreak#1%
    \fi
  \fi
}
\makeatletter
% latex.ltx, line 4070:
\def\label#1{\@bsphack
  \protected@write\@auxout{\let\dblpnct\@gobble}% <-- disable in \label
         {\string\newlabel{#1}{{\@currentlabel}{\thepage}}}%
  \@esphack}
\makeatother
\begin{luacode}
function dosub(s)
    s = string.gsub(s, ':', '\\dblpnct{\\hspace{0.08334em}}:')
    return(s)
end
\end{luacode}

\AtBeginDocument{%
    \luaexec{luatexbase.add_to_callback("process_input_buffer", dosub, "dosub")}%
}

\begin{document}

\begin{equation}
\label{eq:test}
\end{equation}

\end{document}

Best Answer

You should move the \hspace from the gsub in the definition of \dblpnct so that you don't have to fight against the expansion of the \hspace too. Then you can use \NR@sanitize@labelname to sanitize the label name. I exagerated the inserted space to check if there are side effects.

\documentclass{scrbook}

\usepackage[pdfencoding=auto,pdfpagelabels=true,psdextra]{hyperref}   % PROBLEMATIC PACKAGE
\pdfstringdefDisableCommands{\def\dblpnct#1{}}   % TRIED TO FIX IT, BUT FAILED
%
\usepackage{luacode}
\usepackage{etoolbox}
\newrobustcmd\dblpnct[1]{%
  \ifincsname\else
    \ifmmode\else
      \ifhmode
        \unskip
      \fi
      \nolinebreak\hspace{#1}%
    \fi
  \fi
}

\begin{luacode}
function dosub(s)
    s = string.gsub(s, ':', '\\dblpnct{1.08334em}:')
    return(s)
end
\end{luacode}

\makeatletter
\AtBeginDocument{%
    \luaexec{luatexbase.add_to_callback("process_input_buffer", dosub, "dosub")}%
    \ifpatchable*\NR@sanitize@labelname
    {\pretocmd\NR@sanitize@labelname{\let\dblpnct\@gobble}{}{\failed}}
    {}
}
\makeatother

\begin{document}

a:b

\begin{equation}
\label{eq:test} 
\end{equation}

a:b \label{ref:blub} a:b \ref{eq:test} a:b

\end{document}

enter image description here

Related Question