将算法中的编号从 切换\arabic
为\arabic\alpha
特定行
在下面的代码中,我想将 更改#3
为3a
并将#4
更改为3b
。然后数字5
应该是#4
。基本上,我只想更改3-4
为3a
和3b
然后恢复正常编号。
以下是代码:
\documentclass{article}
\usepackage{amsmath, amsfonts, amssymb, amsthm, bm}
\usepackage{commath}
\usepackage{xcolor}
\usepackage[most]{tcolorbox}
\usepackage{tikz}
\usetikzlibrary{backgrounds}
\usepackage[linesnumbered,ruled,vlined]{algorithm2e}
\newcommand\mycommfont[1]{\footnotesize\ttfamily\textcolor{blue}{#1}}
\SetCommentSty{mycommfont}
\definecolor{mybluei}{RGB}{0,173,239}
\newcommand*\circled[1]{\tikz[baseline=(char.base)]{
\node[shape=circle,draw,inner sep=2pt] (char) {#1};}}
\makeatletter
\def\munderbar#1{\underline{\sbox\tw@{$#1$}\dp\tw@\z@\box\tw@}}
\makeatother
\makeatletter
\newcommand{\nosemic}{\renewcommand{\@endalgocfline}{\relax}}% Drop semi-colon ;
\newcommand{\dosemic}{\renewcommand{\@endalgocfline}{\algocf@endline}}% Reinstate semi-colon ;
\newcommand{\pushline}{\Indp}% Indent
\newcommand{\popline}{\Indm\dosemic}% Undent
\let\oldnl\nl% Store \nl in \oldnl
\newcommand{\nonl}{\renewcommand{\nl}{\let\nl\oldnl}}% Remove line number for one line
\makeatother
\begin{document}
\colorbox{mybluei!05}{\color{black}
\begin{minipage}{\textwidth}
\SetAlgoLined
\SetNlSty{textbf}{}{:}
\begin{algorithm}[H]
\DontPrintSemicolon
\nonl{Assume that there is a set of $M$ face images}
\begin{equation*}
\left\{\Gamma_{1}, \Gamma_{2},...,\Gamma_{M}, \right\}
\end{equation*}
Each image has the size of $r \times c$\\
$r\rightarrow \# ~\text{of rows}\quad c \rightarrow \#~ \text{of columns}$\\
Averaging face images
\begin{equation*}
\Psi = \dfrac{1}{M}\sum_{i=1}^{M} \Gamma_{i}
\end{equation*}\\
Remove the average image from all given ones
\begin{equation*}
\circled{H}_{i} = \Gamma_{i} - \Psi \quad i = 1,2,...,M
\end{equation*}\\
Convert $\underset{\text{dim}}{ r \times c}$ images into $\underset{\text{dim}}{ \rho \times 1}$ vectors, where $\rho = r \times c$ using a raster scan.
\begin{equation*}
\hspace{1cm}\circled{H}_{i} \Rightarrow \bm{\munderbar{x}}_{i}\hspace{2cm}i=1,2,...,M
\end{equation*}\\
Stack $M$ vectors into a matrix
\begin{equation*}
X = \left[\bm{\munderbar{x}}_{1},\bm{\munderbar{x}}_{2},...,\bm{\munderbar{x}}_{M} \right]
\end{equation*}
where $X$ is a $\rho \times M$ matrix.\\
Compute matrix R from $X$.
\begin{equation*}
R=\frac{1}{M}X'X\quad (\text{much smaller than $XX'$})
\end{equation*}
where $R$ is $M\times M$ \hspace{1cm} $M\ll P$
\caption{Principal Component Analysis (PCA) Training}
\end{algorithm}
\end{minipage}}
\end{document}
答案1
在我看来,最好使用计数器SubAlgoLine
和条件\theAlgoLine
(打印行号)来实现
\documentclass{article}
\usepackage{amsmath, amsfonts, amssymb, amsthm, bm}
\usepackage{commath}
\usepackage{xcolor}
\usepackage[most]{tcolorbox}
\usepackage{tikz}
\usetikzlibrary{backgrounds}
\usepackage[linesnumbered,ruled,vlined]{algorithm2e}
\newcommand\mycommfont[1]{\footnotesize\ttfamily\textcolor{blue}{#1}}
\SetCommentSty{mycommfont}
\definecolor{mybluei}{RGB}{0,173,239}
\newcommand*\circled[1]{\tikz[baseline=(char.base)]{
\node[shape=circle,draw,inner sep=2pt] (char) {#1};}}
\makeatletter
\def\munderbar#1{\underline{\sbox\tw@{$#1$}\dp\tw@\z@\box\tw@}}
\makeatother
\makeatletter
\newcommand{\nosemic}{\renewcommand{\@endalgocfline}{\relax}}% Drop semi-colon ;
\newcommand{\dosemic}{\renewcommand{\@endalgocfline}{\algocf@endline}}% Reinstate semi-colon ;
\newcommand{\pushline}{\Indp}% Indent
\newcommand{\popline}{\Indm\dosemic}% Undent
\let\oldnl\nl% Store \nl in \oldnl
\newcommand{\nonl}{\renewcommand{\nl}{\let\nl\oldnl}}% Remove line number for one line
\let\@@thealgoline\theAlgoLine
\newif\ifuseothernumbering
\newcounter{SubAlgoLine}
\renewcommand{\theSubAlgoLine}{\@@thealgoline\alph{SubAlgoLine}}
\renewcommand{\theAlgoLine}{%
\ifuseothernumbering
\ifnum\c@AlgoLine=3
\protect\refstepcounter{SubAlgoLine}
\theSubAlgoLine%
\else
\ifnum\c@AlgoLine=4
\protect\refstepcounter{SubAlgoLine}
\addtocounter{AlgoLine}{\m@ne}%
\theSubAlgoLine%
\global\useothernumberingfalse
\else
\@@thealgoline%
\fi
\fi
\else
\@@thealgoline%
\fi
}
\makeatother
\begin{document}
\colorbox{mybluei!05}{\color{black}
\begin{minipage}{\textwidth}
\SetAlgoLined
\SetNlSty{textbf}{}{:}
\useothernumberingtrue
\begin{algorithm}[H]
\DontPrintSemicolon
\nonl{Assume that there is a set of $M$ face images}
\begin{equation*}
\left\{\Gamma_{1}, \Gamma_{2},...,\Gamma_{M}, \right\}
\end{equation*}
Each image has the size of $r \times c$\\
$r\rightarrow \# ~\text{of rows}\quad c \rightarrow \#~ \text{of columns}$\\
Averaging face images
\begin{equation*}
\Psi = \dfrac{1}{M}\sum_{i=1}^{M} \Gamma_{i}
\end{equation*}\\
Remove the average image from all given ones
\begin{equation*}
\circled{H}_{i} = \Gamma_{i} - \Psi \quad i = 1,2,...,M
\end{equation*}\\
Convert $\underset{\text{dim}}{ r \times c}$ images into $\underset{\text{dim}}{ \rho \times 1}$ vectors, where $\rho = r \times c$ using a raster scan.
\begin{equation*}
\hspace{1cm}\circled{H}_{i} \Rightarrow \bm{\munderbar{x}}_{i}\hspace{2cm}i=1,2,...,M
\end{equation*}\\
Stack $M$ vectors into a matrix
\begin{equation*}
X = \left[\bm{\munderbar{x}}_{1},\bm{\munderbar{x}}_{2},...,\bm{\munderbar{x}}_{M} \right]
\end{equation*}
where $X$ is a $\rho \times M$ matrix.\\
Compute matrix R from $X$.
\begin{equation*}
R=\frac{1}{M}X'X\quad (\text{much smaller than $XX'$})
\end{equation*}
where $R$ is $M\times M$ \hspace{1cm} $M\ll P$
\caption{Principal Component Analysis (PCA) Training}
\end{algorithm}
\end{minipage}}
\end{document}