5

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[total={6in,10in},left=1.5in,top=0.5in,includehead,includefoot]{geometry}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{mathtools}

\begin{document}

    \begin{equation}
        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{equation}

\end{document}

Preview:

enter image description here

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?

1
  • 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]. Jul 23, 2020 at 9:08

5 Answers 5

8

With a trick

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

\begin{document}
    \begin{equation}
        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{equation}
\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.

enter image description here

For an exact control, you could go with

\documentclass[11p]{book}
\usepackage[T1]{fontenc}
\usepackage{amsmath}
\usepackage{mathtools}
\begin{document}
    \begin{equation}%
    \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]
    \end{equation}
3
  • 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
    Jul 22, 2020 at 18:16
  • 2
    Hmm, using $A$ appears to solve the problem in my previous comment.
    – Cham
    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
    – user202729
    May 6, 2022 at 16:09
5

Another possibility, withcellspace:

\documentclass[11pt, letterpaper, twoside]{book}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage[total={6in,10in},left=1.5in,top=0.5in,includehead,includefoot]{geometry}
\usepackage{amsfonts}
\usepackage{mathtools}

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

\begin{document}

\begin{equation}
    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{equation}

\end{document} 

enter image description here

2
  • What is the cell space package doing?
    – Cham
    Jul 22, 2020 at 19:30
  • 1
    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.
    – Bernard
    Jul 22, 2020 at 19:48
3

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}

enter image description here

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}

enter image description here

2

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

matrix in TikZ

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

\begin{document}
\begin{equation}
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{equation}
\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)}]

2

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}

\begin{equation}
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{equation}

\end{document}

Remark: You need two compilations.

Output of the above code

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}

\begin{equation}
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{equation}

\end{document}

Result of the second code

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .