1

I am the beginner of LaTeX and I have tried the below code to make table in appropriate place. But when I compile the code table moves down to end of the page.

\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}

enter image description here

Bernard
  • 271,350

2 Answers2

0

This should do:

\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}

screenshot

steve
  • 2,154
0

You need to ad float posioning options. The good choise is htb which means h for here, t at top nad b and p for page.

In MWE belwo I also sugest some small improvements for your table design. Fro this are needed packages caption, multirow and siunitx.

Edit: with use of the \tablenum macro from siunitx package column is fixed numbers aligning at decimal separator.

\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}

enter image description here

Addendum: a table without vertical lines and with horizontal rules defined in the booktabs package has a more professional appearance:

\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}

enter image description here

Zarko
  • 296,517