将下括号与表格列对齐

将下括号与表格列对齐

我正在尝试使用带有\underbraces 的表格,这些表格位于某些列下方。我似乎没有弄清楚列宽和/或括号的正确尺寸——我天真地认为使用\multicol可能会简化问题。我希望列以括号为中心(反之亦然)。任何建议都非常感谢……(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}

example of tabular with underbrace

答案1

您不需要测量宽度,只需使用大括号填充您想要跨越的任何列即可

enter image description here

\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}

答案2

以下用途tabularx计算出列宽而不是测量它们,并根据适当的列跨度\upbracefill插入括号:X

enter image description here

\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}

相关内容