3

I'd like to test if the "name" key attached to a label when nameref is loaded is empty. The value can be retrieved expandably with refcount's \getrefbykeydefault and tested as follows.

\documentclass{article}

\usepackage{nameref}

\begin{document}

\section{bla} \label{mylab1}
\section{} \label{mylab2}

\ExplSyntaxOn
\tl_if_empty:eTF { \getrefbykeydefault{mylab1}{name}{} }
  { EMPTY }{ NOT~EMPTY }
\par
\tl_if_empty:eTF { \getrefbykeydefault{mylab2}{name}{} }
  { EMPTY }{ NOT~EMPTY }
\ExplSyntaxOff

\end{document}

expandtest

However, as soon as the "name" contains nonexpandable tokens, the test errors.

\documentclass{article}

\usepackage{nameref}

\begin{document}

\section{bla\textit{h}} \label{mylab}

\ExplSyntaxOn
\tl_if_empty:eTF { \getrefbykeydefault{mylab}{name}{} }
  { EMPTY }{ NOT~EMPTY }
\ExplSyntaxOff

\end{document}

produces the error

! Argument of \@sect has an extra }.
<inserted text> 
                \par 
l.10 ...eTF { \getrefbykeydefault{mylab}{name}{} }
                                                  
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?
{\normalfont \Large \bfseries }{\unexpanded {h}}\ifx \reserved@a \@empty \ETC.
! Paragraph ended before \@sect was complete.
<to be read again> 
                   \par 
l.10 ...eTF { \getrefbykeydefault{mylab}{name}{} }
                                                  
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.

I'm aware putting commands like \textit in section titles is bad practice. Assuming I don't have control over the input, how can I check whether or not the "name" attached to a label is empty?


If I were using ltproperties, I could test the content of the "title" key (note this is comparable to refcount's "name" key, not its "title" key).

\documentclass{article}

\usepackage{nameref}

\begin{document}

\section{bla\textit{h}}
\label{reallabel1}
\RecordProperties{mylab1}{title}

\section{}
\label{reallabel2}
\RecordProperties{mylab2}{title}

\ExplSyntaxOn
\tl_if_empty:eTF { \RefProperty{mylab1}{title} }
  { EMPTY }{ NOT~EMPTY }
\par
\tl_if_empty:eTF { \RefProperty{mylab2}{title} }
  { EMPTY }{ NOT~EMPTY }
\ExplSyntaxOff

%\RefProperty{mylab1}{title} % this errors

\nameref{reallabel1} % but this works

\end{document}

ltprop

Unlike the \getrefbykeydefault example, this does not error when the section title contains unexpandable tokens. However this has two issues. First, if you uncomment \RefProperty{mylab1}{title}, you get an error, so it is not a replacement for \nameref. Second, I'd like this data to be recoverable from just the label data, without needing \RecordProperties for every instance.

5
  • This will probably get howls from folks who started using TeX more recently than I did, but here goes. I'd pack the contents of the \section argument into a temporary box that begins with \ignorespaces, and then measure the width of that box. If it's zero, then the box is empty; otherwise it's not. This shouldn't get hung up by internal uses of font-changes, etc., although an undefined command or real syntax error would be challenged before measurement could take place. Dec 7, 2023 at 0:50
  • Why not make the default \empty and test for that? You don't need expl syntax for that, but I supposed you could use it if you really wanted to. Dec 7, 2023 at 1:16
  • @JohnKormylo Do you mean test if \getrefbykeydefault{mylab}{name}{\empty} is the same as \empty with \ifx? If so, I think the same issue shows up since you need to expand \getrefbykeydefault, assumedly with an \edef, and the unexpandable commands break it. Or have I misunderstood?
    – mbert
    Dec 7, 2023 at 4:56
  • 1
    the behaviour of \RecordProperties is a bug. I added an issue about that. Dec 7, 2023 at 9:09
  • @UlrikeFischer Found your use case yet? :)
    – gusbrs
    Dec 7, 2023 at 13:49

1 Answer 1

5

Arbitrary user input should never be put in an \edef or expanded with the e or x type. For a safe expansion, LaTeX provides \protected@edef which handles also robust commands.

\documentclass{article}

\usepackage{nameref}

\begin{document}

\section{bla\textit{h}} \label{mylab}

\ExplSyntaxOn\makeatletter 
\protected@edef\l_tmpa_tl{ \getrefbykeydefault{mylab}{name}{}}
\tl_if_empty:NTF \l_tmpa_tl
  { EMPTY }{ NOT~EMPTY }
\ExplSyntaxOff

\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .