
表内の特定の数字へのテキスト内相互参照を作成して、テキストで参照する数字が表内で変更されるたびにテキスト内の数字も変更されるようにすることは可能でしょうか? 私のテキストでは、表内の特定の合計を繰り返し参照する必要があります。問題は、合計を導き出すために使用される要素が継続的に変更されるため、テキスト内の数字を手動で何度も変更する必要があることです。テキスト内の数字の一部を修正し忘れるのではないかと心配しています。例として、表 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日の編集:
私 (Ulrich Diez) は、「number」が「integer」を意味しないことを見落としていました。から -thingie を
削除しました。\number\numexpr...\relax
\StoreNumber
と⟨テキスト内で数字を表すもの⟩.pdf ファイルのブックマークの拡張防止と pdf 文字列の作成に注意する必要があります。
zref-things では、babel-shorthands を有効にするために zref のラッパーを使用しませんでした。
これを実行して、\RetrieveNumber
最終的にセクション コマンドの引数の一部となり、pdf ブックマークにも含まれるようになった場合、問題のセクションのブックマークの作成中に、zref の babel ショートハンドのラッパーによって問題が発生します。
\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εカーネルのメカニズムに似た方法で実装されているため、すべてが一致するまで少なくとも2回文書をコンパイルする必要があります\label
。\ref
??ドキュメント内のあらゆる場所で正しい番号に置き換えられます。パッケージ ブックマークがロードされていないときにパッケージ ハイパーリファレンスが使用されている場合は、ドキュメントを少なくとも 3 回コンパイルする必要があります。
私の例では、textcase パッケージをロードしました。これは、文字の大文字/小文字化を防ぐ手段を提供します。
article のようなドキュメントクラスでは、\pagestyle{headings}
大文字のセクション タイトルを含むページ ヘッダーが自動的に作成されるため、参照ラベルの名前や取得する番号を示す文字が大文字になるのを防ぐためにこれが必要です。そうしないと、ページ ヘッダー内で が に\Retrievenumber{sum in table 1}
変換され\Retrievenumber{SUM IN TABLE 1}
、(「表 1 の合計」という数値とは異なり)「表 1 の合計」という数値は保存されないため、問題が発生します。
ブックマークは次のようになります: