Math & ScienceBmatrix Column Headings

Information and discussion about LaTeX's math and science related features (e.g. formulas, graphs).
Post Reply
statsbob
Posts: 3
Joined: Sat Aug 29, 2009 10:01 pm

Bmatrix Column Headings

Post by statsbob »

I'm a stats professor, and I often show matrices in which columns represent variables. Sometimes I wish to include the variable names as headers on top of the columns. I have just been putting them as the first row of the matrix, but that's not as clear in terms of the brackets and matrix dimensions. Here is a simple example:

Code: Select all

\begin{bmatrix}
  \mu & \tau_1 & \tau_2 \\
  1 & 1 & 0 \\
  1 & 1 & 0 \\
  1 & 1 & 0 \\
  1 & 0 & 1 \\
  1 & 0 & 1 \\
  1 & 0 & 1
\end{bmatrix}
Can I create this so the headings line up with the columns of the matrix, but are outside of the brackets? Thanks!!

Bob

Recommended reading 2024:

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

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

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

Bmatrix Column Headings

Post by localghost »

Take a look at the \bordermatrix described in the "Math mode" document (Section 5 - Matrix, p. 20f).


Best regards and welcome to the board
Thorsten
User avatar
Stefan Kottwitz
Site Admin
Posts: 10345
Joined: Mon Mar 10, 2008 9:44 pm

Bmatrix Column Headings

Post by Stefan Kottwitz »

Hi Bob,

welcome to the board!
You could nest arrays, like

Code: Select all

\[
\begin{array}{c}
\begin{matrix}
\mu & \!\tau_1\! & \tau_2
\end{matrix} \\
\left[\ \begin{matrix}
1 & 1 & 0 \\
1 & 1 & 0 \\
1 & 1 & 0 \\
1 & 0 & 1 \\
1 & 0 & 1 \\
1 & 0 & 1
\end{matrix}\ \right]
\end{array}
\]
I've just used the matrix environment inside \left[ ... \right] to get more space after the brackets.

Stefan
LaTeX.org admin
statsbob
Posts: 3
Joined: Sat Aug 29, 2009 10:01 pm

Re: Bmatrix Column Headings

Post by statsbob »

Thanks for the quick responses, I really appreciate it. I've just been learning on my own since I decided this spring to start preparing my notes in Latex. I'm constantly learning.

I will have to look into the bordermatrix environment later this week. That looks great, but I'm not yet familiar with macros in Latex and am not sure how to use that yet.

The problem I would encounter with second suggestion is that the headings aren't strictly aligned with the matrix columns. I believe I must have the headings in the same environment.
e.g.
\[
\begin{array}{c}
\begin{matrix}
\mu & (\beta\tau)_1 & \tau_2
\end{matrix} \\
\left[\ \begin{matrix}
1 & 1 & 0 \\
1 & 1 & 0 \\
1 & 1 & 0 \\
1 & 0 & 1 \\
1 & 0 & 1 \\
1 & 0 & 1
\end{matrix}\ \right]
\end{array}
\]

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

Bmatrix Column Headings

Post by localghost »

At first, please use the code environment for posting longer pieces of source code to keep a post clear.
statsbob wrote:I will have to look into the bordermatrix environment later this week. That looks great, but I'm not yet familiar with macros in Latex and am not sure how to use that yet.
According to the examples in the linked document you can easily get what you want.

Code: Select all

\documentclass[11pt,a4paper,english]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{babel}

\makeatletter
\newif\if@borderstar
\def\bordermatrix{\@ifnextchar*{%
\@borderstartrue\@bordermatrix@i}{\@borderstarfalse\@bordermatrix@i*}%
}
\def\@bordermatrix@i*{\@ifnextchar[{\@bordermatrix@ii}{\@bordermatrix@ii[()]}}
\def\@bordermatrix@ii[#1]#2{%
\begingroup
\m@th\@tempdima8.75\p@\setbox\z@\vbox{%
\def\cr{\crcr\noalign{\kern 2\p@\global\let\cr\endline }}%
\ialign {$##$\hfil\kern 2\p@\kern\@tempdima & \thinspace %
\hfil $##$\hfil && \quad\hfil $##$\hfil\crcr\omit\strut %
\hfil\crcr\noalign{\kern -\baselineskip}#2\crcr\omit %
\strut\cr}}%
\setbox\tw@\vbox{\unvcopy\z@\global\setbox\@ne\lastbox}%
\setbox\tw@\hbox{\unhbox\@ne\unskip\global\setbox\@ne\lastbox}%
\setbox\tw@\hbox{%
$\kern\wd\@ne\kern -\@tempdima\left\@firstoftwo#1%
\if@borderstar\kern2pt\else\kern -\wd\@ne\fi%
\global\setbox\@ne\vbox{\box\@ne\if@borderstar\else\kern 2\p@\fi}%
\vcenter{\if@borderstar\else\kern -\ht\@ne\fi%
\unvbox\z@\kern-\if@borderstar2\fi\baselineskip}%
\if@borderstar\kern-2\@tempdima\kern2\p@\else\,\fi\right\@secondoftwo#1 $%
}\null \;\vbox{\kern\ht\@ne\box\tw@}%
\endgroup
}
\makeatother

\begin{document}
  \[
    \bordermatrix[{[]}]{%
      & \mu & \tau_1 & \tau_2 \cr
      & 1 & 1 & 0 \cr
      & 1 & 1 & 0 \cr
      & 1 & 1 & 0 \cr
      & 1 & 0 & 1 \cr
      & 1 & 0 & 1 \cr
      & 1 & 0 & 1
    }
  \]
\end{document}
User avatar
Stefan Kottwitz
Site Admin
Posts: 10345
Joined: Mon Mar 10, 2008 9:44 pm

Bmatrix Column Headings

Post by Stefan Kottwitz »

Adjustments could be made like

Code: Select all

\documentclass[a4paper,10pt]{article}
\usepackage{amsmath}
\usepackage{mathtools}
\begin{document}
\[
\begin{array}{c}
\begin{matrix}
\mu & \mathclap{\tau_1} & \tau_2
\end{matrix} \\
\begin{bmatrix}
1 & 1 & 0 \\
1 & 1 & 0 \\
1 & 1 & 0 \\
1 & 0 & 1 \\
1 & 0 & 1 \\
1 & 0 & 1
\end{bmatrix}
\end{array}
\]
\end{document}
or a package like bigdelim could be used, or big delimiters and nested arrays.

Stefan
LaTeX.org admin
statsbob
Posts: 3
Joined: Sat Aug 29, 2009 10:01 pm

Re: Bmatrix Column Headings

Post by statsbob »

Again thanks for both of your responses. The result of the bordermatrix script looks good. Is it reasonable to say that that code is something that one would usually save to a file to be inputted in the preamble when needed?
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Bmatrix Column Headings

Post by localghost »

statsbob wrote:[...] Is it reasonable to say that that code is something that one would usually save to a file to be inputted in the preamble when needed?
The final decision is up to you.
Post Reply