- numbered rows (left)
- grey background
- horizontal line at top and bottom
- captionlabel is Algorithm instead of Table
- use a new counter instead of the table counter
I have succeeded doing this with the following code:
Code: Select all
\documentclass[11pt]{book}
\usepackage{caption}
\usepackage{float}
\usepackage{colortbl}
\definecolor{gray}{rgb}{0.898039215686,0.898039215686,0.898039215686}
\newcounter{rowno}
\setcounter{rowno}{0}
\newcounter{algorithm}
\setcounter{algorithm}{0}
\newcounter{tabular}
\setcounter{tabular}{0}
\captionsetup{labelfont=bf}
\newcommand{\tab}{\hspace{12pt}}
\newcolumntype{g}{>{\columncolor{gray}}l}
\newenvironment{algorithm}{\captionsetup{tablename=Algorithm,list=no}\setcounter{tabular}{\value{table}}\setcounter{table}{\value{algorithm}}\begin{table}[H]\centering}{\end{table}\captionsetup{tablename=Table}\setcounter{table}{\value{tabular}}\addtocounter{algorithm}{1}}
\newenvironment{alg}{\renewcommand{\arraystretch}{1.25}\begin{tabular}{>{\stepcounter{rowno}\therowno:\tab}g}\hline}{\hline\end{tabular}}
\begin{document}
\begin{algorithm}
\caption{Newton-Raphson iteration}
\begin{alg}
Initialise $\mathbf{w}^0$, $\mathbf{r}^0$, tol\\
Set k=0\\
\textbf{while} $\mathbf{r}^k>$ tol \textbf{do}\\
\tab Compute the residual $\mathbf{r}^k=\mathcal{R}\left(\mathbf{w}^k\right)$\\
\tab Compute the Jacobian $\frac{\partial\mathcal{R}}{\partial\mathbf{w}^k}$\\
\tab Solve the system $\frac{\partial\mathcal{R}}{\partial\mathbf{w}^k}\Delta\mathbf{w}^k=-\mathbf{r}^k$\\
\tab Compute the new iterate $\mathbf{w}^{k+1}=\mathbf{w}^k+\Delta\mathbf{w}^k$\\
$\mathbf{end}$\\
\end{alg}
\end{algorithm}
\end{document}
I have two questions:
1 How should I create a list of algorithms (similar to a list of tables, but then only with algorithm entries?
2 Is there an easier way to construct the algorithm environment?