# Underbrace in matrix problem

I have the latex code below for a matrix which I want to define. The problem is that in the 5th column the terms overlap with the 4th column and the right "edge" of the matrix. Can this be solved easily?

\documentclass[12pt]{article}

\usepackage{pgfplots}
\usepackage{amssymb}
\usepackage{undertilde}
\usepackage{amstext}
\usepackage{amsthm}
\usepackage{epsfig}
\usepackage{dsfont} % For using Indicator function
\usepackage{graphics}
\usepackage{color}
\usepackage{booktabs}
\usepackage{graphicx}
\usepackage{caption}
\usepackage{subcaption}
\usepackage{float}
\usepackage{lscape}
\usepackage[sort&compress,round,comma,authoryear]{natbib}%bibtex
\usepackage{mathtools}
\usepackage{epstopdf}
\usepackage[titletoc,title]{appendix}% for appendix met A
\usepackage{bm}
\usepackage{cleveref}
\usepackage{dcolumn}
\usepackage{enumitem} % For labeling enumeration
\newcommand{\sunderb}[2]{
\mathclap{\underbrace{\makebox[#1]{0 $\cdots$ 0}}_{#2}}
}

\begin{document}

$$V= \begin{pmatrix} B_{21}(0)& B_{22}(0) & B_{23}(0)& 0 & \sunderb{3.5em}{12} \\ \sunderb{3.5em}{4} & B_{21}(0)& B_{22}(0) & B_{23}(0) & \sunderb{3.5em}{9} \\ \sunderb{3.5em}{8} & B_{21}(0)& B_{22}(0) & B_{23}(0) & \sunderb{3.5em}{5} \\ \sunderb{3.5em}{12} & B_{21}(0)& B_{22}(0) & B_{23}(0) & 0 \end{pmatrix} }\in \R^{4 \times 16}.$$

\end{document}


After guessing at a definition for \R and removing a stray brace, I modified the \sunderb macro by adding an outer \makebox[#1]. Without it, you ar relying on the natural width of other items in the column to compensate for the \mathclap. When a column, like the last one of the matrix, has nothing else that wide, it provokes the overlap.

\documentclass[12pt]{article}

\usepackage{pgfplots}
\usepackage{amssymb}
\usepackage{undertilde}
\usepackage{amstext}
\usepackage{amsthm}
\usepackage{epsfig}
\usepackage{dsfont} % For using Indicator function
\usepackage{graphics}
\usepackage{color}
\usepackage{booktabs}
\usepackage{graphicx}
\usepackage{caption}
\usepackage{subcaption}
\usepackage{float}
\usepackage{lscape}
\usepackage[sort&compress,round,comma,authoryear]{natbib}%bibtex
\usepackage{mathtools}
\usepackage{epstopdf}
\usepackage[titletoc,title]{appendix}% for appendix met A
\usepackage{bm}
\usepackage{cleveref}
\usepackage{dcolumn}
\usepackage{enumitem} % For labeling enumeration
\newcommand\R{\mathbb{R}}
\newcommand{\sunderb}[2]{\makebox[#1]{$\mathclap{\underbrace{\makebox[#1]{0$\cdots$0}}_{#2}}$}%
}

\begin{document}

$V= \begin{pmatrix} B_{21}(0)& B_{22}(0) & B_{23}(0)& 0 & \sunderb{3.5em}{12} \\ \sunderb{3.5em}{4} & B_{21}(0)& B_{22}(0) & B_{23}(0) & \sunderb{3.5em}{9} \\ \sunderb{3.5em}{8} & B_{21}(0)& B_{22}(0) & B_{23}(0) & \sunderb{3.5em}{5} \\ \sunderb{3.5em}{12} & B_{21}(0)& B_{22}(0) & B_{23}(0) & 0 \end{pmatrix} \in \R^{4 \times 16}.$

\end{document}


I think you don't need a \mathclap in the definition of \sunderb. Also, I reduced slighlty the value of the width (1st argument) as it looks better for my taste. A final comment: needless to load amstext: mathtools does it for you. Same comment for graphics, loaded by graphicx, and finally, epsfig is deprecated in favour of graphicx.

\documentclass[12pt]{article}

\usepackage{amssymb}
\usepackage{mathtools}
\newcommand{\sunderb}[2]{\underbrace{\makebox[#1]{$0 ⋯ 0$}}_{#2}}

\begin{document}

$$V= \begin{pmatrix} B_{21}(0) & B_{22}(0) & B_{23}(0) & 0 & \sunderb{3em}{12} \\ \sunderb{3em}{4} & B_{21}(0) & B_{22}(0) & B_{23}(0) & \sunderb{3em}{9} \\ \sunderb{3em}{8} & B_{21}(0) & B_{22}(0) & B_{23}(0) & \sunderb{3em}{5} \\ \sunderb{3em}{12} & B_{21}(0) & B_{22}(0) & B_{23}(0) & 0 \end{pmatrix} ∈ \mathbb R^{4 × 16}.$$

\end{document}