# How do I typeset vertical and horizontal lines inside a matrix?

(From the notation used in course notes for Stanford CS229 available online.) I'm trying to typeset the following matrices made up of row or column vectors:

Any ideas?

## 3 Answers

You can use rules to create the vertical and horizontal bars and then just put them into your array directy.

\documentclass{article}
\usepackage{array}
\begin{document}
\newcommand*{\vertbar}{\rule[-1ex]{0.5pt}{2.5ex}}
\newcommand*{\horzbar}{\rule[.5ex]{2.5ex}{0.5pt}}

$A = \left[ \begin{array}{cccc} \vertbar & \vertbar & & \vertbar \\ a_{1} & a_{2} & \ldots & a_{n} \\ \vertbar & \vertbar & & \vertbar \end{array} \right]$
\setlength{\extrarowheight}{1ex}
$A = \left[ \begin{array}{ccc} \horzbar & a^{T}_{1} & \horzbar \\ \horzbar & a^{T}_{2} & \horzbar \\ & \vdots & \\ \horzbar & a^{T}_{n} & \horzbar \end{array} \right]$

\end{document}


• If I use the exact code from above the vertical lines are not set correctly. Has anything changed? – Adam Aug 13 '14 at 13:04
• No, the code still does exactly this for me. – Alan Munn Aug 13 '14 at 14:00
• I also copied your exact code into a new document in TeXStudio, and the vertical spacing in the first matrix is wonky. See this image. – Travis Bemrose Sep 16 '15 at 20:01
• @TravisBemrose Hmm, it seems there was a typo in the original code. I've changed the [1ex] to [-1ex]. But Herbert's \vrule works nicely too. – Alan Munn Sep 17 '15 at 0:28
• Yep, once I clean up the \\ newlines and remove some extra ampersands that showed up, it works great now. I'd been using \vert. – Travis Bemrose Sep 17 '15 at 0:36
\documentclass{article}
\usepackage{array}
\begin{document}

$A = \left[ \begin{array}{cccc} \vrule & \vrule & & \vrule\\ a_{1} & a_{2} & \ldots & a_{n} \\ \vrule & \vrule & & \vrule \end{array} \right]$
\setlength{\extrarowheight}{1ex}
$A = \left[ \begin{tabular}{c>{}c<{}c} --- & a^{T}_{1} & ---\\ --- & a^{T}_{2} & ---\\ & \vdots & \\ --- & a^{T}_{n} & --- \end{tabular} \right]$

\end{document}


• When I used --- in math mode, I got three minuses. I had to use \text{---} to make it work. But it seems to be working for you... any ideas why? – zellyn Mar 8 '11 at 19:16
• no, in math it is always ---. I used a tabular which is by default in textmode ... – Herbert Mar 8 '11 at 19:20

I like the following easy solution for vertical lines.

V = \begin{pmatrix}
\mid & \mid & & \mid \\
{\bf v}_{1} & {\bf v}_{2} & \cdots & {\bf v}_{r}\\
\mid & \mid & & \mid \\
\end{pmatrix}

• I think the question in Werner's comment isn't relevant to the question at hand. Perhaps it belongs as its own question? – Luke Oeding Sep 18 '15 at 16:23
• While it might not be relevant to this question per se, it's relevant in general, as \bf (and others) are deprecated in their usage. – Werner Sep 18 '15 at 16:25
• Perhaps I should have used something like \vec{v} or \mathbf{v}. It's an easy modification. – Luke Oeding Sep 18 '15 at 16:53