Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I am trying to define a matrix in segments by enclosing the entries of the matrix in boxes, and also using the open and closed braces to label those segments. So far I have developed the following code to do this.

\documentclass[a4,12pt]{report}

\usepackage{multirow}

\begin{document}

\[
  A =
    \left(
      \begin{array}{rrrrrl}
          \cline{2-4} \cline{5-5}
        \multirow{2}{*}{$B \{$} &
          \multicolumn{1}{|r}{1} & 2 & \multicolumn{1}{r|}{-4} & \multicolumn{1}{|r|}{1} & \multirow{2}{*}{$\} \hat{\mathbf{b}}^{T}$} \\
          & \multicolumn{1}{|r}{2} & -5 & \multicolumn{1}{r|}{-3} & \multicolumn{1}{|r|}{-3} & \\
          \cline{2-4} \cline{5-5}
        \hat{\mathbf{c}} \{  & \multicolumn{1}{|r}{-6} & -3 & \multicolumn{1}{r|}{0} & \multicolumn{1}{|r|}{5} & \} a_{n-1,n} \\
          \cline{2-4} \cline{5-5}
        \hat{\mathbf{d}} \{ & \multicolumn{1}{|r}{-6} &  7 & \multicolumn{1}{r|}{0} & \multicolumn{1}{|r|}{-8} & \} a_{n,n} \\
          \cline{2-4} \cline{5-5}
      \end{array}
    \right)
\]


\end{document}

This produces the following output:

enter image description here

What I want to do is expand the left and right brace in the first and last columns to line up with the rows that have been segmented. One way of doing this is to use the \left\{ and \right\} LaTeX commands. The problem is that when I do this, I get the following LaTeX error:

! Missing \right.  inserted.
<inserted text>
                \right .
l.12         \multirow{2}{*}{$B \left\{$}
                                          &
?

Also, what I would like to do is separate the segments using individual boxes, rather than adjoining boxes as above. Any ideas on how to do this would be very much appreciated.

share|improve this question
    
Have a look at the package blkarray. –  Marco Daniel Apr 20 '12 at 13:07
    
@MarcoDaniel - Thanks for that. I presume that resolves my second problem? –  Bill Apr 20 '12 at 13:09
    
would you be open to using tikz for the boxing? –  cmhughes Apr 20 '12 at 13:26
    
@cmhughes - Indeed I would! I am currently using tikz for some of the lattice graphs. –  Bill Apr 20 '12 at 13:51
    
the "headroom" between digits and the rules above them is terribly uneven and mostly too tight. the array package provides the command \extrarowheight to make adjustments of this sort; alternatively, a \vphantom{...} with some suitable content (maybe $\hat X$) could be applied in the rows that need it. –  barbara beeton Apr 20 '12 at 14:00

3 Answers 3

up vote 9 down vote accepted

I took @mico's answer and added hhline

enter image description here

\documentclass[a4paper,12pt]{report}
\usepackage{multirow,hhline}
\begin{document}
  \[
  A =
  \left(
  \begin{array}{r|rrr||r|l}
    \hhline{~|---||-|~}
    \multirow{2}{*}{$B \bigg\{$} 
    & 1 & 2 & -4 & 1 & 
    \multirow{2}{*}{$\bigg\} \hat{\mathbf{b}}^{T}$} \\
    & 2 & -5 & -3 & -3 & \\
    \hhline{~:===::=:~}
    \hat{\mathbf{c}} \{  & -6 & -3 & 0 & 5 & \} a_{n-1,n} \\
    \hhline{~:===::=:~}
    \hat{\mathbf{d}} \{ & -6 &  7 & 0 & -8 & \} a_{n,n} \\
    \hhline{~|---||-|~}
  \end{array}
  \right)
  \]
\end{document}
share|improve this answer
    
@Bill: And you might want to replace \begin{array}{r|rrr||r|l} by \begin{array}{@{}r@{}|rrr|r|@{}l@{}} to get rid of the horizontal space between the braces and the boxes. –  Stephen Apr 20 '12 at 14:41
    
@DavidCarlisle - Thanks for that David and Stephen! It seems to be the simplest solution that allows me to do both! –  Bill Apr 20 '12 at 22:11

Here's a solution using the tried and tested \tikzmark idea, first show cased in Andrew Stacey's answer to

Adding a large brace next to a body of text

enter image description here

The code below uses a version modified from Peter Grill's answer to

Box around a few items in an itemize environment

My solution is a little bit fiddly- there's a bit of manual column and row spacing, and a couple of uses of \vphantom so that instead of using \bigg you can use \left\{...\right. but the output is (I hope) quite pleasing.

\documentclass[a4,12pt]{report}

\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage{multirow}

\newcommand{\DrawBox}[3][]{%
    \tikz[overlay,remember picture]{
        \coordinate (RightPoint) at (#3.east);
    \draw[red,#1]
      ($(#2)+(-.5em,.9em)$) rectangle
      ($(RightPoint)+(0.2em,-0.3em)$);}
}

\newcommand{\tikzmark}[1]{\tikz[overlay,remember picture] \node (#1) {};}

\begin{document}

\[
  A =
    \left(
    \begin{array}{r@{\hspace{.2cm}}c@{}rrr@{\hspace{.5cm}}r@{}rl}
       \multirow{2}{*}{$B \left\{\vphantom{\begin{array}{c}1\\2\end{array}}\right.$} 
                                &\tikzmark{left1}   & 1   & 2  & -4                     &\tikzmark{left3}   & 1  & 
                                \multirow{2}{*}{$\left.\vphantom{\begin{array}{c}1\\2\end{array}}\right\} \hat{\mathbf{b}}^{T}$} \\
                                &                   & 2   & -5 & -3\tikzmark{right1}    &                   &-3 \tikzmark{right3}   & \\[5pt]
        \hat{\mathbf{c}} \{     &\tikzmark{left2}   & -6  & -3 & 0 \tikzmark{right2}    &\tikzmark{left4}   & 5  \tikzmark{right4}  &\}a_{n-1,n} \\[5pt]
        \hat{\mathbf{d}} \{     &\tikzmark{left5}   & -6  &  7 & 0 \tikzmark{right5}    &\tikzmark{left6}   & -8 \tikzmark{right6}  & \} a_{n,n} \\
      \end{array}
    \right)
\]

\DrawBox[thick,green,fill=yellow,fill opacity=0.1]{left1}{right1}
\DrawBox[thick,blue]{left2}{right2}
\DrawBox[thick,red]{left3}{right3}
\DrawBox[thick,purple]{left4}{right4}
\DrawBox[thick,yellow]{left5}{right5}
\DrawBox[thick,orange]{left6}{right6}

\end{document}
share|improve this answer
    
Thanks for that! It does exactly what I want and more! For the moment, though, I will stick to David's solution as it seems much simpler, and easier to adapt to my example. You might, however, want to post your solution to http://www.texample.net/tikz/, as they are always looking for TikZ examples. I believe the page is moderated by Stefan Kottwitz, who is also a member of this group. –  Bill Apr 20 '12 at 22:28
    
It might be a good idea to include the \DrawBox commands inside the math environment rather than following it. I had to do this when the "array" was the last thing included on a given page; the coloured boxes were drawn on the next page in that case - unless I include the \DrawBox instructions inside the \[ ... \] –  André May 7 '12 at 19:51
    
@André thanks, that's a good idea. I'll make an edit in a couple of days –  cmhughes May 7 '12 at 20:32

You could use the explicit size statement \bigg in order to set the size of the left and right curly braces, as is shown in the following modified form of your MWE. To get the arrays display with a little gap while using the \cline command, it's necessary to break them up into two separteI've also simplified the code to get rid of most of the \multicolumn statements and combining the various paired \cline statements.

\documentclass[a4paper,12pt]{report}
\usepackage{multirow}
\begin{document}      
  \[
  A =
  \left(
  \begin{array}{r|rrr|}
    \cline{2-4} 
    \multirow{2}{*}{$B \bigg\{$} 
    & 1 &  2 & -4  \\ 
    & 2 & -5 & -3 \\
    \cline{2-4} 
    \hat{\mathbf{c}} \{  & -6 & -3 & 0 \\
    \cline{2-4}
    \hat{\mathbf{d}} \{  & -6 &  7 & 0 \\
    \cline{2-4} 
  \end{array}
  \right. \kern-3pt \left. % use \kern-3pt to reduce distance between arrays
  \begin{array}{|r|l}
    \cline{1-1}
     1 & \multirow{2}{*}{$\bigg\} \hat{\mathbf{b}}^{T}$} \\
    -3 & \\
    \cline{1-1}
     5 & \} a_{n-1,n} \\
    \cline{1-1}
    -8 & \} a_{n,n} \vphantom{\hat{\mathbf{d}}}\\  
        % the \vphantom is needed to equalize the heights of the 
        % third rows across the two arrays
    \cline{1-1}
  \end{array}
  \right)
  \]
\end{document}

enter image description here

share|improve this answer
    
Thanks for that. Any idea on how I would separate the boxes? –  Bill Apr 20 '12 at 13:29
    
Do you mean the first three number columns versus the fourth column? –  Mico Apr 20 '12 at 13:43
    
I mean all of them. That is, each segment would be separated by its own box if that makes any sense. –  Bill Apr 20 '12 at 13:49
    
Bill -- I've amended my code to produce two separate arrays. Hope this is what you're looking for. –  Mico Apr 20 '12 at 14:00
    
I added a version of this using hhline –  David Carlisle Apr 20 '12 at 14:13

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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