How to shorten \hline in a matrix

The bmatrix environment and also the other matrix related ones use internally array, but do a backup after the left delimiter and before the right one. Therefore the rule made by \hline presents the problem you're facing.

There are technical reasons for using a backup rather than removing the intercolumn spacing at either end with @{}. There are two different solutions; one is to use booktabs and the other one is making a new environment for matrices with rule separators.

The first is more elegant, in my opinion. Both require you to specify the columns in some way.

\documentclass{article}
\usepackage{amsmath}

\usepackage{booktabs} % required for the first solution

% this is for the second solution; the argument is the number of columns
\newenvironment{lbmatrix}[1]
  {\left[\array{@{}*{#1}{c}@{}}}
  {\endarray\right]}

\begin{document}
\begin{equation*}
\begin{bmatrix}
  a & b\\
  \cmidrule(lr){1-2}
  c & d
\end{bmatrix}
\end{equation*}
\begin{equation*}
\begin{lbmatrix}{2}
  a & b\\
  \hline
  c & d
\end{lbmatrix}
\end{equation*}
\end{document}

enter image description here


Try switching to array instead, apparently the bmatrix environment doesn't work well with horizontal lines (judging from what I've seen in other forums).

If you switch between \begin{array}{@{}cc@{}}, as suggested by daleif, \begin{array}{cc}, it will change the appearance.

matrices

\documentclass{article}
\usepackage{amsmath}
\begin{document}
    \[ \left[ \begin{array}{@{}cc@{}}
    a & b \\ \hline
    c & d
    \end{array} \right]
    %
    \left[ \begin{array}{cc}
    a & b \\ \hline
    c & d
    \end{array} \right]
    \]
\end{document}