内部表格(即“非常长的描述”)超出了页面限制:
\documentclass{article}
\usepackage[margin=3cm]{geometry}
\usepackage{array}
\usepackage{booktabs}
\usepackage[table]{xcolor}
\usepackage{tabularx}
\begin{document}
\newenvironment{messageWithProperties}[1]
{
\noindent
\tabularx{\textwidth}{>{\bfseries}lX}
\toprule
\midrule
header &
$\left\{\tabular{>{\bfseries}l l >{\sffamily}p{\dimexpr\textwidth-12\tabcolsep-5\fboxsep-7.5cm\relax}}
\toprule
id & integer & desc \\
key & integer & desc \\
\bottomrule
\endtabular\right\}$ \\[4ex]
\midrule
#1 &
$\left\{\tabular{>{\bfseries}l l >{\sffamily}p{\dimexpr\textwidth-12\tabcolsep-5\fboxsep-7.5cm\relax}}
\toprule
}
{
\bottomrule
\endtabular\right\}$ \\[4ex]
\midrule
\bottomrule
\endtabularx
}
\begin{messageWithProperties}{someVeryLongName}
userId & string & desc \\
propertyName & veryLongStringType & a very very long description of the field \\
\end{messageWithProperties}
\end{document}
如何强制其进入里面?
最好限制其大小\textwidth - \widthof{#1}
并允许填充所有可用空间。如何做到这一点?
答案1
我会用它environ
来避免tabularx
笨拙的嵌套:
\documentclass{article}
\usepackage[margin=3cm]{geometry}
\usepackage{array}
\usepackage{booktabs}
\usepackage[table]{xcolor}
\usepackage{tabularx,environ}
\newlength{\bracewidth}
\settowidth{\bracewidth}{$\left\{\vbox to 1in{}\right.\kern-\nulldelimiterspace$}
\newcommand{\innerMWP}[1]{%
$\left\{\begin{tabularx}{\dimexpr\hsize-2\bracewidth}{ >{\bfseries}l l >{\sffamily}X }
#1
\end{tabularx}\right\}$%
}
\NewEnviron{messageWithProperties}[1]
{\noindent
\begin{tabularx}{\textwidth}{>{\bfseries}lX}
\toprule
\midrule
header & \innerMWP{%
\toprule
id & integer & desc \\
key & integer & desc \\
\bottomrule
}%
\\[4ex]
\midrule
#1 & \innerMWP{\toprule\BODY\bottomrule} \\[4ex]
\midrule
\bottomrule
\end{tabularx}%
}
\begin{document}
\begin{messageWithProperties}{someVeryLongName}
userId & string & desc \\
propertyName & veryLongStringType & a very very long description of the field \\
\end{messageWithProperties}
\end{document}
没有无用的括号:
\documentclass{article}
\usepackage[margin=3cm]{geometry}
\usepackage{array}
\usepackage{booktabs}
\usepackage[table]{xcolor}
\usepackage{tabularx,environ}
\newcommand{\innerMWP}[1]{%
\begin{tabularx}{\hsize}{ >{\bfseries}l l >{\sffamily}X }
#1
\end{tabularx}%
}
\NewEnviron{messageWithProperties}[1]
{\noindent
\begin{tabularx}{\textwidth}{>{\bfseries}lX}
\toprule\addlinespace
header & \innerMWP{%
\toprule
id & integer & desc \\
key & integer & desc \\
\bottomrule
}%
\\\addlinespace
\midrule
\addlinespace
#1 & \innerMWP{\toprule\BODY\bottomrule} \\
\addlinespace
\bottomrule
\end{tabularx}%
}
\begin{document}
\begin{messageWithProperties}{someVeryLongName}
userId & string & desc \\
propertyName & veryLongStringType & a very very long description of the field \\
\end{messageWithProperties}
\end{document}
答案2
我不知道这是否是一个合适的解决方案,但我发现 这里获得单元格宽度的方式,我已经使用此宽度来设置嵌套表列的宽度:
\documentclass{article}
\usepackage[margin=3cm]{geometry}
\usepackage{array}
\usepackage{booktabs}
\usepackage[table]{xcolor}
\usepackage{tabularx}
% These code lines are from: https://tex.stackexchange.com/a/30673/101651
\makeatletter
\newcommand\cellwidth{\TX@col@width}
\makeatother
\begin{document}
\newenvironment{messageWithProperties}[1]
{
\noindent
\tabularx{\textwidth}{>{\bfseries}lX}
\toprule
\midrule
header &
$\left\{\tabular{>{\bfseries}p{.24\cellwidth} p{.27\cellwidth} >{\sffamily}p{.34\cellwidth}}
\toprule
id & integer & desc \\
key & integer & desc \\
\bottomrule
\endtabular\right\}$ \\[4ex]
\midrule
#1 &
$\left\{\tabular{>{\bfseries}p{.24\cellwidth} p{.27\cellwidth} >{\sffamily}p{.34\cellwidth}}
\toprule
}
{
\bottomrule
\endtabular\right\}$ \\[4ex]
\midrule
\bottomrule
\endtabularx
}
\begin{messageWithProperties}{someVeryLongName}
userId & string & desc \\
propertyName & veryLongStringType & a very very long description of the field \\
\end{messageWithProperties}
\end{document}
PS = 我以前从未做过嵌套表,也没有创建过新环境,因此我将其作为练习来回答。