對錶中數字的文字引用

對錶中數字的文字引用

我想知道是否可以對錶中的特定數字進行文本內交叉引用,以便每當我在文本中引用的數字在表中發生變化時,文本內的數字就會發生變化?在我的文字中,我必須反覆提及表格中的具體金額。問題是,由於用於得出總和的元素不斷變化,我必須多次手動更改文字內的數字。我擔心我可能會錯過更正一些文內數字。舉個例子,假設表 1 採用以下形式

$$\begin{array}{|c|c|} 
 & \text{Sum}  \\ \hline
Bla bla & 5  \\ \hline
\end{array}$$

在表 1 中,總和等於 5。

當我反覆對總和進行類似的引用時,我希望有一個類似的解決方案:

“表 1 顯示總和等於 \ref{表 1 中的總和}”

,我在表 1 中「標記」了 5 的數字「表 1 中的總和」。 (我想舉一個例子,但我認為這是不可能的)最後,我希望有一個更“通用的解決方案”,因為我在實際論文中有幾個“此類”表格。

答案1

在您的問題文字中,您使用環境array來建立表格。但該環境array用於在數學模式下創建矩陣。我建議不要濫用創建表的環境array,而是使用tabular創建表的環境。


透過包包的方式茲參考您可以輕鬆地引入自己的交叉引用系統,類似於LaTeX 2ε-核心中實現的\label- -機制:\ref

以下範例定義

\StoreNumber{⟨ID/label⟩}{⟨something that in the text should denote a number⟩}

\RetrieveNumber{⟨ID/label⟩}


2020年11月5日編輯:

我(烏爾里希·迪茲)忽略了“數字”並不意味著“整數”。
\number\numexpr...\relax從 中刪除了 -thingie \StoreNumber

⟨文中應表示數字的東西⟩您需要注意防止擴充功能以及為 .pdf 檔案的書籤建立 pdf 字串。

對於 zref-things,我沒有使用 zref 的包裝器來啟用 babel-shorthands。
如果我這樣做並\RetrieveNumber最終成為分段命令參數的一部分,該命令也可能最終出現在 pdf 書籤中,那麼 zref 的 babel-shorthands 包裝器將在為相關部分建立書籤時出現問題。


\documentclass{article}

\usepackage[unicode=true]{hyperref}
\usepackage[overload]{textcase}
\usepackage{zref}
\makeatletter
\@ifpackageloaded{hyperref}{%
  \@ifdefinable\RetrieveNumber@RefUndefText{%
    \DeclareRobustCommand\RetrieveNumber@RefUndefText{%
      \texorpdfstring{\nfss@text{\reset@font\bfseries ??}}{??}%
    }%
  }%
  \@ifdefinable\RetrieveNumber{%
    \DeclareRobustCommand\RetrieveNumber[1]{%
      \texorpdfstring{\zref@refused{#1}}{}%
      \zref@extractdefault{#1}{numbertostore}{\RetrieveNumber@RefUndefText}%
    }%
  }%
}{%
  \@ifdefinable\RetrieveNumber@RefUndefText{%
    \DeclareRobustCommand\RetrieveNumber@RefUndefText{%
      \nfss@text{\reset@font\bfseries ??}%
    }%
  }%
  \@ifdefinable\RetrieveNumber{%
    \DeclareRobustCommand\RetrieveNumber[1]{%
      \zref@refused{#1}%
      \zref@extractdefault{#1}{numbertostore}{\RetrieveNumber@RefUndefText}%
    }%
  }%
}%
%
\zref@newprop{numbertostore}[0]{0}%
\@ifdefinable\StoreNumber{%
  \DeclareRobustCommand\StoreNumber[2]{%
    \begingroup
    \zref@setcurrent{numbertostore}{#2}%
    \zref@labelbyprops{#1}{numbertostore}%
    \endgroup
    \RetrieveNumber{#1}%
  }%
}%
\@ifdefinable\CheckWhetherMath{%
  \DeclareRobustCommand\CheckWhetherMath{%
    \relax\ifmmode\expandafter\@firstoftwo\else\expandafter\@secondoftwo\fi 
  }%
}%
\makeatother

\usepackage{amsmath}

\usepackage[cbgreek]{textgreek} % cbgreek/euler/artemisia

\pagestyle{headings}

\begin{document}

\section{About a sum which equals \protect\NoCaseChange{\RetrieveNumber{sum in table 1}}}
\label{about a sum in table 1}

This section has the title: \nameref*{about a sum in table 1}.

\bigskip

\noindent\autoref*{table1} shows that the sum equals \RetrieveNumber{sum in table 1}.

% pi-symbol is  0x03C0 in utf-16.
% higher byte is 03 (Hex) = 3(dec) = 003(oct), 
% lower byte is C0 (Hex) = 192(dec) =  300(oct), 
% Higher byte must be marked by \9, thus the unicode-octal-triplet-pdfstring is: \9003\300

\begin{table}[h]
\centering
\begin{tabular}{|c|c|}
\hline
&\text{Sum}\\ \hline
Bla bla&\StoreNumber{sum in table 1}{\string\texorpdfstring{\CheckWhetherMath{\pi}{\textpi}}{\string\9003\string\300}}\\\hline
\end{tabular}%
\caption{A table where the sum equals \RetrieveNumber{sum in table 1}}%
\label{table1}%
\end{table}

\noindent\autoref*{table1} shows that the sum equals \RetrieveNumber{sum in table 1}.

\begin{equation}
\RetrieveNumber{sum in table 1}+\RetrieveNumber{sum in table 1}=2\cdot\RetrieveNumber{sum in table 1}
\end{equation}

\bigskip

\noindent\autoref*{table1} has the title: \nameref*{table1}

\bigskip

\hrule

\bigskip

\section{About a sum which equals \protect\NoCaseChange{\RetrieveNumber{sum in table 2}}}
\label{about a sum in table 2}

This section has the title: \nameref*{about a sum in table 2}.

\bigskip

\noindent\autoref*{table2} shows that the sum equals \RetrieveNumber{sum in table 2}.

\begin{table}[h]
\centering
\begin{tabular}{|c|c|}
\hline
&\text{Sum}\\ \hline
Bla bla&\StoreNumber{sum in table 2}{\number\numexpr(1*3)+1+1\relax}\\\hline
\end{tabular}%
\caption{A table where the sum equals \RetrieveNumber{sum in table 2}}%
\label{table2}%
\end{table}

\noindent\autoref*{table2} shows that the sum equals \RetrieveNumber{sum in table 2}.

\bigskip

\noindent\autoref*{table2} has the title: \nameref*{table2}

\begin{equation}
\RetrieveNumber{sum in table 2}+\RetrieveNumber{sum in table 2}=2\cdot\RetrieveNumber{sum in table 2}
\end{equation}

\bigskip

\hrule

\bigskip

\tableofcontents

\listoftables

\end{document}

由於儲存和檢索數字的實作類似於LaTeX 2ε-kernel 的\label機制\ref,因此您需要至少編譯文件兩次,直到所有內容都匹配並且??文件中各處均替換為正確的數字。如果套件 hyperref 正在使用而套件書籤未加載,則需要至少編譯文件 3 次。

在我的範例中,我載入了 textcase-package。它提供了防止字元大寫/小寫的方法。

對於像文章這樣的文件類,它會\pagestyle{headings}自動建立帶有大寫字母分段標題的頁眉,這是為了防止表示要檢索的引用標籤或數字名稱的字元大寫。否則,a\Retrievenumber{sum in table 1}將被轉換\Retrievenumber{SUM IN TABLE 1}為頁頭內的內容,這將是一個問題,因為(與數字「表 1 中的總和」不同)數字「表 1 中的總和」從未儲存。

在此輸入影像描述

書籤看起來像這樣:

在此輸入影像描述

相關內容