我目前正在编写一些 MIP 求解器基准库的文档。这包括每种问题类型的描述。例如,对于 binpacking,我有
\section{Bin Packing Problem}\label{bpp}
\subsection{Formulation}
We are given $n$ items in an item set $I$ with weights $w_i$, with $i \in I$.
The items shall be packed into bins (also $n$ many, but we use $B$ as the set
of bins for the sake of readability) with capacity $C$ such that the number of
bins used is minimal. We use two types of variables:
\begin{itemize}
\item $x_{ij} \in \{0, 1\}$ should be $1$ iff item $i$ is packed into bin $j$.
\item $y_j \in \{0, 1\}$ should be $1$ iff bin $j$ is used.
\end{itemize}
\begin{align}
\text{minimize } & \sum_{j \in B} y_j \label{bpp:obj}\\
\text{subject to } & \sum_{j \in B} x_{ij} = 1 & i \in I \label{bpp:setpp}\\
& \sum_{i \in I} w_i x_{ij} \leq C y_j & j \in B \label{bpp:capacity}\\
& x_{ij} \in \{0, 1\} & i \in I, j \in B\\
& y_j \in \{0, 1\} & j \in B
\end{align}
如您所见,目标函数和每个约束都有一个标签。现在,假设这个装箱部分是第 1 部分。因此,标签被命名为 (1.1)、(1.2)、(1.3)。但我希望它们有一个像 (BPP.1)、(BPP.2)、(BPP.3) 这样的名称,即底层问题的一些常见缩写。但是,该部分本身仍应有一个用于目录和其他参考资料的编号。
有什么建议可以实现这一点吗?
答案1
您可以定义样式设置\theequation
:
\renewcommand{\theequation}{BPP.\arabic{equation}}
这将产生名为(BPP.1)
、(BPP.2)
等的标签。
要在文档中使用它,我建议使用宏来设置样式,如下所示:
\newcounter{oldeq}
\newcommand{\setpreeqno}[1]{% set text before equation number
\ifx\preeqno\empty% if no preeqno was given before
\def\preeqno{#1}% set to argument
\ifx\preeqno\empty\else% if an argument was given
\renewcommand{\theequation}{#1.\arabic{equation}}% set to "arg.#"
\setcounter{oldeq}{\value{equation}}% save equation counter
\setcounter{equation}{0}% reset locally to start with "arg.1"
\fi%
\else%
\def\preeqno{#1}% set to argument
\ifx\preeqno\empty% if no argument was given
\renewcommand{\theequation}{\arabic{equation}}% reset to default arabic number
\setcounter{equation}{\value{oldeq}}% continue counting
\else%
\renewcommand{\theequation}{#1.\arabic{equation}}% set to "arg.#"
\setcounter{equation}{0}% reset locally to start with "arg.1"
\fi%
\fi%
\ignorespaces%
}
或者在完整的 MWE 中:
\documentclass{article}
\usepackage{amsmath}
\newcounter{oldeq}
\newcommand{\setpreeqno}[1]{% set text before equation number
\ifx\preeqno\empty% if no preeqno was given before
\def\preeqno{#1}% set to argument
\ifx\preeqno\empty\else% if an argument was given
\renewcommand{\theequation}{#1.\arabic{equation}}% set to "arg.#"
\setcounter{oldeq}{\value{equation}}% save equation counter
\setcounter{equation}{0}% reset locally to start with "arg.1"
\fi%
\else%
\def\preeqno{#1}% set to argument
\ifx\preeqno\empty% if no argument was given
\renewcommand{\theequation}{\arabic{equation}}% reset to default arabic number
\setcounter{equation}{\value{oldeq}}% continue counting
\else%
\renewcommand{\theequation}{#1.\arabic{equation}}% set to "arg.#"
\setcounter{equation}{0}% reset locally to start with "arg.1"
\fi%
\fi%
\ignorespaces%
}
\begin{document}
\thispagestyle{empty}
\section{Bin Packing Problem}\label{bpp}\setpreeqno{BPP}
\subsection{Formulation}
We are given $n$ items in an item set $I$ with weights $w_i$, with $i \in I$.
The items shall be packed into bins (also $n$ many, but we use $B$ as the set
of bins for the sake of readability) with capacity $C$ such that the number of
bins used is minimal. We use two types of variables:
\begin{itemize}
\item $x_{ij} \in \{0, 1\}$ should be $1$ iff item $i$ is packed into bin $j$.
\item $y_j \in \{0, 1\}$ should be $1$ iff bin $j$ is used.
\end{itemize}
\begin{align}
\text{minimize } & \sum_{j \in B} y_j \label{bpp:obj}\\
\text{subject to } & \sum_{j \in B} x_{ij} = 1 & i \in I \label{bpp:setpp}\\
& \sum_{i \in I} w_i x_{ij} \leq C y_j & j \in B \label{bpp:capacity}\\
& x_{ij} \in \{0, 1\} & i \in I, j \in B\\
& y_j \in \{0, 1\} & j \in B
\end{align}
This reference looks like \eqref{bpp:obj}.
\end{document}
编辑我通过处理全局计数器改进了代码(受到@egreg 的回答的启发)
答案2
subequations
使用相同基础设施的变体。
\documentclass{article}
\usepackage{amsmath}
\newenvironment{labeledequations}[1]{%
\setcounter{parentequation}{\value{equation}}%
\setcounter{equation}{0}%
\def\theequation{#1.\arabic{equation}}%
\ignorespaces
}{%
\setcounter{equation}{\value{parentequation}}%
\ignorespacesafterend
}
\begin{document}
\section{Bin Packing Problem}\label{bpp}
\subsection{Formulation}
We are given $n$ items in an item set $I$ with weights $w_i$, with $i \in I$.
The items shall be packed into bins (also $n$ many, but we use $B$ as the set
of bins for the sake of readability) with capacity $C$ such that the number of
bins used is minimal. We use two types of variables:
\begin{itemize}
\item $x_{ij} \in \{0, 1\}$ should be $1$ iff item $i$ is packed into bin $j$.
\item $y_j \in \{0, 1\}$ should be $1$ iff bin $j$ is used.
\end{itemize}
\begin{labeledequations}{BPP}
\begin{align}
\text{minimize } & \sum_{j \in B} y_j
\label{bpp:obj} \\
\text{subject to } & \sum_{j \in B} x_{ij} = 1 && i \in I
\label{bpp:setpp} \\
& \sum_{i \in I} w_i x_{ij} \leq C y_j && j \in B
\label{bpp:capacity} \\
& x_{ij} \in \{0, 1\} && i \in I, j \in B
\\
& y_j \in \{0, 1\} && j \in B
\end{align}
\end{labeledequations}
The references to \eqref{bpp:obj} and \eqref{bpp:capacity} are correct.
\end{document}