如何才能正确对齐表格下的两个小页面?

如何才能正确对齐表格下的两个小页面?

我有一张表格,我想在下面放置两个图例。这些是小页面(我找不到其他方法来完成此操作),但我很难将它们对齐到页面的中心和相同的高度。vspace 和 hspace 不会将相关的小页面向上或向下(或向左或向右)移动,但会使页面上的内容不可预测地跳动。我该如何正确对齐它们。

\documentclass{article}
\usepackage[table]{xcolor}
\def\hbar#1#2{%%
   {\color{#2}\rule{4mm}{#1mm}}}
\def\legbox#1#2{%%
   {\color{#1}\rule{4mm}{4mm}} \hspace{0.4mm}#2 \hspace{3mm}\\}

\begin{document}

\begin{figure}[h!]
  \vspace*{-4.0cm}
  \hspace*{-4.2cm}
  \centering
  {\setlength{\extrarowheight}{50pt}%
  \begin{tabular}{lllccl}
  \arrayrulecolor{gray}\hline
  \hline
    Item Description Name & Most Recent Update & State District & Governmental Body & Bars & More bars \\
    \hline
      Item 1 & Jan 1, 2010 & District 1 & GB 3 &
      \hbar{14}{green} \hbar{20}{green} \hbar{10}{green} \hbar{15}{red}
      \hbar{19}{red} \hbar{6}{red} \hbar{10}{red}  &
      \hbar{15}{red} \hbar{19}{red} \hbar{6}{red}    \\   
      Item 2 & Jan 1, 2010 & District 1 & GB 3 &
      \hbar{14}{green} \hbar{20}{green} \hbar{10}{green} \hbar{15}{red}
      \hbar{19}{red} \hbar{6}{red} \hbar{10}{red}  &
      \hbar{15}{red} \hbar{19}{red} \hbar{6}{red}      \\ 
      Item 3 & Jan 1, 2010 & District 1 & GB 3 &
      \hbar{14}{green} \hbar{20}{green} \hbar{10}{green} \hbar{15}{red}
      \hbar{19}{red} \hbar{6}{red} \hbar{10}{red}  &
      \hbar{15}{red} \hbar{19}{red} \hbar{6}{red}       \\
      Item 4 & Jan 1, 2010 & District 1 & GB 3 &
      \hbar{14}{green} \hbar{20}{green} \hbar{10}{green} \hbar{15}{red}
      \hbar{19}{red} \hbar{6}{red} \hbar{10}{red}  &
      \hbar{15}{red} \hbar{19}{red} \hbar{6}{red}       \\
      Item 5 & Jan 1, 2010 & District 1 & GB 3 &
      \hbar{14}{green} \hbar{20}{green} \hbar{10}{green} \hbar{15}{red}
      \hbar{19}{red} \hbar{6}{red} \hbar{10}{red}  &
      \hbar{15}{red} \hbar{19}{red} \hbar{6}{red}       \\
  \end{tabular}
  }
  \caption{Here goes a table caption to describe the table}
\hspace{-10cm}
\begin{minipage}[t]{5cm}
\vspace{1cm}
 \textbf{Legend 1} \\
\legbox{black}{Long Label 1}
\legbox{blue}{Label 2}
\legbox{black}{Long Label 3}
\legbox{blue}{Label 4}
\legbox{black}{Long Label 5}
\legbox{blue}{Label 6}
\legbox{black}{Long Label 7}
\end{minipage}

\begin{minipage}{6cm}
\hspace{8cm}
\vspace{-4.7cm}
\\\textbf{Legend 2} \\

\legbox{blue}{Another label}
\legbox{black}{Mini}
\legbox{green}{A pretty long label}
\legbox{green}{A label}
\end{minipage}

\end{figure}
\end{document}

它看起来是这样的:

当前布局

以下是我希望它看起来的样子(线条仅用于说明目的):

首选布局

答案1

或许这就是你所希望的:

\documentclass{article}
\usepackage[table]{xcolor}
\def\hbar#1#2{%%
   {\color{#2}\rule{4mm}{#1mm}}}
\def\legbox#1#2{%%
   {\color{#1}\rule{4mm}{4mm}} \hspace{0.4mm}#2 \hspace{3mm}\\}

\begin{document}

\begin{figure}[h!]
  \centering
  \makebox[0pt]
  {%%
    \setlength{\extrarowheight}{50pt}%
    \begin{tabular}{lllccl}
    \arrayrulecolor{gray}\hline
    \hline
      Item Description Name & Most Recent Update & State District & Governmental Body & Bars & More bars \\
      \hline
        Item 1 & Jan 1, 2010 & District 1 & GB 3 &
        \hbar{14}{green} \hbar{20}{green} \hbar{10}{green} \hbar{15}{red}
        \hbar{19}{red} \hbar{6}{red} \hbar{10}{red}  &
        \hbar{15}{red} \hbar{19}{red} \hbar{6}{red}    \\   
        Item 2 & Jan 1, 2010 & District 1 & GB 3 &
        \hbar{14}{green} \hbar{20}{green} \hbar{10}{green} \hbar{15}{red}
        \hbar{19}{red} \hbar{6}{red} \hbar{10}{red}  &
        \hbar{15}{red} \hbar{19}{red} \hbar{6}{red}      \\ 
        Item 3 & Jan 1, 2010 & District 1 & GB 3 &
        \hbar{14}{green} \hbar{20}{green} \hbar{10}{green} \hbar{15}{red}
        \hbar{19}{red} \hbar{6}{red} \hbar{10}{red}  &
        \hbar{15}{red} \hbar{19}{red} \hbar{6}{red}       \\
        Item 4 & Jan 1, 2010 & District 1 & GB 3 &
        \hbar{14}{green} \hbar{20}{green} \hbar{10}{green} \hbar{15}{red}
        \hbar{19}{red} \hbar{6}{red} \hbar{10}{red}  &
        \hbar{15}{red} \hbar{19}{red} \hbar{6}{red}       \\
        Item 5 & Jan 1, 2010 & District 1 & GB 3 &
        \hbar{14}{green} \hbar{20}{green} \hbar{10}{green} \hbar{15}{red}
        \hbar{19}{red} \hbar{6}{red} \hbar{10}{red}  &
        \hbar{15}{red} \hbar{19}{red} \hbar{6}{red}       \\
    \end{tabular}
  }
  \caption{Here goes a table caption to describe the table}
  \vspace{2ex}\par
  \begin{minipage}[t]{5cm}
    {\centering\textbf{Legend 1}}%%

    \legbox{black}{Long Label 1}
    \legbox{blue}{Label 2}
    \legbox{black}{Long Label 3}
    \legbox{blue}{Label 4}
    \legbox{black}{Long Label 5}
    \legbox{blue}{Label 6}
    \legbox{black}{Long Label 7}
  \end{minipage}%%
  \hspace{\fill}
  \begin{minipage}[t]{6cm}
    {\centering\textbf{Legend 2}}%%

    \legbox{blue}{Another label}
    \legbox{black}{Mini}
    \legbox{green}{A pretty long label}
    \legbox{green}{A label}
  \end{minipage}%%
\end{figure}
\end{document}

在此处输入图片描述

我能够消除你的大部分\hspace命令\vspace

您的表格比页面几何形状允许的尺寸大。您可以将其主体放在宽度\makebox为的范围内,使其在页面上居中0pt。本质上,我替换了您的代码:

\begin{figure}[h!]
  \vspace*{-4.0cm}
  \hspace*{-4.2cm}
  \centering
  {\setlength{\extrarowheight}{50pt}%

使用以下代码:

\begin{figure}[h!]
  \centering
  \makebox[0pt]
  {%%
    \setlength{\extrarowheight}{50pt}%

您还大量使用\hspace图例\vspace,但这种方式似乎没有必要。以下是我所做的一些调整:

  • \caption我添加了垂直空间之后,为了更好地控制标题和图例之间的空间,而不是在minipages 内添加垂直空间。
  • minipage图例的 s 中,我有点不确定您想用\hspace\vspace命令实现什么目的。似乎您可以通过在图例标题和正文之间设置段落分隔符并将标题居中来完全避免这些问题。

答案2

该表格无法适应任何合理的页面宽度,因此我将其缩小了。

对于图例,只需使用几个tabular

我还更改了您的\definto\newcommand\hbarinto,\HBar以免与已经存在的命令冲突。非常小心谨慎\def

\documentclass{article}
\usepackage{booktabs,makecell}
\usepackage[table]{xcolor}

\newcommand\HBar[2]{%%
   {\color{#2}\rule{3mm}{#1mm}}%
}
\newcommand\legbox[2]{%%
   {\color{#1}\rule{4mm}{4mm}}\hspace{0.4mm}#2\hspace{3mm}%
}

\begin{document}

\begin{figure}[htp]
\centering
\begingroup\small
\addtolength{\tabcolsep}{-2.8pt}
\begin{tabular}{@{}lllccc@{}}
\toprule
\makecell{Item \\ Description \\ Name} &
  \makecell{Most Recent \\ Update} &
  \makecell{State \\ District} &
  \makecell{Governmental \\ Body} &
  Bars &
  More bars \\
\midrule
\addlinespace
Item 1 & Jan 1, 2010 & District 1 & GB 3 &
\HBar{14}{green} \HBar{20}{green} \HBar{10}{green} \HBar{15}{red}
\HBar{19}{red} \HBar{6}{red} \HBar{10}{red}  &
\HBar{15}{red} \HBar{19}{red} \HBar{6}{red}    \\
\addlinespace
Item 2 & Jan 1, 2010 & District 1 & GB 3 &
\HBar{14}{green} \HBar{20}{green} \HBar{10}{green} \HBar{15}{red}
\HBar{19}{red} \HBar{6}{red} \HBar{10}{red}  &
\HBar{15}{red} \HBar{19}{red} \HBar{6}{red}      \\ 
\addlinespace
Item 3 & Jan 1, 2010 & District 1 & GB 3 &
\HBar{14}{green} \HBar{20}{green} \HBar{10}{green} \HBar{15}{red}
\HBar{19}{red} \HBar{6}{red} \HBar{10}{red}  &
\HBar{15}{red} \HBar{19}{red} \HBar{6}{red}       \\
\addlinespace
Item 4 & Jan 1, 2010 & District 1 & GB 3 &
\HBar{14}{green} \HBar{20}{green} \HBar{10}{green} \HBar{15}{red}
\HBar{19}{red} \HBar{6}{red} \HBar{10}{red}  &
\HBar{15}{red} \HBar{19}{red} \HBar{6}{red}       \\
\addlinespace
Item 5 & Jan 1, 2010 & District 1 & GB 3 &
\HBar{14}{green} \HBar{20}{green} \HBar{10}{green} \HBar{15}{red}
\HBar{19}{red} \HBar{6}{red} \HBar{10}{red}  &
\HBar{15}{red} \HBar{19}{red} \HBar{6}{red}       \\
\bottomrule
\end{tabular}
\endgroup

\caption{Here goes a table caption to describe the table}

\bigskip

\begin{tabular}[t]{@{}l@{}}
\multicolumn{1}{c}{\textbf{Legend 1}} \\
\legbox{black}{Long Label 1} \\
\legbox{blue}{Label 2} \\
\legbox{black}{Long Label 3} \\
\legbox{blue}{Label 4} \\
\legbox{black}{Long Label 5} \\
\legbox{blue}{Label 6} \\
\legbox{black}{Long Label 7} \\
\end{tabular}%
\hspace{1cm}
\begin{tabular}[t]{@{}l@{}}
\multicolumn{1}{c}{\textbf{Legend 2}} \\
\legbox{blue}{Another label} \\
\legbox{black}{Mini} \\
\legbox{green}{A pretty long label} \\
\legbox{green}{A label} \\
\end{tabular}

\end{figure}
\end{document}

在此处输入图片描述

相关内容