如何将文本定位到表格上行所有列的顶部

如何将文本定位到表格上行所有列的顶部

如何让 '1'、'2'、'3'、'4' 和 'Supply'(即第一行中的条目(但在不同的列中))与 'Destination' 处于相同的高度(垂直位置)(即在列的顶部而不是列的中间)?由于使用 'diagbox' 选项,这些元素的垂直位置偏离。

\documentclass[12pt,fleqn]{article}
\usepackage{pict2e}
\usepackage{keyval}
\usepackage{calc}
\usepackage{fp}
\usepackage{diagbox}
\usepackage{booktabs}
\usepackage{tabularx,colortbl}
\begin{document}
\begin{table}[h!]
  \centering
  \caption[caption]{Table 1}
  \label{tab:table1}
\begin{tabular}{|l|cccc|c|}
\hline
\diagbox{\textbf{Source}}{\textbf{Destination}} & $1$ & $2$ & $3$ & $4$ & \textbf{Supply} \\
\hline
$1$ & $c_{11}$ & $c_{12}$ & $c_{13}$ & $c_{14}$ & $s_1$ \\
$2$ & $c_{21}$ & $c_{22}$ & $c_{23}$ & $c_{24}$ & $s_2$ \\
$3$ & $c_{31}$ & $c_{32}$ & $c_{33}$ & $c_{34}$ & $s_3$ \\
$4$ & $c_{41}$ & $c_{42}$ & $c_{43}$ & $c_{44}$ & $s_4$ \\
\hline
\textbf{Demand} & $d_1$ & $d_2$ & $d_3$ & $d_4$ & $T$\\
\hline
\end{tabular}
\end{table}
\end{document}

答案1

同时,当您等待所需的解决方案时,请查看以下表格的重新设计是否可以接受:

在此处输入图片描述

\documentclass[12pt,fleqn]{article}
%\usepackage{pict2e}       % not needed in this mwe
%\usepackage{keyval}       % not needed in this mwe
%\usepackage{calc}         % not needed in this mwe
%\usepackage{fp}           % not needed in this mwe
%\usepackage{diagbox}      % not needed in this mwe
\usepackage{booktabs,
            multirow,       % <--- new
            tabularx}       % <--- new
\usepackage[table]{xcolor}  % <--- changed from colortbl

\begin{document}
    \begin{table}[h!]
\caption[short caption]{long caption}
\label{tab:table1}
    \centering
\begin{tabular}{*{6}{>{$}c<{$}} }
    \toprule
\multirow{2.4}{*}{\textbf{Source}}
    & \multicolumn{4}{c}{\textbf{Destination}}
        & \multirow{2.4}{*}{\textbf{Supply}}    \\
    \cmidrule{2-5}
    &  1     & 2      & 3      & 4      &       \\
    \midrule
1   & c_{11} & c_{12} & c_{13} & c_{14} & s_1   \\
2   & c_{21} & c_{22} & c_{23} & c_{24} & s_2   \\
3   & c_{31} & c_{32} & c_{33} & c_{34} & s_3   \\
4   & c_{41} & c_{42} & c_{43} & c_{44} & s_4   \\
    \midrule
\textbf{Demand}
    & d_1    & d_2    & d_3    & d_4    & T     \\
    \bottomrule
\end{tabular}
    \end{table}
\end{document}

编辑:或者您更喜欢以下解决方案:

\documentclass[12pt,fleqn]{article}
\usepackage{diagbox}
\usepackage{booktabs,
            multirow}      % <--- new
\usepackage[table]{xcolor} % <--- changed from colortbl

\begin{document}
\begin{table}[h!]
  \centering
  \caption[caption]{Table 1}
  \label{tab:table1}
\begin{tabular}{|l|cccc|c|}
\hline
\multirow{2}{*}{
\diagbox{\textbf{Source}}{\textbf{Destination}}
                }
    & $1$ & $2$ & $3$ & $4$ & \textbf{Supply}   \\
    &     &     &     &     &                   \\
\hline
$1$ & $c_{11}$ & $c_{12}$ & $c_{13}$ & $c_{14}$ & $s_1$ \\
$2$ & $c_{21}$ & $c_{22}$ & $c_{23}$ & $c_{24}$ & $s_2$ \\
$3$ & $c_{31}$ & $c_{32}$ & $c_{33}$ & $c_{34}$ & $s_3$ \\
$4$ & $c_{41}$ & $c_{42}$ & $c_{43}$ & $c_{44}$ & $s_4$ \\
\hline
\textbf{Demand} & $d_1$ & $d_2$ & $d_3$ & $d_4$ & $T$\\
\hline
\end{tabular}
\end{table}
\end{document}

这使:

在此处输入图片描述

可能不难猜出我更喜欢哪种解决方案……

附录:

根据您在下面评论中的新问题...如果我理解正确的话,那么您正在寻找以下内容:

在此处输入图片描述

上表第一列使用了S包提供的列类型siunitx

\documentclass[12pt,fleqn]{article}
\usepackage{booktabs,
            multirow}       % <--- new
\usepackage[table]{xcolor}  % <--- instead of colortbl
\usepackage{siunitx}        % <--- new

\begin{document}
    \begin{table}[h!]
\caption[short caption]{long caption}
\label{tab:table1}
    \centering
\begin{tabular}{S[table-format=1.0,
                  table-space-text-post={*}]    % <--- new
                *{5}{>{$}c<{$}}
                }
    \toprule
{\multirow{2.4}{*}{\textbf{Source}}}
    & \multicolumn{4}{c}{\textbf{Destination}}
        & \multirow{2.4}{*}{\textbf{Supply}}    \\
    \cmidrule{2-5}
    &  1     & 2      & 3^*    & 4^\ast &       \\ % two cases how to write *
    \midrule
1   & c_{11} & c_{12} & c_{13} & c_{14} & s_1   \\
2   & c_{21} & c_{22} & c_{23} & c_{24} & s_2   \\
3*  & c_{31} & c_{32} & c_{33} & c_{34} & s_3   \\
4*  & c_{41} & c_{42} & c_{43} & c_{44} & s_4   \\
    \midrule
\textbf{Demand}
    & d_1    & d_2    & d_3    & d_4    & T     \\
    \bottomrule
\end{tabular}
    \end{table}
\end{document}

如果您*打算在表格中写一些注释,那么它也许threeparttable能帮助您。

答案2

对于某些 es 来说,这是完全有可能的\raisebox,但我不认为这样很好:

\documentclass[12pt,fleqn]{article}
\usepackage{pict2e}
\usepackage{keyval}
\usepackage{calc}
\usepackage{fp}
\usepackage{diagbox}
\usepackage{booktabs}
\usepackage{tabularx,colortbl, makecell, caption}

\begin{document}

\begin{table}[h!]
  \centering
  \caption[caption]{Table 1}
  \label{tab:table1}
\begin{tabular}{|l|*{4}{>{$}c<{$}}|>{$}c<{$}|}
\hline
\diagbox[innerwidth =40mm, height =15mm]%,innerleftsep = 12pt
{\raisebox{2.5ex}{\textbf{Source}}}{\raisebox{-0.5ex}{\textbf{Destination}}} & \raisebox{2.25ex}{$1$} & \raisebox{2.25ex}{$2$} & \raisebox{2.25ex}{$3$} & \raisebox{2.25ex}{$4$} & \raisebox{2.25ex}{\textbf{Supply}} \\
\hline
1 & c_{11} & c_{12} & c_{13} & c_{14} & s_1 \\
2 & c_{21} & c_{22} & c_{23} & c_{24} & s_2 \\
3 & c_{31} & c_{32} & c_{33} & c_{34} & s_3 \\
4 & c_{41} & c_{42} & c_{43} & c_{44} & s_4 \\
\hline
\textbf{Demand} & d_1 & d_2 & d_3 & d_4 & T\\
\hline
\end{tabular}
\end{table}

\end{document} 

在此处输入图片描述

相关内容