表格和图形与小页面并排

表格和图形与小页面并排

如何使用 minipage 并排放置表格和图形?我已成功完成,但图形的标题是表格,因为表格是周围的标签。

\begin{table}[ht]
\begin{minipage}[b]{0.45\linewidth}
\centering
\begin{tabular}{ | l | r | r | r |}
    \hline
    Student & Hours/week & Grade \\ \hline \hline   
    Ada Lovelace & 2 & A \\ \hline
    Linus Thorvalds & 8 & A \\ \hline
    Bruce Willis & 12 & F \\ \hline
    Richard Stallman & 10 & B \\ \hline
    Grace Hopper & 12 & A \\ \hline
    Alan Turing & 8 & C \\ \hline
    Bill Gates & 6 & D \\ \hline
    Steve Jobs & 4 & E \\ \hline
   \end{tabular}
    \caption{Student Database}
    \label{table:student}
\end{minipage}
\end{table}

\begin{figure}[ht]
\begin{minipage}[b]{0.45\linewidth}
\centering
\includegraphics[width=40mm]{figures/studentdatabasegraph.png}
\caption{2-D scatterplot of the Student Database}
\label{ }
\end{minipage}
\end{figure}

答案1

表格和图形标题可以放在同一个浮动环境中,但有一个很大的问题:LaTeX 将图形和表格分开放置,并且不会将表格浮动中的图形标题的编号与其他图形同步。因此,图形的编号可能会乱序。

示例文件:

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

\begin{document}

\begin{table}[ht]
\begin{minipage}[b]{0.56\linewidth}
\centering
\begin{tabular}{ | l | r | r | r |}
    \hline
    Student & Hours/week & Grade \\ \hline \hline
    Ada Lovelace & 2 & A \\ \hline
    Linus Thorvalds & 8 & A \\ \hline
    Bruce Willis & 12 & F \\ \hline
    Richard Stallman & 10 & B \\ \hline
    Grace Hopper & 12 & A \\ \hline
    Alan Turing & 8 & C \\ \hline
    Bill Gates & 6 & D \\ \hline
    Steve Jobs & 4 & E \\ \hline
   \end{tabular}
    \caption{Student Database}
    \label{table:student}
\end{minipage}\hfill
\begin{minipage}[b]{0.4\linewidth}
\centering
\includegraphics[width=40mm]{example-image}
\captionof{figure}{2-D scatterplot of the Student Database}
\label{fig:image}
\end{minipage}
\end{table}
\end{document}

结果

更漂亮的桌子

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

\begin{document}
\begin{table}[ht]
  \begin{varwidth}[b]{0.6\linewidth}
    \centering
    \begin{tabular}{ l r r r }
      \toprule
      Student & Hours/week & Grade \\
      \midrule
      Ada Lovelace & 2 & A \\
      Linus Thorvalds & 8 & A \\
      Bruce Willis & 12 & F \\
      Richard Stallman & 10 & B \\
      Grace Hopper & 12 & A \\
      Alan Turing & 8 & C \\
      Bill Gates & 6 & D \\
      Steve Jobs & 4 & E \\
      \bottomrule
    \end{tabular}
    \caption{Student Database}
    \label{table:student}
  \end{varwidth}%
  \hfill
  \begin{minipage}[b]{0.4\linewidth}
    \centering
    \includegraphics[width=40mm]{example-image}
    \captionof{figure}{2-D scatterplot of the Student Database}
    \label{fig:image}
  \end{minipage}
\end{table}
\end{document}

结果

不同的对齐方式

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

\begin{document}
\begin{table}[ht]
  \sbox\tmpbox{%
    \begin{tabular}[b]{ l r r r }
      \toprule
      Student & Hours/week & Grade \\
      \midrule
      Ada Lovelace & 2 & A \\
      Linus Thorvalds & 8 & A \\
      Bruce Willis & 12 & F \\
      Richard Stallman & 10 & B \\
      Grace Hopper & 12 & A \\
      Alan Turing & 8 & C \\
      Bill Gates & 6 & D \\
      Steve Jobs & 4 & E \\
      \bottomrule
    \end{tabular}%
  }%
  \renewcommand*{\arraystretch}{0}
  \begin{tabular*}{\linewidth}{@{\extracolsep\fill}p{\wd\tmpbox}p{40mm}@{}}
    \usebox\tmpbox &
    \includegraphics[width=\linewidth]{example-image} \\
    \caption{Student Database}
    \label{table:student}
    &
    \captionof{figure}{2-D scatterplot of the Student Database}
    \label{fig:image}
  \end{tabular*}
\end{table}
\end{document}

结果

相关内容