問題的一般描述

問題的一般描述

問題的一般描述

我有一個附錄,其章節名稱必須與論文主要部分的章節名稱相同。因此我使用了\namerefpackage的指令nameref

[...]
\mainmatter
\section{My section name}
\label{mySection}
[...]
\appendix
\section{\nameref{mySection}}
[...]

這有效。但是,圖書類別中左側頁面的標題應該顯示頁面第一部分的名稱。使用時\nameref,顯示標題??而不是部分名稱。為什麼會有這樣的表現?我該如何解決這個問題?

簡單的例子

例如,建立一個main.tex包含以下內容的新文件

\documentclass[10pt,a4paper]{book}
\usepackage{nameref}

\begin{document}

\section{Alice in wonderland}
\label{section:alice}

\pagebreak
a %Dummy text otherwise calling \pagebreak twice only skips one page
\pagebreak %Need to skip two pages, not just one because only the header of the left page of the book class displays the section name, not the one of the right page

%\section{Alice in wonderland} %Writes the header correctly
\section{\nameref{section:alice}} %Header shows "??"

\end{document}

編譯用pdflatex main.tex.在第三頁的頂部,顯示標題0.2 ??而不是0.2 ALICE IN WONDERLAND

答案1

預設情況下book,類別的所有標題都大寫。這個上殼的工作方式意味著當\nameref{section:alice}寫入標頭時,實際資料是

\nameref{SECTION:ALICE}

這是對未知標籤的引用,我們也在 MWE 的日誌中看到它

LaTeX Warning: Reference `SECTION:ALICE' on page 3 undefined on input line 17

一個骯髒的修復方法是實際使用\label{SECTION:ALICE}


如今,使用純大寫標題可能會被視為喊叫,所以我通常只是禁用自動大寫。

通常我從不使用book該類,而是使用該類memoir,該類具有nameref內置功能,如\titleref.此外,memoir透過以下方式也可以輕鬆停用預設的大寫字母:

\nouppercaseheads
\pagestyle{headings} % reactivate the page style (\nou.. changes an internal macro in the headers, thus the header macros has to be applied again)

該類memoir可以用作該類的替代品book

答案2

您可以使用迭戈·迪亞斯的伎倆在這裡這是受到refcount包裝的啟發。

\documentclass[10pt,a4paper]{book}
\usepackage{nameref}

\makeatletter
\newcommand{\getnamereftext}[1]{%
  \@ifundefined{r@#1}{}{%
    \unexpanded\expandafter\expandafter\expandafter{%
      \expandafter\expandafter\expandafter\@thirdoffive\csname r@#1\endcsname
    }%
  }%
}
\makeatother

\begin{document}

\section{Alice in wonderland}
\label{section:alice}

\clearpage
a %Dummy text otherwise calling \pagebreak twice only skips one page
\clearpage %Need to skip two pages

\section{\getnamereftext{section:alice}}

\end{document}

在此輸入影像描述

答案3

您的\nameref{},其工作方式類似於\label{}必須位於\section{}命令之外:

\section{\nameref{section:alice}} %Header shows "??"

\section{ Alice in Wonderland} \nameref{section:alice} %works, returns label

正如 daleif 所指出的:將\label{}\nameref{}都大寫可以完美地作為一種骯髒的解決方法,而不返回內容\label{}

相關內容