我不想使用嵌套表。我当前的代码产生以下结果。
我想要获得以下输出。
最小工作示例:
\documentclass{article}
\usepackage[a4paper,margin=1cm,showframe]{geometry}
\usepackage{array,multirow,longtable}
\parindent=0pt
\arrayrulewidth=1pt\relax
\tabcolsep=5pt\relax
\newcolumntype{A}[2]{%
>{\minipage{\dimexpr#1\linewidth-2\tabcolsep-#2\arrayrulewidth\relax}\vspace\tabcolsep}%
c<{\vspace\tabcolsep\endminipage}}
\newcommand\x{\centering$\displaystyle\int f(x)\,\textrm{d}x=F(x)+C$}
\begin{document}
\begin{longtable}{
|A{0.2}{1.5} % 0.2 of \linewidth, 1.5 of \arrayrulewidth
|A{0.5}{1} % 0.5 of \linewidth, 1 of \arrayrulewidth
|A{0.3}{1.5} % 0.3 of \linewidth, 1.5 of \arrayrulewidth
|}\hline
%%%%%%%%%%%%%%% FIRST ROW %%%%%%%%%%%%%%%
\x & \multicolumn{2}{
A{0.8}{1.5} % 0.8 of \linewidth, 1.5 of \arrayrulewidth
|}{\x} \tabularnewline\hline
%%%%%%%%%%%%%%% SECOND ROW %%%%%%%%%%%%%%%
\multicolumn{2}{
|A{0.7}{1.5} % 0.7 of \linewidth, 1.5 of \arrayrulewidth
|}{\multirow{2}{*}{\x}} &
\x \tabularnewline\cline{3-3}
%%%%%%%%%%%%%%% THIRD ROW %%%%%%%%%%%%%%%
\multicolumn{2}{
|A{0.7}{1.5} % 0.7 of \linewdith, 1.5 of \arrayrulewidth
|}{} &
\x \tabularnewline\hline
\end{longtable}
\end{document}
如何在不使用嵌套表的情况下使跨多行的内容垂直居中?
我尝试了所有答案(Aditya 的答案除外),但都没有将跨多行的单元格垂直居中。请重新检查您的答案,否则我可能错了。:-)
答案1
该软件包ltablex
可在 CTAN 上获得:http://www.ctan.org/tex-archive/macros/latex/contrib/ltablex
当您想要不同的列宽时,三个 X 列必须给出 3X:0.6+1.5+0.9=3 对于第二行和第三行,您有 2X+1X=3X
\documentclass{article}
\usepackage[a4paper,margin=1cm,showframe]{geometry}
\usepackage{multirow,bigstrut,ltablex,calc}
\parindent=0pt
\tabcolsep=5pt
\newcolumntype{A}[1]{>{\hsize=#1\hsize\rule{0pt}{\tabcolsep}\newline}
X
<{\newline\rule[-\tabcolsep]{0pt}{\tabcolsep}}}
\newcommand\x{\makebox[\hsize]{$\displaystyle\int f(x)\,\textrm{d}x=F(x)+C$}}
\begin{document}
\keepXColumns
\begin{tabularx}{\linewidth}{| A{0.6} | A{1.5} | A{0.9} |}\hline
\x & \multicolumn{2}{A{2.4}|}{\x} \tabularnewline\hline
%%%%%%%%%%%%%%% SECOND ROW %%%%%%%%%%%%%%%
\multicolumn{2}{|A{2}|}{\multirow{4}[8]{*}{\x}} & \x\tabularnewline\cline{3-3}
%%%%%%%%%%%%%%% THIRD ROW %%%%%%%%%%%%%%%
\multicolumn{2}{|A{2}|}{} & \x \tabularnewline\hline
\end{tabularx}
\end{document}
答案2
您可以通过对原始代码进行简单修改来获得所需的结果,将第二行替换\multirow{2}{*}{\x}
为:\hfil\multirow{4}{*}{\x}\hfil
\documentclass{article}
\usepackage[a4paper,margin=1cm,showframe]{geometry}
\usepackage{array,multirow,longtable}
\parindent=0pt
\arrayrulewidth=1pt\relax
\tabcolsep=5pt\relax
\newcolumntype{A}[2]{%
>{\minipage{\dimexpr#1\linewidth-2\tabcolsep-#2\arrayrulewidth\relax}\vspace\tabcolsep}%
c<{\vspace\tabcolsep\endminipage}}
\newcommand\x{\centering$\displaystyle\int f(x)\,\textrm{d}x=F(x)+C$}
\begin{document}
\begin{longtable}{
|A{0.2}{1.5} % 0.2 of \linewidth, 1.5 of \arrayrulewidth
|A{0.5}{1} % 0.5 of \linewidth, 1 of \arrayrulewidth
|A{0.3}{1.5} % 0.3 of \linewidth, 1.5 of \arrayrulewidth
|}\hline
%%%%%%%%%%%%%%% FIRST ROW %%%%%%%%%%%%%%%
\x & \multicolumn{2}{
A{0.8}{1.5} % 0.8 of \linewidth, 1.5 of \arrayrulewidth
|}{\x} \tabularnewline\hline
%%%%%%%%%%%%%%% SECOND ROW %%%%%%%%%%%%%%%
\multicolumn{2}{
|A{0.7}{1.5} % 0.7 of \linewidth, 1.5 of \arrayrulewidth
|}{\hfil\multirow{4}{*}{\x}\hfil} & \x \tabularnewline\cline{3-3}
%%%%%%%%%%%%%%% THIRD ROW %%%%%%%%%%%%%%%
\multicolumn{2}{
|A{0.7}{1.5} % 0.7 of \linewdith, 1.5 of \arrayrulewidth
|}{} & \x \tabularnewline\hline
\end{longtable}
\end{document}
答案3
对于排版这样的表格,我发现 ConTeXt 提供的界面更易于阅读。
\startsetups table:setup
\setupTABLE[each][each][align={middle,middle}, frame=on, framecolor=red]
\setupTABLE[column][1] [width=0.2\hsize]
\setupTABLE[column][2] [width=0.5\hsize]
\setupTABLE[column][3] [width=0.3\hsize]
\stopsetups
\startbuffer
$\displaystyle \int f(x)\,\normal{d}x=F(x)+C$
\stopbuffer
\starttext
\bTABLE[setups=table:setup]
\bTR
\bTD \getbuffer \eTD \bTD[nc=2] \getbuffer \eTD
\eTR
\bTR
\bTD[nc=2, nr=2] \getbuffer \eTD \bTD \getbuffer \eTD
\eTR
\bTR
\bTD \getbuffer \eTD
\eTR
\eTABLE
\stoptext
这给出
答案4
我提出的解决方案省去了minipages
明确的\vspace
指令。这是通过在每行中自动插入一个“支柱”来实现的;支柱的高度比数学表达式的高度高出一定量。
\documentclass{article}
\usepackage[a4paper,margin=1cm,showframe]{geometry}
\usepackage{array,multirow,tabularx}
\parindent=0pt
\arrayrulewidth=1pt
\tabcolsep=5pt
%% 'rbstrut' is short for 'really big strut'
\newcommand{\rbstrut}{\ensuremath{\vphantom{\displaystyle \int\limits_a^t}}}
\newcolumntype{A}[1]{>{\hsize=#1\hsize \centering \arraybackslash\rbstrut}X}
\newcommand\x{\ensuremath{\displaystyle\int f(x)\,\textrm{d}x=F(x)+C}}
\begin{document}
%% in tabularx terminology, the sum of the widths (arguments of A) have to
%% add up to the integer (in this case 3) that equals the number of columsn
\begin{tabularx}{\textwidth}{| A{0.6} | A{1.5} | A{0.9} |}
\hline
%%%% FIRST ROW
\x & \multicolumn{2}{c|}{\x} \\ \hline
%%%% SECOND ROW
\multicolumn{2}{|A{2.1}|}{ \multirow{4}{*}{\x} } & \x \\ \cline{3-3}
%%%% THIRD ROW
\multicolumn{2}{|c|}{} & \x \\ \hline
\end{tabularx}
\end{document}