特定表的问题

特定表的问题

我是初学者LaTeX,我尝试使用下面的代码将表格放在合适的位置。但是当我编译代码时,表格会向下移动到页面末尾。

\documentclass[12pt,a4paper]{article}
\usepackage[hmargin=2cm,vmargin=2cm]{geometry}
\title{Tutorial on Tables and Figures}
\date{\today}
\begin{document}
\maketitle
\centering
\begin{table}
\caption{Cost of fruits in India}
\begin{tabular}{||l|c|c|c|c||}\hline
\multicolumn 2 {||c|}{Fruit details} & 
\multicolumn 3 {c|}{Cost calculations} \\ \hline
Fruit & Type & No. of units & cost/unit & cost (Rs.) \\ \hline
Mango & Malgoa & 18 & 50 & \\ \cline{2-4}
& Alfonso & 2 & 300 & 1,500 \\ \hline
Jackfruit & Kolli Hills & 10 & 50 & 500 \\ \hline
Banana & Green & 10 & 20 & 200 \\ \hline
\multicolumn 4{||r|}{Total cost (Rs.)} & 2,200 \\ \hline
\end{tabular}
\end{table}
\end{document}

在此处输入图片描述

答案1

这应该可以:

\documentclass[12pt,a4paper]{article}
\usepackage[hmargin=2cm,vmargin=2cm]{geometry}
\title{Tutorial on Tables and Figures}
\date{\today}

\begin{document}

\maketitle

\begin{table}[h] % see https://tex.stackexchange.com/questions/39017/how-to-influence-the-position-of-float-environments-like-figure-and-table-in-lat
    \centering % the \centering belongs inside the table environment
    \caption{Cost of fruits in India}\vspace{0.5em} % add some vertical space after the caption
    \begin{tabular}{||l|c|c|c|c||}\hline
        \multicolumn{2}{||c|}{Fruit details} & 
        \multicolumn{3}{c|}{Cost calculations} \\ \hline
        Fruit & Type & No. of units & cost/unit & cost (Rs.) \\ \hline
        Mango & Malgoa & 18 & 50 & \\ \cline{2-4}
        & Alfonso & 2 & 300 & 1,500 \\ \hline
        Jackfruit & Kolli Hills & 10 & 50 & 500 \\ \hline
        Banana & Green & 10 & 20 & 200 \\ \hline
        \multicolumn{4}{||r|}{Total cost (Rs.)} & 2,200 \\ \hline
    \end{tabular}
    %\caption{Cost of fruits in India} % you can also put the caption here
\end{table}

\end{document}

截屏

答案2

您需要添加浮动定位选项。好的选择是htbh此处、t顶部bp页面。

在下面的 MWE 中,我还对您的表格设计提出了一些小的改进。为此需要包captionmultirowsiunitx

编辑: 使用包列\tablenum 中的宏siunitx将数字固定在小数点分隔符处对齐。

\documentclass[12pt,a4paper]{article}
\usepackage[margin=2cm]{geometry}
\usepackage[skip=1ex]{caption}
\usepackage{multirow}
\usepackage{siunitx}
\newcommand{\mrte}[3][1]{%
    \multirow{#1}{*}{\tablenum[table-format=#2,
                               group-four-digits,
                               group-separator={,}]{#3}}} % <---

\title{Tutorial on Tables and Figures}
\author{Me}
\date{\today}

\begin{document}
\maketitle

\begin{table}[htb]
    \centering
    \renewcommand\arraystretch{1.1} % for more vertical space in cells
\caption{Cost of fruits in India}
\begin{tabular}{|l|c|S[table-format=2.0]|
                     S[table-format=3.0]|
                     c|}\hline
\multicolumn{2}{|c|}{Fruit details} &
\multicolumn{3}{c|}{Cost calculations} \\ \hline
Fruit       & Type          & {No. of units} & {cost/unit} & {cost (Rs.)} \\ \hline
Mango       & Malgoa        & 18    & 50    &                       \\ \cline{2-4}
            & Alfonso       & 2     & 300   & \mrte[-2]{4.0}{1500}  \\ \hline
Jackfruit   & Kolli Hills   & 10    & 50    & \mrte{4.0}{500}       \\ \hline
Banana      & Green         & 10    & 20    & \mrte{4.0}{200}       \\ \hline
\multicolumn{4}{|r|}{Total cost (Rs.)}      & \mrte{4.0}{2200}      \\ \hline
\end{tabular}
\end{table}
\end{document}

在此处输入图片描述

附录: 包中定义的没有垂直线但有水平线的表格booktabs具有更专业的外观:

\documentclass[12pt,a4paper]{article}
\usepackage[margin=2cm]{geometry}
\usepackage[skip=1ex]{caption}
\usepackage{booktabs,   % for the second example
            multirow}
\usepackage{siunitx}
\newcommand{\mrte}[3][1]{%
    \multirow{#1}{*}{\tablenum[table-format=#2,
                               group-four-digits,
                               group-separator={,}]{#3}}} % <---

\title{Tutorial on Tables and Figures}
\author{Me}
\date{\today}

\begin{document}
\maketitle

\begin{table}[htb]
    \centering
    \renewcommand\arraystretch{1.1} % for more vertical space in cells
\caption{Cost of fruits in India}
\begin{tabular}{ l l S[table-format=2.0]
                     S[table-format=3.0]
                     c}
    \toprule
\multicolumn{2}{c}{Fruit details} &
\multicolumn{3}{c}{Cost calculations} \\ 
    \cmidrule(r){1-2}
    \cmidrule(l){3-5}
Fruit       & Type          & {No. of units} & {cost/unit} & {cost (Rs.)} \\
    \midrule
\multirow{2}{*}{Mango}
            & Malgoa        & 18    & 50    &                       \\ 
            & Alfonso       & 2     & 300   & \mrte[-2]{4.0}{1500}  \\ 
    \addlinespace
Jackfruit   & Kolli Hills   & 10    & 50    & \mrte{4.0}{500}       \\ 
Banana      & Green         & 10    & 20    & \mrte{4.0}{200}       \\ 
    \midrule
\multicolumn{4}{r}{Total cost (Rs.)}        & \mrte{4.0}{2200}      \\
    \bottomrule
\end{tabular}
\end{table}
\end{document}

在此处输入图片描述

相关内容