如何垂直對齊我的桌面頂部、頂部、中間的三列

如何垂直對齊我的桌面頂部、頂部、中間的三列

我希望第三列中的公式垂直居中對齊,即公式應垂直位於描述的兩行中間。

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

相關內容