TransWikia.com

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

TeX - LaTeX Asked on November 29, 2021

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?

5 Answers

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

Answered by F. Pantigny on November 29, 2021

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)}]

Answered by Felix Emanuel on November 29, 2021

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

Answered by Sebastiano on November 29, 2021

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

Answered by Bernard on November 29, 2021

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}

Answered by pluton on November 29, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP