# How to change the vertical position of a horizontal line in a matrix?

I need to adjust the vertical position of a horizontal line inside a matrix. Here's a MWE code to play with:

\documentclass[11pt,letterpaper,twoside]{book}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{mathtools}

\begin{document}

$$R = \left[ \begin{array}{ccc|ccc} A & B & C & A & B &C \\[2ex] A & B & C & A & B &C \\[2ex] A & B & C & A & B &C \\[2ex] \hline A & B & C & A & B &C \\[2ex] A & B & C & A & B &C \\[2ex] A & B & C & A & B &C \end{array} \right]\!.$$

\end{document}


Preview:

I don't want the line to produce any gap on the crossing vertical line, and I need to keep the \\[2ex] vertical spacing between each element in the matrix. So how can I move up the horizontal line, without changing the rest of the matrix?

• If you are not tied to exactly 2ex of row sep, you could also modify the \arraystretch for that matrix, e.g. to \def\arraystretch{1.6}, and then removing the additional [2ex] to each \\ . Maybe there is also a possibility to set \arraystretch so that is is equivalent to \\[2ex]. Commented Jul 23, 2020 at 9:08

With a trick

\documentclass[11p]{book}
\usepackage[T1]{fontenc}
\usepackage{amsmath}
\usepackage{mathtools}

\begin{document}
$$R = \left[ \begin{array}{ccc|ccc} A & B & C & A & B &C \\[2ex] A & B & C & A & B &C \\[2ex] A & B & C & A & B &C \\[.65ex] \hline \rule[0pt]{2pt}{\heightof{A}+1ex}A & B & C & A & B &C \\[2ex] A & B & C & A & B &C \\[2ex] A & B & C & A & B &C \end{array} \right]$$
\end{document}


Replace \rule[0pt]{2pt}{\heightof{A}+1ex} by \rule[0pt]{0pt}{\heightof{A}+1ex}. Also the [.65ex] could certainly be computed in a better way by getting the interline value in the matrix when the option \\[0ex] is used. I do not know this value.

For an exact control, you could go with

\documentclass[11p]{book}
\usepackage[T1]{fontenc}
\usepackage{amsmath}
\usepackage{mathtools}
\begin{document}
$$% \renewcommand{\arraystretch}{0}% R = \left[ \begin{array}{ccc|ccc} A & B & C & A & B &C \\[2ex] A & B & C & A & B &C \\[2ex] A & B & C & A & B &C \\[1ex] \hline \rule[0pt]{2pt}{\heightof{A}+1ex}% A & B & C & A & B &C \\[2ex] A & B & C & A & B &C \\[2ex] A & B & C & A & B &C \end{array} \right]$$

• The problem with this solution is the command \heightof{A}. A is actually a matrix element with indices (a math symbol, like R_{1212}, or with the tensor package : \tensor{R}{_{1212}} for better indices placements), and it gives a compilation error.
– Cham
Commented Jul 22, 2020 at 18:16
• Hmm, using $A$ appears to solve the problem in my previous comment.
– Cham
Commented Jul 22, 2020 at 18:18
• I think \rule[0pt] can be replaced with just \rule because the first optional argument is raise value, no raise is equivalent to raise 0pt? // remark, this is somewhat similar to \strut Commented May 6, 2022 at 16:09

Another possibility, withcellspace:

\documentclass[11pt, letterpaper, twoside]{book}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{amsfonts}
\usepackage{mathtools}

\usepackage[math]{cellspace}
\setlength{\cellspacetoplimit}{1.6ex}
\setlength{\cellspacebottomlimit}{1.6ex}

\begin{document}

R = \left[ \begin{array}{ccc|>{}Sc<{}cc} \multicolumn{6}{c} {}\\ \noalign{\vspace{-3.2ex}} A & B & C & A & B &C \\ A & B & C & A & B &C \\ A & B & C & A & B &C \\ \hline A & B & C & A & B &C \\ A & B & C & A & B &C \\ A & B & C & A & B &C \\[-0.6ex] \end{array} \right].

\end{document}


• What is the cell space package doing?
– Cham
Commented Jul 22, 2020 at 19:30
• It ensures a minimal vertical spacing at the top and bottom of cells in columns with specifier prefixed with the letter S (or C if you load siunitx). I used the prefix for the 4th column. It is commonly used to ensure cells contents is vertically centred in their row. Commented Jul 22, 2020 at 19:48

I think that this code it is possible to create, quickly, with nicematrix package. Here I add my humble version using array enviroment.

\documentclass[a4paper,12pt]{article}
\usepackage{amsmath}

\begin{document}
$\left(\begin{array}{@{}c|c@{}} \begin{matrix} A & B & C\\ A & B & C\\ A & B & C \end{matrix} & \begin{matrix} A & B & C\\ A & B & C\\ A & B & C \end{matrix} \\ \hline \begin{matrix} A & B & C\\ A & B & C\\ A & B & C \end{matrix}& \begin{matrix} A & B & C\\ A & B & C\\ A & B & C \end{matrix} \end{array}\right)$
\end{document}


Or this rudimental solution with the vertical space for the matrices [.3cm].

\documentclass[a4paper,12pt]{article}
\usepackage{amsmath}

\begin{document}
$R=\left(\begin{array}{@{}c|c@{}} \begin{matrix} A & B & C\\[.3cm] A & B & C\\[.3cm] A & B & C \end{matrix} & \begin{matrix} A & B & C\\[.3cm] A & B & C\\[.3cm] A & B & C \end{matrix}\\[.1cm] \\[-.1cm] \hline\\ \begin{matrix} A & B & C\\[.3cm] A & B & C\\[.3cm] A & B & C \end{matrix}& \begin{matrix} A & B & C\\[.3cm] A & B & C\\[.3cm] A & B & C \end{matrix} \end{array}\right).$
\end{document}


In case you'd like a TikZ alternative, using its matrix library:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}

\begin{document}
$$R = \begin{tikzpicture}[baseline] \matrix (matrix) [ matrix of math nodes, row sep = 2ex, column sep = \columnsep, left delimiter = \lbrack, right delimiter = \rbrack ] { A & B & C & A & B &C \\ A & B & C & A & B &C \\ A & B & C & A & B &C \\ A & B & C & A & B &C \\ A & B & C & A & B &C \\ A & B & C & A & B &C \\ }; \draw (matrix.east) -- (matrix.west) (matrix.north) -- (matrix.south); \end{tikzpicture} .$$
\end{document}


If you need to align = with the middle line in your matrix, you could yshift the baseline of your tikzpicture:

\begin{tikzpicture}[baseline={([yshift=-3pt]matrix)}]

The environment {bNiceMatrix} of nicematrix is similar to the classical environment {bmatrix} of amsmath but creates PGF/Tikz nodes under the array accessible by the user.

Here I use these nodes with Tikz to draw the vertical rule (not broken) and the horizontal rule (with a yshift).

\documentclass{article}
\usepackage{nicematrix}
\usepackage{tikz}

\begin{document}

$$R = \begin{bNiceMatrix}[margin] A & B & C & A & B &C \\[2ex] A & B & C & A & B &C \\[2ex] A & B & C & A & B &C \\[2ex] A & B & C & A & B &C \\[2ex] A & B & C & A & B &C \\[2ex] A & B & C & A & B &C \CodeAfter \tikz \draw (1-|4) -- (7-|4) ([yshift=4pt]4-|1) -- ([yshift=4pt]4-|7) ; \end{bNiceMatrix}$$

\end{document}


Remark: You need two compilations.

However, in this case, it seems that the use of the keys cell-space-limits of nicematrix does the job (no need to use Tikz).

\documentclass{article}
\usepackage{nicematrix}

\begin{document}

$$R = \begin{bNiceArray}{ccc|ccc}[margin,cell-space-limits=6pt] A & B & C & A & B &C \\ A & B & C & A & B &C \\ A & B & C & A & B &C \\ \hline A & B & C & A & B &C \\ A & B & C & A & B &C \\ A & B & C & A & B &C \end{bNiceArray}$$

\end{document}