# Aligning underbraces with table columns

I'm trying to use a tabular with \underbraces under some of the columns. I don't seem to have figured out the right dimensions of the column widths and/or the braces -- I naively thought that using \multicol might simplify the problem. I'd like the columns to center on the braces (or vice versa). Any suggestions gratefully received... (MacTeX 2017)

\documentclass{article}

\usepackage[showframe]{geometry}
\usepackage{calc}
\usepackage{xparse}
\usepackage{array}

\newlength{\entrywd}
\newlength{\entrybracewd}

\setlength{\tabcolsep}{6pt}

\newenvironment{testtable}[2][\textwidth]{%
\setlength{\entrywd}{#1/#2-2\tabcolsep}
\setlength{\entrybracewd}{#1/#2-\tabcolsep}
\begingroup
\centering
\tabular{*{#2}{p{\entrywd}}}
}{%
\endtabular
\par
\endgroup
}

\newcommand{\mkbrace}[1]{%
\entryi[#1]{\raisebox{0.5\baselineskip}{$\underbrace{\hbox to #1\entrybracewd{\hfill}}$}}%
}

%% Used this because an optional argument of \newcommand does not work with \multicolumn:
%% https://tex.stackexchange.com/questions/257902/defining-multicolum-by-macro-with-optional-argument/257904#257904
%% https://tex.stackexchange.com/questions/17133/custom-macro-for-multicolumn-with-without-xparse

\NewExpandableDocumentCommand{\entry}{ O{1} m }{%
\multicolumn{#1}{>{\centering}p{#1\entrywd}}{#2}%
}

\NewExpandableDocumentCommand{\entryi}{ O{1} m }{%
\multicolumn{#1}{>{\centering}p{#1\entrybracewd}}{#2}%
}

\parindent0pt
\begin{document}

\begin{testtable}{7}
\entry[2]{one and some more of this sort}&\entry{bla bla bla and more}&\entry{this goes on and on and more}&\entry{bunch of really boring stuff}&\entry{I wish I could figure out}&\entry{so that I could move on with this nonsense}\\
\mkbrace{2}&\mkbrace{2}&\mkbrace{3}\\
\end{testtable}

\begin{testtable}{5}
\entry[2]{one and some more of this sort}&\entry{bla bla bla and more}&\entry{this goes on and on and more}&\entry{bunch of really boring stuff}\\
\mkbrace{2}&\mkbrace{2}&\mkbrace{1}\\
\end{testtable}

\begin{testtable}[0.5\textwidth]{4}
\entry[2]{one and some more of this sort}&\entry{bla bla bla and more}&\entry{this goes on and on and more}\\[-\baselineskip]
\mkbrace{2}&\mkbrace{2}\\
\end{testtable}

\end{document}


• Just for testing purposes, this is a case where vertical rulers | might be useful. – user31729 Dec 28 '17 at 22:45

## 2 Answers

You don't need to measure the widths, just use a bracefill to fill whatever columns you want to span

\documentclass{article}

\usepackage[showframe]{geometry}
\usepackage{calc}
\usepackage{xparse}
\usepackage{array}

\newlength{\entrywd}
\newlength{\entrybracewd}

\setlength{\tabcolsep}{6pt}

\newenvironment{testtable}[2][\textwidth]{%
\setlength{\entrywd}{#1/#2-2\tabcolsep}
\setlength{\entrybracewd}{#1/#2-\tabcolsep}
\begingroup
\centering
\tabular{*{#2}{p{\entrywd}}}
}{%
\endtabular
\par
\endgroup
}

\newcommand{\mkbrace}[1]{%
\multicolumn{#1}{c}{\mbox{}\upbracefill\mbox{}}%
}

\NewExpandableDocumentCommand{\entry}{ O{1} m }{%
\multicolumn{#1}{>{\centering}p{#1\entrywd}}{#2}%
}

\NewExpandableDocumentCommand{\entryi}{ O{1} m }{%
\multicolumn{#1}{>{\centering}p{#1\entrybracewd}}{#2}%
}

\parindent0pt
\begin{document}

\begin{testtable}{7}
\entry[2]{one and some more of this sort}&\entry{bla bla bla and more}&\entry{this goes on and on and more}&\entry{bunch of really boring stuff}&\entry{I wish I could figure out}&\entry{so that I could move on with this nonsense}\\
\mkbrace{2}&\mkbrace{2}&\mkbrace{3}\\
\end{testtable}

\begin{testtable}{5}
\entry[2]{one and some more of this sort}&\entry{bla bla bla and more}&\entry{this goes on and on and more}&\entry{bunch of really boring stuff}\\
\mkbrace{2}&\mkbrace{2}&\mkbrace{1}\\
\end{testtable}

\begin{testtable}[0.5\textwidth]{4}
\entry[2]{one and some more of this sort}&\entry{bla bla bla and more}&\entry{this goes on and on and more}\\[-\baselineskip]
\mkbrace{2}&\mkbrace{2}\\
\end{testtable}

\end{document}


The following uses tabularx to figure out the column widths rather than measuring them, and \upbracefill to insert the brace according to the appropriate X-column span:

\documentclass{article}

\usepackage[showframe]{geometry}
\usepackage{xparse,tabularx}

\setlength{\tabcolsep}{6pt}

\newenvironment{testtable}[2][\textwidth]{%
\centering
\tabularx{#1}{ *{#2}{X} }
}{%
\endtabularx
\par
}

\newcommand{\mkbrace}[1]{\multicolumn{#1}{c}{\upbracefill}}
\NewExpandableDocumentCommand{\entry}{ O{1} m }{%
\multicolumn{#1}{>{\hsize=#1\hsize\centering}X}{#2}%
}

\setlength{\parindent}{0pt}

\begin{document}

\begin{testtable}{7}
\entry[2]{one and some more of this sort} &
\entry{bla bla bla and more} &
\entry{this goes on and on and more} &
\entry{bunch of really boring stuff} &
\entry{I wish I could figure out} &
\entry{so that I could move on with this nonsense} \\
\mkbrace{2} & \mkbrace{2} & \mkbrace{3}
\end{testtable}

\begin{testtable}{5}
\entry[2]{one and some more of this sort} &
\entry{bla bla bla and more} &
\entry{this goes on and on and more} &
\entry{bunch of really boring stuff} \\
\mkbrace{2} & \mkbrace{2} & \mkbrace{1}
\end{testtable}

\begin{testtable}[0.5\textwidth]{4}
\entry[2]{one and some more of this sort} &
\entry{bla bla bla and more} &
\entry{this goes on and on and more} \\
\mkbrace{2} & \mkbrace{2}
\end{testtable}

\end{document}