标题在顶部,表格垂直对齐在顶部

标题在顶部,表格垂直对齐在顶部

我是 LaTex 的新手,很难完成一些简单的工作。以下代码对我来说有两个错误:

  1. 在表格左侧生成标题
  2. 不同大小的表格不垂直对齐

有人能帮忙解决上述两个问题吗?

\documentclass[10pt,a4paper,oneside]{report}
\usepackage[a4paper,left=0.75in,right=0.75in,top=.5in,bottom=1in]{geometry}
\usepackage{graphicx}
\usepackage{titlesec}
\usepackage{longtable}
\usepackage{array}
\usepackage{float}

\begin{document}

\begin{table}[H]
    \centering
    \resizebox{\columnwidth}{!} {%
        \begin{tabular}{lll}

            Heading 1
            \begin{tabular}{ @{}l l@{} l@{} }
                \textbf{\scalebox{.7}[1.0]{\enspace asd}} \quad&    \textbf{\scalebox{.7}[1.0]{\enspace LMN\quad}} &   \textbf{\scalebox{.7}[1.0]{\enspace xyz \quad}} \\\hline  
                \enspace \tt{MYSTRING} \quad & \quad {A} & \quad {A} \\   
                \enspace \tt{MYSTRING} & \quad {B} & \quad {B} \\  
                \enspace \tt{MYSTRING} & \quad {J} & \quad {J} \\  
                \enspace \tt{MYSTRING} & \quad {K} & \quad {K} \\
                \hline
                \multicolumn{2}{l}{\textbf{Message 1}} & \textbf{Messsage 2} \\
            \end{tabular}   

            &

            Heading 2
            \begin{tabular}{ @{}l l@{} l@{} }
                \textbf{\scalebox{.7}[1.0]{\enspace asd}} \quad&    \textbf{\scalebox{.7}[1.0]{\enspace LMN\quad}} &   \textbf{\scalebox{.7}[1.0]{\enspace xyz \quad}} \\\hline  
                \enspace \tt{MYSTRING} \quad & \quad {A} & \quad {A} \\   
                \enspace \tt{MYSTRING} & \quad {B} & \quad {B} \\  
                \enspace \tt{MYSTRING} & \quad {C} & \quad {C} \\  
                \enspace \tt{MYSTRING} & \quad {D} & \quad {D} \\   
                \enspace \tt{MYSTRING} & \quad {E} & \quad {E} \\  
                \enspace \tt{MYSTRING} & \quad {F} & \quad {F} \\  
                \enspace \tt{MYSTRING} & \quad {G} & \quad {G} \\  
                \enspace \tt{MYSTRING} & \quad {H} & \quad {H} \\   
                \enspace \tt{MYSTRING} & \quad {I} & \quad {I} \\  
                \enspace \tt{MYSTRING} & \quad {J} & \quad {J} \\  
                \enspace \tt{MYSTRING} & \quad {K} & \quad {K} \\
                \hline
                \multicolumn{2}{l}{\textbf{Message 1}} & \textbf{Messsage 2} \\
            \end{tabular}   

            &

            Heading 3
            \begin{tabular}{ @{}l l@{} l@{} }
                \textbf{\scalebox{.7}[1.0]{\enspace asd}} \quad&    \textbf{\scalebox{.7}[1.0]{\enspace LMN\quad}} &   \textbf{\scalebox{.7}[1.0]{\enspace xyz \quad}} \\\hline  
                \enspace \tt{MYSTRING} \quad & \quad {A} & \quad {A} \\   
                \enspace \tt{MYSTRING} & \quad {B} & \quad {B} \\  
                \hline
                \multicolumn{2}{l}{\textbf{Message 1}} & \textbf{Messsage 2} \\
            \end{tabular}   


        \end{tabular}%
    }
\end{table}

\end{document}

答案1

一些建议和意见:

  • tabular环境中不需要嵌套环境table。单层tabulars 就足够了。使用[t]位置说明符来通知 LaTeX 它们应该彼此顶部对齐。使用\hspace{\fill}指令来最大化表格之间的垂直空白量。

  • 放置标题行里面相应的tabular环境,并通过包装器将它们置于三列的中心\multicolumn{3}{c}{...}

  • 据我所知,不需要 outer\resizebox指令或任何指令\scalebox。我还建议您删除所有 26 个 [!] 实例\enspace和所有 46 个 [!!] 实例\quad。如果这些指令是为了更改列间空白量而存在的,请了解如何修改参数\tabcolsep

  • A为了进一步减少代码混乱,请删除所有围绕字母、B等的花括号。

  • 由于每个环境的第一列的大部分内容都tabular需要使用等宽字体排版,因此只需将相应的列类型定义为 而>{\ttfamily}l不是仅仅来告知 LaTeX 这一事实l

在此处输入图片描述


\documentclass[10pt,a4paper,oneside]{report}
\usepackage[hmargin=0.75in,top=.5in,bottom=1in]{geometry}
\usepackage{array}

\begin{document}

\begin{table}
\begin{tabular}[t]{@{} >{\ttfamily}lll @{}}
\multicolumn{3}{c}{Heading 1}\\[2ex]
\multicolumn{1}{@{}l}{asd} & LMN & xyz \\
\hline  
MYSTRING & A & A \\   
MYSTRING & B & B \\  
MYSTRING & J & J \\  
MYSTRING & K & K \\
\hline
\multicolumn{2}{@{}l}{\textbf{Message 1}} & \textbf{Messsage 2} \\
\end{tabular}   
\hspace{\fill}
\begin{tabular}[t]{@{} >{\ttfamily}lll @{}}
\multicolumn{3}{c}{Heading 2} \\[2ex]
\multicolumn{1}{@{}l}{asd} & LMN & xyz \\
\hline  
MYSTRING & A & A \\   
MYSTRING & B & B \\  
MYSTRING & C & C \\  
MYSTRING & D & D \\   
MYSTRING & E & E \\  
MYSTRING & F & F \\  
MYSTRING & G & G \\  
MYSTRING & H & H \\   
MYSTRING & I & I \\  
MYSTRING & J & J \\  
MYSTRING & K & K \\
\hline
\multicolumn{2}{@{}l}{\textbf{Message 1}} & \textbf{Messsage 2} \\
\end{tabular}   
\hspace{\fill}
\begin{tabular}[t]{@{} >{\ttfamily}lll @{}}
\multicolumn{3}{c}{Heading 3} \\[2ex]
\multicolumn{1}{@{}l}{asd} & LMN & xyz \\\hline  
MYSTRING & A & A \\   
MYSTRING & B & B \\  
\hline
\multicolumn{2}{@{}l}{\textbf{Message 1}} & \textbf{Messsage 2} \\
\end{tabular}   
\end{table}
\end{document}

答案2

与使用表格环境来并排列出表格相比,使用 minipages 通常更好,尤其是在环境中table。这也使对齐表格变得更容易。除此之外,我认为您有几件事情是不必要的。例如,您使用 删除列之间的空格,@{}然后使用 和 添加新空格\enspace\quad您应该避免\tt使用 和\texttt。我已经以更接近我将在下面展示的方式重写了表格。

\documentclass[10pt,a4paper,oneside]{report}
\usepackage[a4paper,left=0.75in,right=0.75in,top=.5in,bottom=1in]{geometry}
\usepackage{graphicx}
\usepackage{titlesec}
\usepackage{longtable}
\usepackage{array}
\usepackage{float}

\begin{document}

\begin{table}[h]
  \resizebox{\linewidth}{!}{%
    \mbox{\begin{minipage}[t]{0.4\linewidth}
        \caption{Heading 1}
        \begin{tabular}[t]{lll}
          \textbf{asd} & \textbf{LMN} & \textbf{xyz} \\ \hline  
          \texttt{MYSTRING} & A & A \\   
          \texttt{MYSTRING} & B & B \\  
          \texttt{MYSTRING} & J & J \\  
          \texttt{MYSTRING} & K & K \\ \hline
          \multicolumn{2}{l}{\textbf{Message 1}} & \textbf{Messsage 2}
        \end{tabular}             
      \end{minipage}}%
    \mbox{\begin{minipage}[t]{0.4\linewidth}
        \caption{Heading 2}
        \begin{tabular}{lll}
          \textbf{asd} & \textbf{LMN} & \textbf{xyz} \\\hline  
          \texttt{MYSTRING} & A & A \\   
          \texttt{MYSTRING} & B & B \\  
          \texttt{MYSTRING} & C & C \\  
          \texttt{MYSTRING} & D & D \\   
          \texttt{MYSTRING} & E & E \\  
          \texttt{MYSTRING} & F & F \\  
          \texttt{MYSTRING} & G & G \\  
          \texttt{MYSTRING} & H & H \\   
          \texttt{MYSTRING} & I & I \\  
          \texttt{MYSTRING} & J & J \\  
          \texttt{MYSTRING} & K & K \\ \hline
          \multicolumn{2}{l}{\textbf{Message 1}} & \textbf{Messsage 2}
        \end{tabular}   
      \end{minipage}}%
    \mbox{\begin{minipage}[t]{0.4\linewidth}
        \caption{Heading 3}
        \begin{tabular}{lll}
          \textbf{asd} & \textbf{LMN} & \textbf{xyz} \\ \hline  
          \texttt{MYSTRING} & A & A \\   
          \texttt{MYSTRING} & B & B \\ \hline
          \multicolumn{2}{l}{\textbf{Message 1}} & \textbf{Messsage 2} \\
        \end{tabular}         
      \end{minipage}}
  }
\end{table}

\end{document}

在此处输入图片描述

答案3

我不确定我是否完全理解你想要什么,尤其是列标题和所有这些\resizebox,但这里有一个建议,带有subcaptionbooktabs包。我相信它可以进一步改进。

\documentclass[10pt,a4paper,oneside]{report}
\usepackage[a4paper,left=0.75in,right=0.75in,top=.5in,bottom=1in, showframe]{geometry}
\usepackage{graphicx}
\usepackage{titlesec}
\usepackage{longtable}
\usepackage{array}
\usepackage{float, caption, subcaption, booktabs}

\begin{document}

\begin{table}[H]
 \centering\small
\begin{subtable}[t]{0.32\linewidth}
\centering
 \caption{Heading 1}
 \begin{tabular}[t]{ @{}l l@{} l@{} }
 \textbf{\scalebox{.7}[1.0]{\enspace asd}} \quad& \textbf{\scalebox{.7}[1.0]{\enspace LMN\quad}} & \textbf{\scalebox{.7}[1.0]{\enspace xyz \quad}} \\\toprule
 \enspace \tt{MYSTRING} \quad & \quad {A} & \quad {A} \\
 \enspace \tt{MYSTRING} & \quad {B} & \quad {B} \\
 \enspace \tt{MYSTRING} & \quad {J} & \quad {J} \\
 \enspace \tt{MYSTRING} & \quad {K} & \quad {K} \\
 \bottomrule
                \addlinespace
 \multicolumn{2}{l}{\textbf{Message 1}} & \textbf{Message 2} \\
 \end{tabular}
\end{subtable}
\hfill
\begin{subtable}[t]{0.32\linewidth}
\centering
\caption{Heading 2}
            \begin{tabular}{ @{}l l@{} l@{} }
                \textbf{\scalebox{.7}[1.0]{\enspace asd}} \quad& \textbf{\scalebox{.7}[1.0]{\enspace LMN\quad}} & \textbf{\scalebox{.7}[1.0]{\enspace xyz \quad}} \\\toprule
                \enspace \tt{MYSTRING} \quad & \quad {A} & \quad {A} \\
                \enspace \tt{MYSTRING} & \quad {B} & \quad {B} \\
                \enspace \tt{MYSTRING} & \quad {C} & \quad {C} \\
                \enspace \tt{MYSTRING} & \quad {D} & \quad {D} \\
                \enspace \tt{MYSTRING} & \quad {E} & \quad {E} \\
                \enspace \tt{MYSTRING} & \quad {F} & \quad {F} \\
                \enspace \tt{MYSTRING} & \quad {G} & \quad {G} \\
                \enspace \tt{MYSTRING} & \quad {H} & \quad {H} \\
                \enspace \tt{MYSTRING} & \quad {I} & \quad {I} \\
                \enspace \tt{MYSTRING} & \quad {J} & \quad {J} \\
                \enspace \tt{MYSTRING} & \quad {K} & \quad {K} \\
                \bottomrule
                \addlinespace
                \multicolumn{2}{l}{\textbf{Message 1}} & \textbf{Message 2} \\
            \end{tabular}
\end{subtable}
\hfill
\begin{subtable}[t]{0.32\linewidth}
\centering
            \caption{Heading 3}
            \begin{tabular}{ @{}l l@{} l@{} }
                \textbf{\scalebox{.7}[1.0]{\enspace asd}} \quad& \textbf{\scalebox{.7}[1.0]{\enspace LMN\quad}} & \textbf{\scalebox{.7}[1.0]{\enspace xyz \quad}} \\\toprule
                \enspace \tt{MYSTRING} \quad & \quad {A} & \quad {A} \\
                \enspace \tt{MYSTRING} & \quad {B} & \quad {B} \\
                \bottomrule
                \addlinespace
                \multicolumn{2}{l}{\textbf{Message 1}} & \textbf{Message 2} \\
            \end{tabular}
\end{subtable}
\end{table}

\end{document} 

在此处输入图片描述

答案4

还有更多解决方案...

在此处输入图片描述

在 mWE 中,makecell用于列标题、tabularx封闭(子)表和\caption{...}表格标题的包:

\documentclass[10pt,a4paper,oneside]{report}
\usepackage[a4paper,
            left=0.75in,right=0.75in,top=.5in,bottom=1in, 
            showframe]{geometry}
\usepackage{graphicx}
%\usepackage{titlesec}
%\usepackage{longtable}
\usepackage{booktabs, makecell, tabularx}
    \newcolumntype{C}{>{\centering\arraybackslash}X}
    \renewcommand\theadfont{\bfseries\normalfont}
\usepackage{caption, subcaption}

\begin{document}

\begin{table}[htb]
    \centering
    \small
\begin{tabularx}{\textwidth}{@{} CCC @{}}
    \caption{Heading 1}
    \begin{tabular}[t]{ @{}>{\ttfamily}l c c@{} }
\thead{asd} & \thead{LMN} & \thead{xyz} \\
    \toprule
MYSTRING    &   A   &   A               \\
MYSTRING    &   B   &   B               \\
MYSTRING    &   J   &   J               \\
MYSTRING    &   K   &   K               \\
    \bottomrule
\multicolumn{2}{l}{\textbf{Message 1}} & \textbf{Message 2}    \\
    \end{tabular}
    &   \caption{Heading 2}
        \begin{tabular}{ @{}>{\ttfamily}l c c@{} }
        \thead{asd} & \thead{LMN} & \thead{xyz} \\
        \toprule
        MYSTRING    &   B   &   B               \\
        MYSTRING    &   C   &   C               \\
        MYSTRING    &   D   &   D               \\
        MYSTRING    &   E   &   E               \\
        MYSTRING    &   F   &   F               \\
        MYSTRING    &   G   &   G               \\
        MYSTRING    &   H   &   H               \\
        MYSTRING    &   I   &   I               \\
        MYSTRING    &   J   &   J               \\
        MYSTRING    &   K   &   K               \\
    \bottomrule
\multicolumn{2}{l}{\textbf{Message 1}} & \textbf{Message 2}    \\
    \end{tabular}
        &   \caption{Heading 3}
            \begin{tabular}{ @{}>{\ttfamily}l c c@{} }
            \thead{asd} & \thead{LMN} & \thead{xyz} \\
            \toprule
            MYSTRING    &   A   &   A               \\
            MYSTRING    &   B   &   B               \\
        \bottomrule
    \multicolumn{2}{l}{\textbf{Message 1}} & \textbf{Message 2}    \\
            \end{tabular}
\end{tabularx}
\end{table}

\end{document} 

相关内容