Math & ScienceCentering an hline inside a matrix

Information and discussion about LaTeX's math and science related features (e.g. formulas, graphs).
Post Reply
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

Centering an hline inside a matrix

Post by Cham »

Here's a matrix with two lines inside. One vertical line and an horizontal line to subdivide some parts inside the matrix. The MWE code below is working great, but the horizontal line isn't centered between the first and second rows, and it isn't pretty :

Code: Select all

\documentclass[12pt,letterpaper]{article}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{amsmath}

\begin{document}
	\begin{equation}
		M =
		\left[ \begin{array}{c|ccc}
			\phantom{\!\! -\,} \gamma \phantom{\, v} & -\, \gamma \, v & \phantom{-\,} 0 \phantom{\, v} & \phantom{-\,} 0 \phantom{-\,} \\[6pt]
			\hline \!\! -\, \gamma \, v & \phantom{-\,} \gamma \phantom{\, v} & \phantom{-\,} 0 \phantom{\, v} & \phantom{-\,} 0 \phantom{-\,} \\[6pt]
			\phantom{\!\! -\,} 0 \phantom{\, v} & \phantom{-\,} 0 \phantom{\, v} & \phantom{-\,} 1 \phantom{\, v} & \phantom{-\,} 0 \phantom{-\,} \\[6pt]
			\phantom{\!\! -\,} 0 \phantom{\, v} & \phantom{-\,} 0 \phantom{\, v} & \phantom{-\,} 0 \phantom{\, v} & \phantom{-\,} 1 \phantom{-\,}
		\end{array} \right]\!,
	\end{equation}
\end{document}
Here's a preview :
matrix.jpg
matrix.jpg (9.08 KiB) Viewed 9770 times
How can I make that horizontal hline to be perfectly centered between the first two rows ?

Take note that the spacing between each row should stay constant (I used [6pt] in the code above).

Recommended reading 2024:

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

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

Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

Re: Centering an hline inside a matrix

Post by Cham »

Nobody has an idea how to vertically center the hline between two rows in a matrix ?
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

Centering an hline inside a matrix

Post by Cham »

I'm hacking the matrices by changing the command \hline to \hline \\[-12.5pt], and adding [2.1pt] after the first \\ in the matrix (before the \hline command). The numbers were found by trial and error.

Is there a better way to center that pesky hline ?
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

Re: Centering an hline inside a matrix

Post by Cham »

So, must I conclude that there's no other way to center the hline ?
User avatar
Stefan Kottwitz
Site Admin
Posts: 10345
Joined: Mon Mar 10, 2008 9:44 pm

Centering an hline inside a matrix

Post by Stefan Kottwitz »

It was vertically centered, originally. You just changed it by manually adding 6pt whitespace below the lines, that's the shift.

A better and symmetric way would be redefining \arraystretch, such as

\renewcommand*{\arraystretch}{1.4}

globally, for all arrays, in the preamble, or locally, you could do it right after \begin{equation}.

Stefan
LaTeX.org admin
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

Re: Centering an hline inside a matrix

Post by Cham »

Thanks for the answer, Stefan.

But what do you mean, it was centered originaly ? As I see the preview, the horizontal line isn't centered between the numbers.
User avatar
Stefan Kottwitz
Site Admin
Posts: 10345
Joined: Mon Mar 10, 2008 9:44 pm

Centering an hline inside a matrix

Post by Stefan Kottwitz »

By originally, I meant before you shifted the lines down 6 pt by \\[6pt].

Stefan
LaTeX.org admin
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

Centering an hline inside a matrix

Post by Cham »

Stefan_K wrote:By originally, I meant before you shifted the lines down 6 pt by \\[6pt].
Ha, yes, of course. But then if you do add the larger spacing, how the line should stay centered ?

EDIT : Hmmm! There's something I really don't understand here, about rows spacing in LaTeX...
User avatar
Stefan Kottwitz
Site Admin
Posts: 10345
Joined: Mon Mar 10, 2008 9:44 pm

Centering an hline inside a matrix

Post by Stefan Kottwitz »

Adding such space is just not the right tool when you use a centered line. It's like reserving 6 pt but expecting the line to move 3 pt backwards then. It would need to add the same amount of pt after the hline, if you would like to do it that way.

\arraystretch is better in this regard.

Stefan
LaTeX.org admin
Post Reply