1
\documentclass{book}

\usepackage{array}
\usepackage{mdframed}
\usepackage{multirow}
\usepackage{xcolor} % Required for specifying colors by name
\definecolor{ocre}{RGB}{243,102,25}
\usepackage{float}
\usepackage{caption}
\usepackage{booktabs}
\usepackage{longtable}

\newmdenv[%
linecolor=ocre,
backgroundcolor=ocre!10,
linewidth=1pt]
{Tablebox}   


\newenvironment{mytablebox}
{
    \begin{Tablebox}
        
    }
    {
    \end{Tablebox}
}



\begin{document}
    The various activities which are performed in the Crane shop are given in table \ref{table:crane_shop_activities}.
    \begin{mytablebox}
        %\begin{table}[H]
        \centering
        \captionof{table}{Crane shop activities.}
            \label{table:crane_shop_activities}
        \begin{longtable}{|m{2cm}| m{4cm}| m{4cm}|}
            \hline
            \textbf{Category} & \textbf{Types} & \textbf{Activities}\\
            \hline
            \multirow{3}{*}{Tower Car} & Mark II, III, IV & \multirow{3}{4cm}{Earlier Manufacturing, currently only POH}\\
            \cline{2-2}
            & DHTC (Diesel Hydraulic Tower Car) & \\
            \cline{2-2}
            & 8 Wheeler &\\
            \hline
            \multirow{2}{*}{20T Crane} & Mechanical & \multirow{2}{4cm}{Both Manufacturing and POH}\\
            \cline{2-2}
            & Hydraulic (retrofitting of mechanical superstructure with hydraulic one) & \\
            \hline
            \multirow{2}{*}{140T Crane} & Old Design Crane & POH, MLR, SP MLR\\
            \cline{2-3}
            & New Design Crane & Manufacturing, POH, MLR, SP MLR\\
            \hline
        
        \end{longtable}
        %\end{table}
    \end{mytablebox}
    
    Activities related to 140T crane are given in detail in table \ref{table:140T_activities}.
    \begin{mytablebox}
        \begin{table}[H]
            \caption{140T Crane activities.}
            \begin{tabular}{|p{0.45\textwidth}|p{0.45\textwidth}|}
                \hline
                1$^{st}$ POH & After 8 yrs\\
                \hline
                2$^{nd}$ POH & After 16 yrs\\
                \hline
                MLR & After 22 yrs\\
                \hline
                SP MLR & After 30 yrs\\
                \hline
                SR (Special Repair) & Accidental and unpredictable\\
                \hline
            \end{tabular}
            \label{table:140T_activities}
        \end{table}
    \end{mytablebox}

    
    
\end{document}

Table number wrong in second table

Table number 2 is coming as 3rd table. How to correct this?

1
  • 1
    Off-topic: Please don't write 1$^{st}$ and 2$^{nd}$. Instead, please write 1\textsuperscript{st} and 2\textsuperscript{nd}.
    – Mico
    Jun 29, 2017 at 16:59

2 Answers 2

5

I'm afraid there's no gentle way to state this: You're misusing the longtable and table environments and the \captionof macro.

  • If you use a longtable environment, do not use a \captionof{table}{...} statement. Instead, use a standard \caption statement.

  • If you're going to encase some tabular material inside a mytablebox environment, do not use a table environment along with a \caption statement. Instead, get rid of the table wrapper, apply a \centering directive, and use a \captionof statement.


enter image description here

\documentclass{book}
\usepackage{array}
\setlength\extrarowheight{2pt} % for a more "open" look
\usepackage{mdframed}
\usepackage{multirow}
\usepackage{xcolor} % Required for specifying colors by name
\definecolor{ocre}{RGB}{243,102,25}
\usepackage{float}
\usepackage[skip=0.333\baselineskip]{caption}
\usepackage{booktabs}
\usepackage{longtable}

\newmdenv[%
    linecolor=ocre,
    backgroundcolor=ocre!10,
    linewidth=1pt]
  {Tablebox}   
\newenvironment{mytablebox}
   {\begin{Tablebox}}{\end{Tablebox}}

\begin{document}
Various activities performed in the Crane shop are given in table \ref{table:crane_shop_activities}.
    \begin{mytablebox}
    \begin{longtable}{|m{2cm}| m{4cm}| m{4cm}|}
        \caption{Crane shop activities.}
        \label{table:crane_shop_activities}\\
        \hline
        \textbf{Category} & \textbf{Types} & \textbf{Activities}\\
        \hline
        \multirow{3}{*}{Tower Car} & Mark II, III, IV & 
        \multirow{3}{4cm}{Earlier Manufacturing, currently only POH}\\
        \cline{2-2}
        & DHTC (Diesel Hydraulic Tower Car) & \\
        \cline{2-2}
        & 8 Wheeler &\\
        \hline
        \multirow{2}{*}{20T Crane} & Mechanical & 
        \multirow{2}{4cm}{Both Manufacturing and POH}\\
        \cline{2-2}
        & Hydraulic (retrofitting of mechanical superstructure with hydraulic one) & \\
        \hline
        \multirow{2}{*}{140T Crane} & Old Design Crane & POH, MLR, SP MLR\\
        \cline{2-3}
        & New Design Crane & Manufacturing, POH, MLR, SP MLR\\
        \hline
    \end{longtable}
    \end{mytablebox}

Activities related to 140T crane are given in detail in table \ref{table:140T_activities}.
    \begin{mytablebox}
    \centering % <-- new!
    %\begin{table}[H] %% do NOT use a 'table' env.
        \captionof{table}{140T Crane activities.} %% use '\captionof', not '\caption'
        \label{table:140T_activities}
        \begin{tabular}{|p{0.45\textwidth}|p{0.45\textwidth}|}
        \hline
        1$^{st}$ POH & After 8 yrs\\
        \hline
        2$^{nd}$ POH & After 16 yrs\\
        \hline
        MLR & After 22 yrs\\
        \hline
        SP MLR & After 30 yrs\\
        \hline
        SR (Special Repair) & Accidental and unpredictable\\
        \hline
        \end{tabular}
    %\end{table}
    \end{mytablebox}

\end{document}
0
0

solution with longtabular

You have to write the \caption and \label inside the longtabular.

\documentclass{book}

\usepackage{array}
\usepackage{mdframed}
\usepackage{multirow}
\usepackage{xcolor} % Required for specifying colors by name
\definecolor{ocre}{RGB}{243,102,25}
\usepackage{float}
\usepackage{caption}
\usepackage{booktabs}
\usepackage{longtable}

\newmdenv[%
linecolor=ocre,
backgroundcolor=ocre!10,
linewidth=1pt]
{Tablebox}   

\newenvironment{mytablebox}
{
    \begin{Tablebox}
}
{
    \end{Tablebox}
}

\begin{document}
    The various activities which are performed in the Crane shop are given in table \ref{table:crane_shop_activities}.
    \begin{mytablebox}
        \centering
        \begin{longtable}{|m{2cm}| m{4cm}| m{4cm}|}
            \caption{Crane shop activities.}
            \label{table:crane_shop_activities}\\
            \hline
            \textbf{Category} & \textbf{Types} & \textbf{Activities}\\
            \hline
            \multirow{3}{*}{Tower Car} & Mark II, III, IV & \multirow{3}{4cm}{Earlier Manufacturing, currently only POH}\\
            \cline{2-2}
            & DHTC (Diesel Hydraulic Tower Car) & \\
            \cline{2-2}
            & 8 Wheeler &\\
            \hline
            \multirow{2}{*}{20T Crane} & Mechanical & \multirow{2}{4cm}{Both Manufacturing and POH}\\
            \cline{2-2}
            & Hydraulic (retrofitting of mechanical superstructure with hydraulic one) & \\
            \hline
            \multirow{2}{*}{140T Crane} & Old Design Crane & POH, MLR, SP MLR\\
            \cline{2-3}
            & New Design Crane & Manufacturing, POH, MLR, SP MLR\\
            \hline
        \end{longtable}
    \end{mytablebox}

    Activities related to 140T crane are given in detail in table \ref{table:140T_activities}.
    \begin{mytablebox}
        \begin{table}[H]
            \caption{140T Crane activities.}
            \begin{tabular}{|p{0.45\textwidth}|p{0.45\textwidth}|}
                \hline
                1$^{st}$ POH & After 8 yrs\\
                \hline
                2$^{nd}$ POH & After 16 yrs\\
                \hline
                MLR & After 22 yrs\\
                \hline
                SP MLR & After 30 yrs\\
                \hline
                SR (Special Repair) & Accidental and unpredictable\\
                \hline
            \end{tabular}
            \label{table:140T_activities}
        \end{table}
    \end{mytablebox}
\end{document}

enter image description here

solution without longtabular

If you use tabular instead of longtable it works. To avoid the different spacing you should use \captionof for the second table also.

\documentclass{book}

\usepackage{array}
\usepackage{mdframed}
\usepackage{multirow}
\usepackage{xcolor} % Required for specifying colors by name
\definecolor{ocre}{RGB}{243,102,25}
\usepackage{float}
\usepackage{caption}
\usepackage{booktabs}
\usepackage{longtable}

\newmdenv[%
linecolor=ocre,
backgroundcolor=ocre!10,
linewidth=1pt]
{Tablebox}   

\newenvironment{mytablebox}
{
    \begin{Tablebox}
}
{
    \end{Tablebox}
}

\begin{document}
    The various activities which are performed in the Crane shop are given in table \ref{table:crane_shop_activities}.
    \begin{mytablebox}
        \centering
        \captionof{table}{Crane shop activities.}
        \label{table:crane_shop_activities}
        \begin{tabular}{|m{2cm}| m{4cm}| m{4cm}|}
            \hline
            \textbf{Category} & \textbf{Types} & \textbf{Activities}\\
            \hline
            \multirow{3}{*}{Tower Car} & Mark II, III, IV & \multirow{3}{4cm}{Earlier Manufacturing, currently only POH}\\
            \cline{2-2}
            & DHTC (Diesel Hydraulic Tower Car) & \\
            \cline{2-2}
            & 8 Wheeler &\\
            \hline
            \multirow{2}{*}{20T Crane} & Mechanical & \multirow{2}{4cm}{Both Manufacturing and POH}\\
            \cline{2-2}
            & Hydraulic (retrofitting of mechanical superstructure with hydraulic one) & \\
            \hline
            \multirow{2}{*}{140T Crane} & Old Design Crane & POH, MLR, SP MLR\\
            \cline{2-3}
            & New Design Crane & Manufacturing, POH, MLR, SP MLR\\
            \hline
        \end{tabular}
    \end{mytablebox}

    Activities related to 140T crane are given in detail in table \ref{table:140T_activities}.
    \begin{mytablebox}
        \captionof{table}{140T Crane activities.}
        \label{table:140T_activities}
        \begin{tabular}{|p{0.45\textwidth}|p{0.45\textwidth}|}
            \hline
            1$^{st}$ POH & After 8 yrs\\
            \hline
            2$^{nd}$ POH & After 16 yrs\\
            \hline
            MLR & After 22 yrs\\
            \hline
            SP MLR & After 30 yrs\\
            \hline
            SR (Special Repair) & Accidental and unpredictable\\
            \hline
        \end{tabular}
    \end{mytablebox}
\end{document}

enter image description here

1
  • Thanks for the suggestion. I know it works with tabular but I need longtable. Jun 29, 2017 at 16:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .