Graphics, Figures & TablesCreating a new table-ish environment

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
CaluTeX
Posts: 1
Joined: Sat Feb 25, 2012 9:23 pm

Creating a new table-ish environment

Post by CaluTeX »

Basically what I want is to create a table-ish environment with the following properties:

- 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}
The algorithm environment is based on the table environment with some extras in order to fulfill my wishes. However as it basically still is a table environment, it shows up in my list of tables. I excluded the algorithm environment as entries through the "list=no" option. However I now also want to create a list of algorithms.

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?
Last edited by Stefan Kottwitz on Sat Feb 25, 2012 10:12 pm, edited 1 time in total.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

Learn LaTeX easily with newest books:

The LaTeX Beginner's Guide: 2nd edition and perfect for students writing a thesis

The LaTeX Cookbook: 2nd edition full of practical examples for mathematics, physics, chemistry, and more

LaTeX Graphics with TikZ: the first book about TikZ for perfect drawings in your LaTeX thesis

User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Creating a new table-ish environment

Post by cgnieder »

Question 1 is solved by defining a new floating environment »algorithm«, for example by using the floatrow package.

Code: Select all

\documentclass{article}
\usepackage{caption,floatrow,colortbl,mathtools}

\DeclareNewFloatType{algorithm}{placement=ht,name=Algorithm,fileext=loa}
\floatsetup[algorithm]{capposition=top}
\captionsetup[algorithm]{labelfont=bf}

\definecolor{gray}{rgb}{0.898039215686,0.898039215686,0.898039215686}

\newcounter{rowno}
\setcounter{rowno}{0}
\newcolumntype{g}{>{\columncolor{gray}}l}
\newenvironment{alg}{%
  \renewcommand{\arraystretch}{1.25}%
  \begin{tabular}{>{\stepcounter{rowno}\therowno:\tab}g}\hline}
  {\hline\end{tabular}}

\newcommand{\tab}{\hspace{12pt}}


\begin{document}

\listoftables

\listof{algorithm}{List of Algorithms}

\begin{table}[h]
 \caption{Some table}
 some table material
\end{table}


\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}
algorithm.png
algorithm.png (25.61 KiB) Viewed 3043 times
Regards
site moderator & package author
Post Reply