\nameref 和 xifthen 的 \isempty 无法正常工作

\nameref 和 xifthen 的 \isempty 无法正常工作

因此,我制作了一个宏\fullref并将其与 组合起来,\ifthenelse以在未打印任何内容时删除[]。但是,例如,当定理没有名称时,\nameref不会打印任何内容,但xifthen不会\isempty返回 true。发生了什么?

\documentclass[12pt]{article}
\usepackage[english, ukrainian]{babel}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}


\usepackage{xifthen}

\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{thmtools}
\declaretheoremstyle[
notefont=\normalfont\sffamily, notebraces={}{},
headfont=\bfseries\itshape,
bodyfont=\normalfont\itshape,
headformat=\NAME\ \NUMBER \NOTE,
headpunct=\sffamily.\\,
spaceabove=2mm, spacebelow=2mm,
postheadspace=0mm, headindent=3mm,
]{thmstyle}
\declaretheorem[style=thmstyle, numberwithin=section, name=Theorem]{thm}


\usepackage[]{hyperref}
\usepackage{nameref}
\usepackage{cleveref}
\crefname{thm}{thm.}{thms.}

\newcommand{\fullref}[1]{
    (See. \hyperref[#1]{
        \namecref{#1}\ \labelcref*{#1}\ 
        \ifthenelse{\isempty{\nameref*{#1}}}{}{{\sffamily[}\nameref*{#1}{\sffamily]}}, \cpageref{#1}})
}

\begin{document}
    \section{Theorems Section}
    \begin{thm}[Theorem with name]
        Theorem with name
        \label{thm:thm}
    \end{thm}
    \begin{thm}
        Theorem without name
        \label{thm:noname}
    \end{thm}
    
    \begin{gather}
        x^2+4=0,\ x \in \emptyset.\label{eq:shit}   
    \end{gather}

    \section{fullref calls}
    \fullref{thm:thm}\\
    \fullref{eq:shit}\\
    \fullref{thm:noname}
\end{document}

在此处输入图片描述

答案1

测试\isempty{\nameref*{#1}}总是会返回 false。

您需要更深入地了解。名称是 a 中的第三个支撑对象(共五个)\newlabel,您必须测试它。

感谢 Ulrike Fischer 指出\getrefbykeydefault

\documentclass[12pt]{article}
\usepackage[english, ukrainian]{babel}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{thmtools}
\usepackage{xifthen}
\usepackage[]{hyperref}
%\usepackage{nameref}
\usepackage{cleveref}

\declaretheoremstyle[
  notefont=\normalfont\sffamily, notebraces={}{},
  headfont=\bfseries\itshape,
  bodyfont=\normalfont\itshape,
  headformat=\NAME\ \NUMBER \NOTE,
  headpunct=\sffamily.\\,
  spaceabove=2mm, spacebelow=2mm,
  postheadspace=0mm, headindent=3mm,
]{thmstyle}
\declaretheorem[
  style=thmstyle,
  numberwithin=section,
  name=Theorem
]{thm}


\crefname{thm}{thm.}{thms.}

\makeatletter
\makeatletter
\newcommand{\fullref}[1]{%
  (See %
  \hyperref[#1]{%
    \namecref{#1} %
    \labelcref*{#1}, %
    \protected@edef\@temp{\getrefbykeydefault{#1}{name}{}}%
    \if\relax\detokenize\expandafter{\@temp}\relax\else\textsf{[}\nameref*{#1}\textsf{]}, \fi
    \cpageref{#1}%
  }%
  )%
}
\makeatother

\begin{document}

    \section{Theorems Section}
    \begin{thm}[Theorem with name]
        Theorem with name
        \label{thm:thm}
    \end{thm}
    \begin{thm}
        Theorem without name
        \label{thm:noname}
    \end{thm}
    
    \begin{gather}
        x^2+4=0,\ x \in \emptyset.\label{eq:shit}   
    \end{gather}

    \section{fullref calls}
    \fullref{thm:thm}\\
    \fullref{eq:shit}\\
    \fullref{thm:noname}

\end{document}

在此处输入图片描述

相关内容