5
\begin{flalign*}
&\left.
\begin{bmatrix}
x_{11} & \cdots & x_{1k} \\
\vdots & \ddots & \vdots \\
x_{n1} & \cdots & x_{nk} 
\end{bmatrix}
\right)~\text{$n$ observations}&
\end{flalign*}

enter image description here

I want to draw a round bracket under the matrix with text "k variables". I know how to draw a round bracket on the left and right side of the matrix, but I don't know how to draw it under the matrix.

CarLaTeX
  • 62,716

3 Answers3

3

Last Edit: [all three (forth just semi-automates second) ways]

\documentclass{article}

\usepackage{mathtools}
\usepackage{tikz}
\usepackage{anyfontsize}
\usetikzlibrary{arrows,matrix,positioning,decorations.pathreplacing,calc}


\makeatletter
\newcommand\getwidthofnode[2]{%
    \pgfextractx{#1}{\pgfpointanchor{#2}{east}}%
    \pgfextractx{\pgf@xa}{\pgfpointanchor{#2}{west}}% \pgf@xa is a length defined by PGF for temporary storage. No need to create a new temporary length.
    \addtolength{#1}{-\pgf@xa}%
}
\makeatother

\makeatletter
\newcommand\getheightofnode[2]{%
    \pgfextracty{#1}{\pgfpointanchor{#2}{north}}%
    \pgfextracty{\pgf@ya}{\pgfpointanchor{#2}{south}}% \pgf@xa is a length defined by PGF for temporary storage. No need to create a new temporary length.
    \addtolength{#1}{-\pgf@ya}%
}
\makeatother

\newlength\TestLength
\newlength\TestHeight
\begin{document}


% First way: No tikz but a \vphantom "trick" for the \right) 
First way:

$\underbrace{\begin{aligned}
\begin{bmatrix}
x_{11} & \cdots & x_{1k} \\
\vdots & \ddots & \vdots \\
x_{n1} & \cdots & x_{nk} 
\end{bmatrix}\end{aligned}
}_{k~\text{variables}}
\left.\vphantom{\begin{aligned}
\begin{bmatrix}
x_{11} & \cdots & x_{1k} \\
\vdots & \ddots & \vdots \\
x_{n1} & \cdots & x_{nk} 
\end{bmatrix}\end{aligned}}\right)~\text{$n$ observations}$
\vspace*{15pt}

% Second way: tikz and brace ( manual fix of shifting)
Second way:

\begin{tikzpicture}
   \node (m1) at (0,0) {$\begin{aligned}
\begin{bmatrix}
x_{11} & \cdots & x_{1k} \\
\vdots & \ddots & \vdots \\
x_{n1} & \cdots & x_{nk} 
\end{bmatrix}\end{aligned}$};
\draw [thick,decorate,decoration={brace,amplitude=6pt,mirror}] ([xshift=10pt]m1.south west) -- 
([xshift=-10pt]m1.south east) node[midway] (m2) {};
\node [anchor=north,yshift=-6pt] at (m2) {$k$ variables};
\draw[thick,decorate,decoration={brace,amplitude=6pt,mirror}] ([yshift=6pt]m1.south east) --
([yshift=-6pt]m1.north east) node[midway] (m3) {};
\node[anchor=west,xshift=6pt] at (m3) {$n$ observations};
\end{tikzpicture}
\vspace*{15pt}

% Third way: tikz and round bracket (using "\getwidthofnode" and "\getheightofnode" to automate procedure)
Third way: 

\begin{tikzpicture}
   \node (m1) at (0,0) {$\begin{aligned}
\begin{bmatrix}
x_{11} & \cdots & x_{1k} \\
\vdots & \ddots & \vdots \\
x_{n1} & \cdots & x_{nk} 
\end{bmatrix}\end{aligned}$};
\getwidthofnode{\TestLength}{m1}
\pgfmathsetmacro\myLength{\TestLength*0.7}
\node (m2)[yscale=0.5,rotate=-90,font=\fontsize{\myLength}{45}\selectfont] at (m1.south) {)};%yscale here is to reduce width of horizontal ")"
\node [anchor=north] at (m2) {$k$ variables};
\getheightofnode{\TestHeight}{m1}
\pgfmathsetmacro\myLength{\TestHeight*0.7}
\node (m3) [xscale=0.8,font=\fontsize{\myLength}{45}\selectfont] at ($(m1)+(\TestLength/2,0)$) {)};%xscale here is to reduce width of vertical ")"
\node (m4) at (m3) {\phantom{$n$ observations}};
\getwidthofnode{\TestLength}{m4}
\node at ($(m4)+(\TestLength/2,0)$) {$n$ observations};
\end{tikzpicture}
\vspace*{15pt}

% Fourth way: tikz and brace (using "\getwidthofnode" and "\getheightofnode" to semi-automate procedure)
Fourth way:

\begin{tikzpicture}
\xdef\MatrixToBraceXSouthScale{0.7}
\xdef\MatrixToBraceYEastScale{0.7}
   \node (m1) at (0,0) {$\begin{aligned}
\begin{bmatrix}
x_{11} & \cdots & x_{1k} \\
\vdots & \ddots & \vdots \\
x_{n1} & \cdots & x_{nk} 
\end{bmatrix}\end{aligned}$};
\getwidthofnode{\TestLength}{m1}
\pgfmathsetmacro\myWidth{\TestLength/2}
\getheightofnode{\TestLength}{m1}
\pgfmathsetmacro\myHeight{\TestLength/2}
\draw [thick,decorate,decoration={brace,amplitude=6pt,mirror}]  ($(m1)-({\myWidth*\MatrixToBraceXSouthScale pt},{\myHeight pt})$)--
($(m1)+({\myWidth*\MatrixToBraceXSouthScale pt},{-\myHeight pt})$) node[midway](m2) {};
\node [anchor=north,yshift=-7pt] at (m2) {$k$ variables};
\draw [thick,decorate,decoration={brace,amplitude=6pt,mirror}] ($(m1)+({\myWidth pt},{-\myHeight*\MatrixToBraceYEastScale pt})$)--
($(m1)+({\myWidth pt},{\myHeight*\MatrixToBraceYEastScale pt})$) node [midway](m3){};
\node[anchor=west,xshift=6pt] at (m3) {$n$ observations};
\end{tikzpicture}


\end{document}

Used @Caramdir's solution of \widthof within tikzpicture

Results:

enter image description here

koleygr
  • 20,105
  • But the OP is looking for round bracket, not braces. – Masroor Aug 29 '17 at 07:03
  • @Masroor thanks for the comment... If he ask for such a change I will try to change my edited solution (using tikz and replacing braces)... I just think he could be satisfied with this because it is more used in such kind of math. – koleygr Aug 29 '17 at 07:43
  • @Masroor fixed but used font characters and not decoration. – koleygr Aug 29 '17 at 08:40
  • I prefer @campa's answer and upvoted that. (I will try to understand it too... \copy command wasn't something familiar and may be my answer will be helpful as a step for some users before reaching campa's level) – koleygr Aug 29 '17 at 13:58
2

You can use the abraces package. If you wrap your bmatrix in \aunderbrace[l1r]{} you'll get what I think you want. You can use this brace the same way would would for other vanilla underbraces, using _ or ^ to put a superscript or subscript on them.

See the answer to the question here https://tex.stackexchange.com/a/132527/54688.

\documentclass{article}
\usepackage{mathtools}% http://ctan.org/pkg/mathtools
\usepackage{abraces}% http://ctan.org/pkg/abraces
\begin{document}
  \begin{flalign*}
    \aunderbrace[l1r]{%
      \begin{bmatrix}
      x_{11} & \cdots & x_{1k} \\
      \vdots & \ddots & \vdots \\
      x_{n1} & \cdots & x_{nk}
      \end{bmatrix}
    }_{k~\text{variables}}
  \end{flalign*}
\end{document}
tmgriffiths
  • 493
  • 4
  • 13
  • I think it is more complicated when using both right and under braces. See my answer that of course is not the best but you can see what I mean. – koleygr Aug 29 '17 at 06:59
1

My answer is a combination of all answers exposed here so far.

enter image description here

\documentclass{article}
\usepackage{amsmath,amsfonts}
\usepackage[overload]{abraces}

\let\underfence\underbrace

\begin{document}

\[
X=
\underfence[l1r]{%
    \begin{aligned}
        \begin{bmatrix}
        x_{11} & \cdots & x_{1k} \\
        \vdots & \ddots & \vdots \\
        x_{n1} & \cdots & x_{nk} 
        \end{bmatrix} \\[1ex]
    \end{aligned}
}_{k~\text{variables}}
\left.\vphantom{%
    \begin{aligned}
        \begin{bmatrix}
        x_{11} & \cdots & x_{1k} \\
        \vdots & \ddots & \vdots \\
        x_{n1} & \cdots & x_{nk} 
        \end{bmatrix}
    \end{aligned}
}\right)~\text{\scriptsize $n$ observations}
\]

\end{document}

UPDATE 01/04/2018

Applying this answer of Martin Scharrer to the matrix we get

enter image description here

The matrix is delimited by the parenthesis that are located according to an invisible bounding box that contains it.

enter image description here

The code

\documentclass[border=4mm,tikz]{standalone}
\usepackage{amsmath}
  \usetikzlibrary{matrix}

\begin{document}

\tikzset{
  withparens/.style = {%
    %draw=red,
    outer sep=0pt,
    right delimiter=),
    below delimiter=),
    align=center},
}

\begin{tikzpicture}
    \node (m1) [right=1.5cm, withparens] {%
        $\begin{bmatrix}
        x_{11} & \cdots & x_{1k} \\
        \vdots & \ddots & \vdots \\
        x_{n1} & \cdots & x_{nk} 
        \end{bmatrix}$
        };
\node [anchor=north, yshift=-1.1cm] at (m1) {$k$ variables};
\node [anchor=west, xshift=1.8cm] at (m1) {$n$ observations};
\end{tikzpicture}

\end{document}
Cragfelt
  • 4,005