
私は本のクラスと番号の方程式を次のように使用したいと思います。
方程式は方程式の数だけ持つ必要があるそれなし 章番号。私はこれを使って
\renewcommand{\theequation}{\arabic{equation}}
\ref
コマンドを使うときは、
\ref
が方程式の同じ章で呼び出される場合、参照は単に(equation)
\ref
方程式の別の章で参照される場合は、(equation)\textsubscript{name-of-the-chapter-containing-equation}
ここで私の試みはzref
\documentclass{book}
\usepackage{amsmath}
\usepackage{hyperref}
\usepackage{zref-user}
\makeatletter
\zref@newprop{chapter}{\thechapter}
\zref@addprop{main}{chapter}
\makeatother
\numberwithin{equation}{section}
\renewcommand{\theequation}{\arabic{equation}}
\newcommand{\myref}[1]{\ifnum\thechapter=\zref[chapter]{#1} (\zref{#1}) \else (\zref{#1})\textsubscript{something} \fi}
\begin{document}
\chapter{First}
\ifnum\myref[chapter]{eq:1}=1 a \else b \fi
\myref{eq:1}, \myref{eq:2}, \myref{eq:3}, \myref{eq:4}
\section{First First}
\begin{equation}\zlabel{eq:1}
E=\gamma m
\end{equation}
\section{First Second}
\begin{equation}\zlabel{eq:2}
0=0
\end{equation}
\chapter{Second}
\myref{eq:1}, \myref{eq:2}, \myref{eq:3}, \myref{eq:4}
\begin{equation}\zlabel{eq:3}
e^{i\pi}+1=0
\end{equation}
\chapter{Third}
\myref{eq:1}, \myref{eq:2}, \myref{eq:3}, \myref{eq:4}
\begin{equation}\zlabel{eq:4}
f(w) = \frac{1}{2i\pi}\oint_{C_w}\frac{f(z)\mathrm{d}z}{z-w}
\end{equation}
\chapter{Last}
\myref{eq:1}, \myref{eq:2}, \myref{eq:3}, \myref{eq:4}
\end{document}
私は定義に2つの問題があると考えています\myref
\ifnum
\zref[chapter]{#1}
maybe は数値を返さないため、`\thechapter' と比較することができず、正しく動作しません。番号に対応する章名を取得するコマンドを見つける必要があります(または、 を使用するかもしれません
\nameref
)。
どうすれば解決できるでしょうか?
答え1
これは次のように実行できます。まず、\zrefused
andを使用し\zref@extract
て比較する値を取得し、追加の書式設定をトリガーします。
このパッケージでは、zref-titleref
タイトル フィールドを取得する方法が示されていますが、章だけでなくセクションなどでも更新されます。そのため、代わりにzref-titleref
章のタイトルだけを取得するコードを修正したバージョンを記述します。
\documentclass{book}
\usepackage{amsmath}
\usepackage{zref-user,gettitlestring}
\usepackage{hyperref}
\newcommand{\setcurrentchaptername}[1]{\GetTitleStringNonExpand{#1}\edef\currentchaptername{\detokenize\expandafter{\GetTitleStringResult}}}
\def\currentchaptername{}
\makeatletter
\zref@newprop{chapter}{\thechapter}
\zref@addprop{main}{chapter}
\zref@newprop{chaptername}{\currentchaptername}
\zref@addprop{main}{chaptername}
\ZREF@patch{@chapter}{\def\@chapter[#1]{\setcurrentchaptername{#1}%
\ZREF@org@@chapter[{#1}]}}
\makeatother
\numberwithin{equation}{chapter}
\renewcommand{\theequation}{\arabic{equation}}
\makeatletter
\newcommand{\myref}[1]{\zrefused{#1}%
\ifnum\zref@extract{#1}{chapter}=\thechapter\relax
(\zref{#1}) \else (\zref{#1})\textsubscript{\zref@extract{#1}{chaptername}}\fi}
\makeatother
\begin{document}
\chapter{First}
\myref{eq:1}, \myref{eq:2}, \myref{eq:3}, \myref{eq:4}
\section{First First}
\begin{equation}\zlabel{eq:1}
E=\gamma m
\end{equation}
\section{First Second}
\begin{equation}\zlabel{eq:2}
0=0
\end{equation}
\chapter{Second}
\myref{eq:1}, \myref{eq:2}, \myref{eq:3}, \myref{eq:4}
\begin{equation}\zlabel{eq:3}
e^{i\pi}+1=0
\end{equation}
\chapter{Third}
\myref{eq:1}, \myref{eq:2}, \myref{eq:3}, \myref{eq:4}
\begin{equation}\zlabel{eq:4}
f(w) = \frac{1}{2i\pi}\oint_{C_w}\frac{f(z)\mathrm{d}z}{z-w}
\end{equation}
\chapter{Last}
\myref{eq:1}, \myref{eq:2}, \myref{eq:3}, \myref{eq:4}
\end{document}
コードにあるように、\zref@newprop
は追跡する新しいプロパティを設定するために使用されます。これら\zlabel
には、コマンドが呼び出されたときに正しいデータを取得する方法が必要です。章番号の場合、これは にあり\thechapter
、これはすでに使用されています。章のタイトルの場合、これを に保存し\currentchaptername
、\chapter
この名前が設定されるようにコマンドを変更します。別の(たとえば短い)名前を使用する場合は、コマンドの\currentchaptername
直後に を再定義できます\chapter
。参照コマンド では\myref
、最初に を発行してラベル データを取得し\zrefused
、次に を\zref@extract
発行して特定のフィールドを取得します。
chapter
方程式の相対番号をs ではなく s内に変更しましたsection
。そうしないと、ラベルが重複します。各セクションでリセットしたい場合は、代わりに関連するセクション タイトルも参照する必要があり、直接使用できる可能性がありますzref-titleref
。
上記のコードは、番号付きの章のタイトルのみを取得します。必要に応じて、同様のパッチを\chapter*
viaに適用することも、そのような状況で直接自分で設定することもできます。@schapter
\currentchatpername