当章节标题包含宏时 nameref 失败

当章节标题包含宏时 nameref 失败

我正在尝试使用\nameref章节。只要命令\chapter没有宏作为参数,它就可以正常工作。如果命令\chapter以宏作为参数,\nameref则始终打印最后一章的名称。\autoref但工作正常。MWE 在此处。

\documentclass{book}
\usepackage{pgfkeys}
\usepackage[usenames,dvipsnames,svgnames,table]{xcolor}
\usepackage{hyperref}

\hypersetup{
  colorlinks=true,
  linkcolor=Blue
}

\pgfkeys{%
  /chapter/.is family,%
  /chapter,%
  title/.initial = {},%
  label/.initial = {},%
}

\newcommand{\Chapter}[1]{%
  \pgfkeys{/chapter, #1, title/.get = \theChapterTitle, label/.get = \theChapterLabel}%
  \chapter{\theChapterTitle}\label{\theChapterLabel}%
}

\newcommand{\ChapRef}[1]{%
  \autoref{#1} {\textemdash} \nameref{#1}%
}

\begin{document}
%
\Chapter{label=chapter-one, title=Chapter One}
%
\Chapter{label=chapter-two, title=Chapter Two}
%
\Chapter{label=chapter-three, title=Chapter Three}
%

\ChapRef{chapter-one}

\ChapRef{chapter-two}

\ChapRef{chapter-three}

\end{document}

除了我的评论之外,我还附加了有关 \chapter 命令接受多个参数的 MWE。

\documentclass{book}
\usepackage{pgfkeys}
\usepackage{hyperref}

\hypersetup{
  colorlinks=true,
}

\pgfkeys{%
  /chap/.is family,%
  /chap,%
  label/.initial = {},%
  one/.initial = {},%
  two/.initial = {},%
}

\let\ea=\expandafter

\newcommand{\Chapter}[1]{%
  \pgfkeys{/chap, #1, one/.get=\First, two/.get=\Second, label/.get=\thechaplabel}%
  \ea\chapter{\ea\ea\ea\First\,\ea\textemdash\,\ea\Second}\label{\thechaplabel}
}

\newcommand{\ChapRef}[1]{%
  \autoref{#1} - \nameref{#1}%
}

\begin{document}

\Chapter{one=ABC, two=abc, label={abc-abc}}
\Chapter{one=PQR, two=pqr, label={pqr-pqr}}
\Chapter{one=XYZ, two=xyz, label={xyz-xyz}}

\chapter{New Chapter}
\ChapRef{abc-abc}

\ChapRef{pqr-pqr}

\ChapRef{xyz-xyz}

\end{document}

始终\nameref显示最新的章节名称,而\autoref显示正确的章节编号。

答案1

您的\Chapter宏存储\ChapterTitle在名称中,而不是名称本身。

在查看\ChapterTitle之前请务必先进行扩展:\chapter

\newcommand{\Chapter}[1]{%
  \pgfkeys{/chapter, #1, title/.get = \theChapterTitle, label/.get = \theChapterLabel}%
  \expandafter\chapter\expandafter{\theChapterTitle}\label{\theChapterLabel}%
}

对于 来说这不是必需的\label,因为此命令会扩展其参数,而该参数最终必须简化为字符串。


我不确定你使用的语法是否比更简单的语法更容易管理

\chapter{Chapter One}\label{chapter-one}

对于使用更多键的更复杂情况,有以下可能性:

\documentclass{book}
\usepackage{pgfkeys}
\usepackage{hyperref}

\hypersetup{
  colorlinks=true,
}

\pgfkeys{%
  /chap/.is family,%
  /chap,%
  label/.initial = {},%
  one/.initial = {},%
  two/.initial = {},%
}

% \expandonce is provided by etoolbox
\providecommand{\expandonce}{\unexpanded\expandafter}
% hyperref doesn't like \, in bookmarks
\newcommand{\titleemdash}{%
  \texorpdfstring{\,\textemdash\,}{\textemdash}%
}

\newcommand{\Chapter}[1]{%
  \pgfkeys{/chap, 
    #1,
    one/.get=\First,
    two/.get=\Second,
    label/.get=\thechaplabel
  }%
  % use the (top level) value of \First and \Second
  \begingroup\edef\x{\endgroup
    \noexpand\chapter{%
      \expandonce{\First}%
      \noexpand\titleemdash
      \expandonce{\Second}}}\x
  \label{\thechaplabel}
}

\newcommand{\ChapRef}[1]{%
  \autoref{#1} - \nameref{#1}%
}

\begin{document}

\Chapter{one=ABC, two=abc, label={abc-abc}}
\Chapter{one=PQR, two=pqr, label={pqr-pqr}}
\Chapter{one=XYZ, two=xyz, label={xyz-xyz}}

\chapter{New Chapter}
\ChapRef{abc-abc}

\ChapRef{pqr-pqr}

\ChapRef{xyz-xyz}

\end{document}

相关内容