Math & ScienceDiagonal overbraces in a matrix

Information and discussion about LaTeX's math and science related features (e.g. formulas, graphs).
Post Reply
benbean
Posts: 6
Joined: Thu Mar 11, 2010 9:40 pm

Diagonal overbraces in a matrix

Post by benbean »

I have looked on the web for an answer to this but have had no luck yet so hopefully someone can offer some help here.
I have the matrix below:

\begin{pmatrix}
x_{1,1} & x_{1,2} & \cdots & x_{1,j} \\
x_{2,1} & x_{2,2} & \cdots & x_{2,j} \\
\vdots & \vdots & \ddots & \vdots \\
x_{i,1} & x_{i,2} & \cdots & x_{i,j}
\end{pmatrix}

Is it possible to group the elements x_{1,1}, x_{2,2}, \ddots, x_{i,j} in a diagonal curly brace? Possibly in a form like \overbrace or \underbrace?

If this is possible could I then have this brace with text?

Thanks,
benbean

Recommended reading 2024:

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

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Diagonal overbraces in a matrix

Post by localghost »

I'm not aware of a solution with the known commands. But you can use pgf/tikZ and some of its libraries.

Code: Select all

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix,decorations.pathreplacing}

\begin{document}
  \begin{tikzpicture}[decoration={brace,amplitude=5pt}]
    \matrix (magic) [matrix of math nodes,left delimiter=(,right delimiter=)] {
      x_{1,1} & x_{1,2} & \cdots & x_{1,j} \\
      x_{2,1} & x_{2,2} & \cdots & x_{2,j} \\
      \vdots & \vdots & \ddots & \vdots \\
      x_{i,1} & x_{i,2} & \cdots & x_{i,j} \\
    };
    \draw[decorate,red] (magic-1-1.north) -- (magic-4-4.north) node[above=5pt,midway,sloped] {label};
  \end{tikzpicture}
\end{document}
The pstricks package offers similar capabilities.


Best regards and welcome to the board
Thorsten
benbean
Posts: 6
Joined: Thu Mar 11, 2010 9:40 pm

Diagonal overbraces in a matrix

Post by benbean »

Thanks a lot Thorsten exactly what I had in mind!

Just a small query - Is it possible to extend the curly brace so as to enclose the two end elements, namely the x_{1,1} and x_{i,j} elements? The solution you posted seems to have the brace stop over the 'midpoint' of each element.

I am very new to LaTeX so the learning curve is looking steep at the minute :D .

Thanks again
b
User avatar
gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

Diagonal overbraces in a matrix

Post by gmedina »

Hi,

you can fine-tune the coordinates; a little example based on localghost's code:

Code: Select all

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix,decorations.pathreplacing,calc}

\begin{document}
  \begin{tikzpicture}[decoration={brace,amplitude=5pt}]
    \matrix (magic) [matrix of math nodes,left delimiter=(,right delimiter=)] {
      x_{1,1} & x_{1,2} & \cdots & x_{1,j} \\
      x_{2,1} & x_{2,2} & \cdots & x_{2,j} \\
      \vdots & \vdots & \ddots & \vdots \\
      x_{i,1} & x_{i,2} & \cdots & x_{i,j} \\
    };
    \draw[decorate,red] ($(magic-1-1.north west)+(0.2,0.15)$) -- (magic-4-4.north east) node[above=5pt,midway,sloped] {label};
  \end{tikzpicture}
\end{document}
1,1,2,3,5,8,13,21,34,55,89,144,233,...
benbean
Posts: 6
Joined: Thu Mar 11, 2010 9:40 pm

Re: Diagonal overbraces in a matrix

Post by benbean »

Thanks gmedina, I am able to get what I am after with your suggestion.

Thank you guys for all your help. I was at a loss until I came here :)
benbean
benbean
Posts: 6
Joined: Thu Mar 11, 2010 9:40 pm

Re: Diagonal overbraces in a matrix

Post by benbean »

This is a bit off topic but how would I write this matrix in an equation style? If it is defined as A = <matrix>, how do I get it so that the "A = " part is centered inline with the matrix?

Any help is greatly received,
benbean
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Diagonal overbraces in a matrix

Post by localghost »

benbean wrote:This is a bit off topic but how would I write this matrix in an equation style? If it is defined as A = <matrix>, how do I get it so that the "A = " part is centered inline with the matrix? [...]
I hope that you only mean the alignment with respect to the equal sign and not how you can get it into a math environment in general. Since the origin of the tikzpicture environment is put at the lower left corner by default, it needs an additional option.

Code: Select all

\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{matrix,decorations.pathreplacing,calc}

\begin{document}
\[
  \boldsymbol{A}=
  \begin{tikzpicture}[decoration={brace,amplitude=5pt},baseline=(current bounding box.west)]
    \matrix (magic) [matrix of math nodes,left delimiter=(,right delimiter=)] {
      x_{1,1} & x_{1,2} & \cdots & x_{1,j} \\
      x_{2,1} & x_{2,2} & \cdots & x_{2,j} \\
      \vdots & \vdots & \ddots & \vdots \\
      x_{i,1} & x_{i,2} & \cdots & x_{i,j} \\
    };
    \draw[decorate,red] ($(magic-1-1.north west)+(0.2,0.15)$) -- (magic-4-4.north east) node[above=5pt,midway,sloped] {label};
  \end{tikzpicture}
\]
\end{document}
benbean
Posts: 6
Joined: Thu Mar 11, 2010 9:40 pm

Re: Diagonal overbraces in a matrix

Post by benbean »

Yes it was only the alignment I was trying to fix. Your code worked again so many thanks again! It's all looking how I intended, thanks for all your help.

b :)
Post Reply