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

% that links to image floats jumps to the beginning
% of the float and not to its caption

% the pages of the TOC is numbered roman
% and a pdf-bookmark for the TOC is added
  \mainmatter }

% enables calculations

% increases the bottom float placement fraction

% avoids that floats are placed above its sections

% for footnotes in tables

% for linewrapping in urls

% for including sourcecode

% for centering captions

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


\caption{Computing Similarity Matrix}

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

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

\State answer $\leftarrow
        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@{}}{%

\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$
            \State answer[r][c] $\leftarrow$ $0.0$

\State \Return answer



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:

enter image description here

The piece of code relating to your braced array:

\State %
  0.0 & \ldots & 0.0 \\ \hline
  \vdots & \ddots & \vdots \\ \hline
  0.0 & \ldots & 0.0 \\ \hline
answer $\leftarrow
  \underbrace{\vrule width0pt depth \dimexpr\dp0 + .3ex\relax\copy0}_{|p_1|}%
  \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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.