![\nameref 和 xifthen 的 \isempty 无法正常工作](https://linux22.com/image/461113/%5Cnameref%20%E5%92%8C%20xifthen%20%E7%9A%84%20%5Cisempty%20%E6%97%A0%E6%B3%95%E6%AD%A3%E5%B8%B8%E5%B7%A5%E4%BD%9C.png)
因此,我制作了一个宏\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}