如何在交叉引用中包含書籍/部分名稱(如果是跨書籍/部分)?

如何在交叉引用中包含書籍/部分名稱(如果是跨書籍/部分)?

我的文檔結構基本上是其他較小文檔的集合。我決定不再往下走xr-超級並擁有單獨的 PDF 文檔,因為無論如何最終的要求都是單一文件。因此,我決定建立一個大的 LaTeX 文檔,其中的子文檔如下圖書,例如:

\documentclass{memoir}
\renewcommand*{\thebook}{\Alph{book}} % "Book A", "Book B", etc.
\begin{document}
\book{maindoc}
\chapter{maindoc first chapter}
\label{chap:maindoc-first}
\chapter{maindoc second chapter}
\label{chap:maindoc-second}

\book{extradoc}
\setcounter{chapter}{0} % reset chapter counter with each book
\chapter{extradoc first chapter}
\label{chap:extradoc-first}
\chapter{extradoc second chapter}
\label{chap:extradoc-second}
As shown in \ref{chap:maindoc-first}, and further research in \ref{chap:extradoc-first} ...

\book{anotherdoc}
\setcounter{chapter}{0}
\chapter{anotherdoc chapter}
\end{document}

現在,這As shown in \ref{chap:maindoc-first}將呈現為好像該章節位於當前部分的範圍內。我想要完成的是這樣的:

  • 如maindoc的第1章和第1章所示...
  • 如第1章(maindoc)和第1章...所示
  • 如(1,maindoc)和(1)...所示
  • 如圖A-1和1...

但是,由於大多數參考文獻都在文件本身中,因此我不想添加前綴全部的參考文獻如本次問答

我怎樣才能做到這一點?有沒有辦法確定引用的範圍並有條件地包含前綴?

最終,我想對定理、圖形、表格等的引用做同樣的事情。

答案1

由於您正在使用memoir,因此您應該利用該類別提供的交叉引用設施。這些內容在第 16 章中有所介紹使用者手冊

memoir然而,確實有一些關於格式化的相當明確的想法,因此並不總是提供超出這些想法的靈活性。在這種情況下,它認為對文件其他部分的交叉引用應該要大寫。如果您想避免這種情況,則需要提供替代方案。

以下內容基於 提供的版本memoir

\aref% based on \Aref
\bref% \Bref
\cref% \Cref
\sref% \Sref

小寫名稱由以下提供:

\lcbookrefname% based on \bookrefname
\lcchapterrefname% \chapterrefname
\lcsectionrefname% identical to \sectionrefname
\lcappendixrefname% based on \appendixrefname

\titleref這允許您在與提供的命令結合使用時根據需要設定引用memoir

為了自動執行此操作,\chapref{}提供了一個命令,該命令應產生根據您列出的第一種可能性格式化的參考。您可以建立其他命令以類似的方式實現其他格式。

筆記:我懷疑我編寫命令的方式\chapref至少可能不是最好的方式。買者自負...

\documentclass{memoir}
\renewcommand*{\thebook}{\Alph{book}} % "Book A", "Book B", etc.
\newcommand*{\aref}[1]{\lcappendixrefname\ref{#1}}
\newcommand*{\bref}[1]{\lcbookrefname\ref{#1}}
\newcommand*{\cref}[1]{\lcchapterrefname\ref{#1}}
\newcommand*{\sref}[1]{\lcsectionrefname\ref{#1}}
\newcommand*{\lcbookrefname}{book~}
\newcommand*{\lcchapterrefname}{chapter~}
\newcommand*{\lcsectionrefname}{\S}
\newcommand*{\lcappendixrefname}{appendix~}
\makeatletter
\def\chapref@book#1:#2-#3\@nil{#2}
\newcommand*{\chapref}[1]{%
  \edef\tempa{\expandafter\chapref@book#1\@nil}%
  \edef\tempb{\ref{book:\tempa}}%
  \edef\tempc{\thebook}%
  \ifx\tempb\tempc
  \cref{#1}%
  \else
  \cref{#1} of the \titleref{book:\tempa}%
  \fi}
\makeatother
\begin{document}
  \book{maindoc}
  \label{book:maindoc}
  \chapter{maindoc first chapter}
  \label{chap:maindoc-first}
  \chapter{maindoc second chapter}
  \label{chap:maindoc-second}

  \book{extradoc}
  \label{book:extradoc}
  \setcounter{chapter}{0} % reset chapter counter with each book
  \chapter{extradoc first chapter}
  \label{chap:extradoc-first}
  \chapter{extradoc second chapter}
  \label{chap:extradoc-second}

  Manually using the facilities of the class:

  As shown in \cref{chap:maindoc-first} of the \titleref{book:maindoc}, and further research in \cref{chap:extradoc-first} ...

  \noindent Or using the custom command:

  As shown in \chapref{chap:maindoc-first}, and further research in \chapref{chap:extradoc-first} \dots

  \book{anotherdoc}
  \setcounter{chapter}{0}
  \chapter{anotherdoc chapter}
\end{document}

手動和自動章節參考

相關內容