かすみん日記

暇なときに何か喋ります

【LaTeX】数式番号にセクション番号を入れる(付録の修正についても)

数式番号にセクション番号を入れる方法を述べます。

標準(jsarticle.cls)なら、数式番号は全セクションを通して(1), (2), ...のように出力されるかと思います。

これにセクション番号を入れて、(1.1), (1.2), ..., (2.1), ...のように出力されるようにします。

ついでに、数式番号だけじゃなくて図や表の番号もそういうスタイルに変更します。

環境

バージョン違っても多分大丈夫だと思いますが、想定している文書クラスが違うとダメかもしれないです。

  • TeXLive 2019
  • uplatex + dvipdfmx
  • 文書クラス:jsarticle.cls

方法

プリアンブル(\documentclass\begin{document}の間)に下記コードを追加します:

\makeatletter
\@addtoreset{equation}{section}
\@addtoreset{figure}{section}
\@addtoreset{table}{section}
\makeatother

\renewcommand{\theequation}{\thesection.\arabic{equation}}
\renewcommand{\thefigure}{\thesection.\arabic{figure}}
\renewcommand{\thetable}{\thesection.\arabic{table}}

これでokと言いたいところですが、これでは\appendix以降の付録のセクション中では、数式番号が(付録A.1)のように出力されてしまいます。

数式番号には付録という文字を入れたくないので、jsarticle.clsで定義されている\appendixコマンドを改造します。

ちなみに、jsarticle.clsの場所が知りたいときは、ターミナルでkpsewhich jsarticle.clsと打つとファイルのパスを教えてくれます。

さて、\appendixコマンドを以下のように再定義します。 プリアンブルに追加してください。

\makeatletter
\renewcommand{\appendix}{\par
  \setcounter{section}{0}%
  \setcounter{subsection}{0}%
  \gdef\presectionname{\appendixname}%
  \gdef\postsectionname{}%
  \gdef\thesection{\presectionname\@Alph\c@section\postsectionname}%
  \gdef\thesubsection{\@Alph\c@section.\@arabic\c@subsection}%
  %% 追加
  \renewcommand{\theequation}{\@Alph\c@section.\arabic{equation}}%
  \renewcommand{\thefigure}{\@Alph\c@section.\arabic{figure}}%
  \renewcommand{\thetable}{\@Alph\c@section.\arabic{table}}%
}
\makeatother

これで、付録のセクション中での数式番号は(A.1), (A.2), ...のようになります。

\thesectionを再定義してしまうと、見出しや目次などで付録の文字が表示されなくなってしまうので注意です。

また、もう一つ補足として、セクションが無い場合には、数式番号が(0.1), (0.2), ...のように、セクション番号が0と扱われて出力されます。

1ページのアブストなどの文書ではセクションは使わないので、そのときは元に戻したいです。

プリアンブルをstyファイルにしていろんな文書で使い回してる人には、if文でなんとか条件分けして欲しいところです。

ということで、以下のような修正案を提示します:

\renewcommand{\theequation}{%
  \ifnum\thesection>0
    \thesection.\arabic{equation}%
  \else
    \arabic{equation}%
  \fi
}

セクションがある(セクション番号が0より大きい)ときにだけ、数式番号にセクション番号を追加します。

行末の%は取っちゃダメです。余計なスペースが入ります。

まとめとサンプル

以上でもう文句はないかと思います。

最後に全部まとめた、タイプセット可能なサンプルコードを書いておきます。

%% uplatex -> dvipdfmx
\documentclass[uplatex]{jsarticle}

\usepackage{amsmath}

\makeatletter

\@addtoreset{equation}{section}
\@addtoreset{figure}{section}
\@addtoreset{table}{section}

\renewcommand{\theequation}{%
  \ifnum\thesection>0
    \thesection.\arabic{equation}%
  \else
    \arabic{equation}%
  \fi
}
\renewcommand{\thefigure}{%
  \ifnum\thesection>0
    \thesection.\arabic{figure}%
  \else
    \arabic{figure}%
  \fi
}
\renewcommand{\thetable}{%
  \ifnum\thesection>0
    \thesection.\arabic{table}%
  \else
    \arabic{table}%
  \fi
}

\renewcommand{\appendix}{\par
  \setcounter{section}{0}%
  \setcounter{subsection}{0}%
  \gdef\presectionname{\appendixname}%
  \gdef\postsectionname{}%
  \gdef\thesection{\presectionname\@Alph\c@section\postsectionname}%
  \gdef\thesubsection{\@Alph\c@section.\@arabic\c@subsection}%
  %% 追加
  \renewcommand{\theequation}{\@Alph\c@section.\arabic{equation}}%
  \renewcommand{\thefigure}{\@Alph\c@section.\arabic{figure}}%
  \renewcommand{\thetable}{\@Alph\c@section.\arabic{table}}%
}

\makeatother

\begin{document}

\section*{イントロ}
数式(\ref{eq:Euler})はオイラーの公式です(図\ref{fig:Euler}や表\ref{tab:Euler}を参照).
\begin{align}
  e^{i \theta} = \cos\theta + i \sin\theta
  \label{eq:Euler}
\end{align}
\begin{figure}
  \caption{オイラー}
  \label{fig:Euler}
\end{figure}
\begin{table}
  \caption{オイラー}
  \label{tab:Euler}
\end{table}

\section{にゃあ}
にゃお〜.数式(\ref{eq:nyaa})はにゃんこです(図\ref{fig:nyaa}や表\ref{tab:nyaa}を参照).
\begin{align}
  1 + 1 = 3
  \label{eq:nyaa}
\end{align}
\begin{figure}
  \caption{にゃあ}
  \label{fig:nyaa}
\end{figure}
\begin{table}
  \caption{にゃあ}
  \label{tab:nyaa}
\end{table}

\appendix
\section{にゃ〜ん}
付録にゃ.数式(\ref{eq:nyan})は付録のにゃんこです(図\ref{fig:nyan}や表\ref{tab:nyan}を参照).
\begin{align}
  1 + 1 = 3
  \label{eq:nyan}
\end{align}
\begin{figure}
  \caption{にゃ〜ん}
  \label{fig:nyan}
\end{figure}
\begin{table}
  \caption{にゃ〜ん}
  \label{tab:nyan}
\end{table}

\end{document}

タイプセット結果:

f:id:geniusium:20220208090946p:plain

以上です。お疲れさまでした。