如何使表格和圖形並排出現在同一高度?

如何使表格和圖形並排出現在同一高度?

我編寫了以下程式碼來使表格和圖形並排顯示:

    \documentclass{article}
    \usepackage{graphicx}
    \usepackage{capt-of}% or \usepackage{caption}
    \usepackage{booktabs}
    \usepackage{varwidth}

    \begin{document}
    \begin{table}[ht]
    \begin{minipage}[t]{0.4\linewidth}
        \centering
        \begin{tabular}{|l l|}
            \hline
                2 Amenity & 3 Barrier \\ 
                4 Boundary & 5 Building  \\ 
                6 Craft & 7 Emergency \\ 
                8 Geological & 9 Highway \\ 
                10 Historic & 11 Landuse \\ 
                12 Leisure & 13 Man made \\ 
                14 Military & 15 Natural \\ 
                16 Office & 17 Place \\ 
                18 Power & 19 Transport \\ 
                20 Railway & 21 Route \\ 
                22 Shop & 23 Sport \\ 
                24 Tourism & 25 Waterway \\ 
                26 Aerialway & 27 Aeroway \\ 
            \hline 
        \end{tabular}
    \end{minipage}\hfill
    \begin{minipage}[t]{0.4\linewidth}
        \centering
        \includegraphics[scale=0.35]{OSMTreeDendrogram.png}
    \end{minipage}
    \caption{Tree representation of localities and their location types given by the Open Street Map.}
    \label{OSM_tree}
\end{table}
    \end{document}

它有效,但它們不在同一高度。它們看起來像這樣:

在此輸入影像描述

如何使表格的起始高度與圖形相同?

答案1

試試以下 MWE:

\documentclass{article}
\usepackage[demo]{graphicx}
\usepackage{capt-of}% or \usepackage{caption}
\usepackage{booktabs}
\usepackage{varwidth}

\usepackage[export]{adjustbox}

\begin{document}
\begin{table}[ht]
\begin{tabular}{cc}
    \begin{tabular}{|l l|}
        \hline
    2 Amenity       & 3 Barrier \\
    4 Boundary      & 5 Building  \\
    6 Craft         & 7 Emergency \\
    8 Geological    & 9 Highway \\
    10 Historic     & 11 Landuse \\
    12 Leisure      & 13 Man made \\
    14 Military     & 15 Natural \\
    16 Office       & 17 Place \\
    18 Power        & 19 Transport \\
    20 Railway      & 21 Route \\
    22 Shop         & 23 Sport \\
    24 Tourism      & 25 Waterway \\
    26 Aerialway    & 27 Aeroway \\
        \hline
    \end{tabular}
&
\includegraphics[scale=0.35,valign=m]{OSMTreeDendrogram.png}
\end{tabular}
\caption{Tree representation of localities and their location types given by the Open Street Map.}
\label{OSM_tree}
\end{table}
\end{document}

我使用環境和包中的tabular垂直對齊選項來代替迷你頁。這是您要找的嗎?valign=madjustbox

在此輸入影像描述

附錄: 如果您喜歡垂直對齊表格和圖像的頂部(抱歉,我在您的問題中忽略了這一點),那麼您需要更改:

  • \begin{tabular}{|l l|}\begin{tabular}[t]{|l l|}
  • \includegraphics[scale=0.35,valign=m]{OSMTreeDendrogram.png}\includegraphics[scale=0.35,valign=T]{OSMTreeDendrogram.png}

那麼結果如下:

在此輸入影像描述

我更喜歡第一個解決方案:)

答案2

另一個範例程式碼(帶有調整表格大小的選項),它將表格和圖形並排放置。

  \begin{table}[h]
    \centering
    \begin{tabular}{ccccccc}
      \resizebox{0.5\textwidth}{!}{
        \begin{tabular}{|l|l|l|l|l|l|l|}
          \hline
          Test 1 & 0 & 1 & 2 & 3 & 4 & Test 2 \\
          \hline
          0 & 224 &  &  &  &  & 224\\
          \hline
          1 & 58 & 57 &  &  &  & 115\\
          \hline
          2 & 26 & 27 & 27  &  &  & 80\\
          \hline
          3 & 16 & 16 & 15 & 15 &  & 62\\
          \hline
          4 & 10 & 10 & 11 & 10 & 10 & 51\\
          \hline
        \end{tabular}
      }
      \includegraphics[scale=0.35, valign=m]{fig.png}
    \end{tabular}
    \caption*{Table with data and its graph shown side by side}
    \label{tab:data_and_fig}
  \end{table}

現在應該是這樣的。

相關內容