我希望第三列中的公式垂直对齐到中间,即公式应该垂直位于两行描述的中间。
\documentclass{article}
\usepackage{amsmath}
\usepackage{tabularx,booktabs}
\usepackage{caption}
\begin{document}
\begin{table}[htb]
\centering
\caption{I would like the formulae here to be vertically at the middle of the two lines of text}
\begin{tabularx}{\linewidth}{lX>{$}c<{$}}
\toprule
Name & Description & \multicolumn{1}{c}{Formula}\\
\midrule
Ducks & Something which goes on two lines, something which goes on two lines &
A=\dfrac{B}{C}\\
Lions & Something which goes on two lines, something which goes on two lines &
D=\dfrac{E}{F}\\
\bottomrule
\end{tabularx}
\end{table}
\begin{table}[htb]
\centering
\caption{Here the formulae are correctly aligned with the description, but I would like the name at the top like in the previous table}
\begin{tabular}{lm{24em}>{$}c<{$}}
\toprule
Name & Description & \multicolumn{1}{c}{Formula}\\
\midrule
Ducks & Something which goes on two lines, something which goes on two lines &
A=\dfrac{B}{C}\\
Lions & Something which goes on two lines, something which goes on two lines &
D=\dfrac{E}{F}\\
\bottomrule
\end{tabular}
\end{table}
\end{document}
答案1
这里有一个基于X
列类型的重新定义的解决方案,以及针对第一列的两种可能的破解方法(它们可能必须适应实际内容):
\documentclass{article}
\usepackage{amsmath}
\usepackage{tabularx, booktabs, makecell}
\renewcommand{\tabularxcolumn}[1]{m{#1}}
\begin{document}
\begin{table}[htb]
\centering
\begin{tabularx}{\linewidth}{lX>{$}c<{$}}
\toprule
Name & Description & \multicolumn{1}{c}{Formula}\\
\midrule
\makecell[l]{Ducks\\\mbox{}} & Something which goes on two lines, something which goes on two lines &
A=\dfrac{B}{C}\\
\addlinespace
\makecell[l]{Lions\\\mbox{}} & Something which goes on two lines, something which goes on two lines &
D=\dfrac{E}{F}\\
\addlinespace
\raisebox{1.4ex}{Lions} & Something which goes on two lines, something which goes on two lines &
D=\dfrac{E}{F}\\
\bottomrule
\end{tabularx}
\end{table}
\end{document}
答案2
这相当困难:问题在于你想以两种方式对齐中间框:左侧沿基线对齐,右侧沿中心对齐。这种类型的表格通常需要嵌套表格(使用 tabularx 很难实现)或测量部分内容以使用 \llap 技巧。
这里我们缺少基于 xcoffins 的具有多个句柄的表格代码......
\documentclass{article}
\usepackage{amsmath}
\usepackage{tabularx,booktabs}
\usepackage{caption,xcoffins}
\newlength\colA
\begin{document}
\begin{table}[htb]
\centering
\renewcommand{\tabularxcolumn}[1]{m{#1}}
\settowidth\colA{\begin{tabular}{l}Name\\Ducks\\Lions\end{tabular}}
\caption{I would like the formulae here to be vertically at the middle of the two lines of text}
\begin{tabularx}{\linewidth}{p{\dimexpr\colA-2\tabcolsep}X>{$}c<{$}}
\toprule
Name & Description & \multicolumn{1}{c}{Formula}\\
\midrule
&\leavevmode\llap{\makebox[\colA][l]{Ducks}}Something which goes on two lines, something which goes on two lines &
A=\dfrac{B}{C}\\
&\leavevmode\llap{\makebox[\colA][l]{Lions}}Something which goes on two lines, something which goes on two lines and on one more line and on one more line and on one more line &
D=\dfrac{E}{F}\\
\bottomrule
\end{tabularx}
\end{table}
%Only for show some coffin code:
\NewCoffin\CoffinA
\NewCoffin\CoffinB
\NewCoffin\CoffinC
\SetHorizontalCoffin\CoffinA{Ducks}
\SetHorizontalCoffin\CoffinC{$D=\dfrac{E}{F}$}
\SetVerticalCoffin\CoffinB{\dimexpr \textwidth-\CoffinWidth\CoffinA-\CoffinWidth\CoffinC-6\tabcolsep}{\noindent Something which goes on two lines, something which goes on two lines and on one more line and on one more line and on one more line}
\JoinCoffins\CoffinA[H,r]\CoffinB[T,l](2\tabcolsep,0pt)
\JoinCoffins\CoffinA[vc,r]\CoffinC[vc,l](2\tabcolsep,0pt)
\noindent\hspace*{\tabcolsep}\TypesetCoffin\CoffinA
\end{document}
答案3
像这样:
编辑:
列内容的位置由行基线决定,但(不幸的是)行基线不能在列之间更改。
到目前为止,除了在最后一列或第一列中使用对齐基线的框外,我还没有看到任何其他可能性。 适合您的特定情况的良好候选者是\adjustbox
:
\documentclass{article}
\usepackage{amsmath}
\usepackage{tabularx,booktabs}
\usepackage{caption}
\usepackage{adjustbox}
\begin{document}
\begin{table}[htb]
\centering
\caption{I would like the formulae here to be vertically at the middle of the two lines of text}
\begin{tabularx}{\linewidth}{lXc}
\toprule
Name & Description & Formula \\
\midrule
Ducks & Something which goes on two lines, something which goes on two lines &
\adjustbox{valign=t}{$A=\dfrac{B}{C}$}\\
Lions & Something which goes on two lines, something which goes on two lines &
\adjustbox{valign=t}{$D=\dfrac{E}{F}$}\\
\bottomrule
\end{tabularx}
\end{table}
\end{document}
为了更加方便的书写表格,可以定义新的列类型:
\newcolumntype{E}{>{\begin{adjustbox}{valign=t}$}c<{$\end{adjustbox}}}
然后将表格(正文)写为:
\begin{tabularx}{\linewidth}{lXE}
\toprule
Name & Description & Formula \\
\midrule
Ducks & Something which goes on two lines, something which goes on two lines &
A=\dfrac{B}{C}\\
Lions & Something which goes on two lines, something which goes on two lines &
D=\dfrac{E}{F}\\
\bottomrule
\end{tabularx}
如果中间列单元格中的文本有任意行数,或者公式只有一行,结果会更糟。在这种情况下,一个可能的解决方案是multirow
在第一列中使用并手动调整这些单元格跨越的列数,并在第二列中将基线设置在垂直中间:
\documentclass{article}
\usepackage{amsmath}
\usepackage{booktabs, multirow, tabularx}
\usepackage{caption}
\usepackage{adjustbox}
\usepackage{lipsum}
\begin{document}
\begin{table}[htb]
\centering
\renewcommand\tabularxcolumn[1]{m{#1}} % <---
\caption{I would like the formulae here to be vertically at the middle of the two lines of text}
\begin{tabularx}{\linewidth}{lX>{$}c<{$}}
\toprule
Name & Description & Formula \\
\midrule
\multirow{-6}{*}{Ducks} % <--- manually adjusted
& \lipsum[66] & A=\dfrac{B}{C}\\
\bottomrule
\end{tabularx}
\end{table}
\end{document}
或者使用@Bernard 的答案中提出的解决方案。
答案4
为了全面了解各种可能性,我提供了一个表格,卡路里。使用 可以很容易地实现此类对齐calstable
。我已删除左侧和右侧边距,这会增加代码,但这对于创建 OP 的表格来说并非绝对必要:
\documentclass{article}
\usepackage{cals, caption, amsmath}
\usepackage{lmodern}
\begin{document}
\begin{table*}
\caption{I would like the formulae here to be vertically at the middle of the two lines of text}
\begin{calstable}[c]
% Defining columns relative to each other and relative to the margins
\colwidths{{\dimexpr(\columnwidth)/6\relax}
{\dimexpr(\columnwidth)/6*4\relax}
{\dimexpr(\columnwidth)/6\relax}
}
% Set up the tabular
\makeatletter
\def\cals@framers@width{0.8pt} % Outside frame rules, reduce if the rule is too heavy
\def\cals@bodyrs@width{0.6pt}
\def\cals@cs@width{0pt} % Inside rules, reduce if the rule is too heavy
\def\cals@rs@width{0.4pt}
\def\bb{\ifx\cals@borderB\relax % Botton border switch (off-on)
\def\cals@borderB{0pt}
\else \let\cals@borderB\relax\fi}
\def\lp{\ifdim\cals@paddingL=0.0pt\relax % Left padding switch (off-on)
\cals@setpadding{Ag}
\else \setlength{\cals@paddingL}{0pt}\fi}
\def\rp{\ifdim\cals@paddingR=0.0pt\relax % Right padding switch (off-on)
\cals@setpadding{Ag}
\else \setlength{\cals@paddingR}{0pt}\fi}
% R1H1
\thead{\small%
\brow
\lp\alignL\cell{Name}\lp
\alignL\cell{Description}
\rp\alignR\cell{Formula}\rp
\erow
\normalsize%
}
\tfoot{\lastrule\strut}
%R2B1
\brow
\lp\bb\alignL\cell{Ducks}\lp
\alignL\cell{Something which goes on two lines, something which goes on two lines}
\rp\alignR\cell{\vfil$A=\dfrac{B}{C}$}\rp
\erow
%R3B2
\brow
\bb\lp\alignL\cell{Gnus}\lp
\alignL\cell{Something which goes on two lines, something which goes on two lines and on one more line and on one more line and on one more line}
\rp\alignR\cell{\vfil$D=\dfrac{E}{F}$}\rp\bb
\erow
%R4B3
\brow
\lp\alignL\cell{Gnats}\lp
\alignL\cell{Something which goes on two lines, something which goes on two lines and on one more line and on one more line and on one more line}
\rp\alignR\cell{\vfil $D=\dfrac{E}{F}$}\rp
\erow
\makeatletter
\end{calstable}\par
\end{table*}
\end{document}