# Array inside Algorithm with underbrace and sidebrace

I'm writing my master's thesis (using LyX's thesis template) and am trying to display an algorithm. Since LyX's algorithm support doesn't quite cut it for me, I've decided to go ERT with LaTeX code (sigh!).

One of the steps in the algorithm that I want to display creates a two-dimensional array; and rather than describe it or show real code, I figured I'd draw a 2D array. This is easily done.
The complicated part occurs when I try to put an underbrace to show the dimensionality of the array. I tried to follow this post, but I get the following error:

! Extra alignment tab has been changed to \cr.
<template> \endtemplate

l.80     }

? h
You have given more \span or & marks than there were
in the preamble to the \halign or \valign now in progress.
So I'll assume that you meant to type \cr instead.


Now, since I'm using LyX, I can't really force it to ignore and continue (this particular error comes from pdflatexing my MWE on the command line, posted below). What I really want to do is add an underbrace to span all columns with the inline math $|p_1|$; I also need a brace that spans all rows with the inline math $|p_1|$ (This is a square array, with $|p_1|$ many rows and columns).

I'd appreciate any help in solving this issue.

Minimal Working Example:

\documentclass{IEEEtran} %using this documentClass just for the MWE

% increases link area for cross-references and autoname them
\AtBeginDocument{\renewcommand{\ref}[1]{\mbox{\autoref{#1}}}}

% that links to image floats jumps to the beginning
% of the float and not to its caption
\usepackage{hyperref}
\usepackage[figure]{hypcap}

% the pages of the TOC is numbered roman
% and a pdf-bookmark for the TOC is added
\let\myTOC\tableofcontents
\renewcommand\tableofcontents{%
\frontmatter
\pdfbookmark[1]{\contentsname}{}
\myTOC
\mainmatter }

% enables calculations
\usepackage{calc}

% increases the bottom float placement fraction
\renewcommand{\bottomfraction}{0.5}

% avoids that floats are placed above its sections
\let\mySection\section\renewcommand{\section}{\suppressfloats[t]\mySection}

% for footnotes in tables
\usepackage{footnote}

% for linewrapping in urls
\usepackage{url}

% for including sourcecode
\usepackage{listings}

% for centering captions
\usepackage{caption}

\usepackage{algorithm, algpseudocode}
\usepackage{amsmath, amssymb}

\begin{document}

\begin{algorithm}
\caption{Computing Similarity Matrix}
\label{alg:simMatrix}
\begin{algorithmic}[1]

\For {i $\leftarrow$ 1 ... max($|p_2| - |p_1|$, 0)}
\State $p_1 \leftarrow p_1 \bigcup$ NULL
\EndFor

\For {i $\leftarrow$ 1 ... max($|p_2| - |p_1|$, 0)}
\State $p_2 \leftarrow p_2 \bigcup$ NULL
\EndFor

\State answer $\leftarrow \begin{array}{|c|c|c|} \hline 0.0 & \ldots & 0.0 \\ \hline \vdots & \ddots & \vdots \\ \hline 0.0 & \ldots & 0.0 \\ \hline % don't forget to add underbrace and sidebrace to indicate height and width of the table. \multicolumn{1}{c}{} & \multicolumn{1}{c}{} & \multicolumn{2}{@{}l@{}}{% \raisebox{.5\normalbaselineskip}{% \rlap{$\underbrace{\hphantom{\mbox{0.0\hspace*{\dimexpr4\arraycolsep+\arrayrulewidth}0.0}}}_{\ell}$}}% } \end{array}$

\For {r $\leftarrow$ 1 ... $|p_1|-1$}
\For {c $\leftarrow$ 1 ... $|p_1|-1$}
\If {$p_1$[$r-1$] == $p_2$[$c-1$]}
\State answer[r][c] $\leftarrow$ answer[r-1][c-1] $+1$
\Else
\State answer[r][c] $\leftarrow$ $0.0$
\EndIf
\EndFor
\EndFor

\end{algorithmic}
\end{algorithm}

\end{document}


Technical Details:

• Mac OS X 10.7.5 (Lion)
• LyX Version 2.0.6
• MikTex Version 2.0 (Build 150)

You followed the wrong post. In fact, the linked post is used for spanning multiple columns using an \underbrace if it doesn't span the entire array. In your case, you want the \underbrace to span the entire array. So, instead, I've followed Side brace around image with underbrace, with some small modifications:

The piece of code relating to your braced array:

\State %
%
\sbox0{$\vcenter{\hbox{$\begin{array}{|c|c|c|}
\hline
0.0 & \ldots & 0.0 \\ \hline
\vdots & \ddots & \vdots \\ \hline
0.0 & \ldots & 0.0 \\ \hline
\end{array}$}}$}%
%
answer $\leftarrow \underbrace{\vrule width0pt depth \dimexpr\dp0 + .3ex\relax\copy0}_{|p_1|}% \left.\kern-\nulldelimiterspace \vphantom{\copy0} \right\rbrace \scriptstyle|p_1|$


Here are some considerations:

• Note the particular use of % signs. They are important to avoid line breaks, but I've kept them in to make the code a little more readable. See What is the use of percent signs (%) at the end of lines? for more on this.

• To mimic the style of the right brace "label" to that of the \underbrace, an explicit \scriptstyle is required.

All other details regarding the construction is similar to that listed in the above-linked answer.