2

We generate a multicol document using the scrartcl document class.

Taking the sample below data, we see when we use the \begin{table}[h] command no table is generated in the multicols section. If we use the \begin{centering} command a table is generated however he have no \caption{} etc.

Yet when we are in the single column feature (at the end of the data) we see the \begin{table}[h] behaves as expected. Does anyone know how to integrate this into the multicol environment?

\documentclass[10pt,a4paper]{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage[margin=2.5cm]{geometry}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{graphicx}
\usepackage{booktabs}
\usepackage[colaction]{multicol}
\usepackage{hyperref}
\usepackage{lineno} 
\usepackage{setspace}
\usepackage{lipsum}
\title{Table formatting}
\author{The geniuses at SE}

\begin{document}
\maketitle
\doublespacing

\begin{abstract}

    \lipsum[1-1]


\end{abstract}

\begin{multicols}{2}

\section{Introduction}      
\lipsum[1-2]

\begin{table}[h]
    \centering
    \caption{My caption}
    \label{my-label}
    \begin{tabular}{lllll}
        & \multicolumn{2}{l}{XYZ} & \multicolumn{2}{l}{ABC} \\
        & Sex         & P         & Sex         & P         \\
        AGE1 & 0.5         & 1         & 0.6         & 1         \\
        &             &           &             &          
    \end{tabular}
\end{table}

\lipsum[1-1]

\begin{centering}
    \begin{tabular}{lllll}
        & \multicolumn{2}{l}{XYZ} & \multicolumn{2}{l}{ABC} \\
        & Sex         & P         & Sex         & P         \\
        AGE1 & 0.5         & 1         & 0.6         & 1         \\
        &             &           &             &          
    \end{tabular}
\end{centering}

\lipsum[1-1]

\end{multicols}
\bigskip
\lipsum[1-1]

\begin{table}[h]
    \centering
    \caption{My caption}
    \label{my-label}
    \begin{tabular}{lllll}
        & \multicolumn{2}{l}{XYZ} & \multicolumn{2}{l}{ABC} \\
        & Sex         & P         & Sex         & P         \\
        AGE1 & 0.5         & 1         & 0.6         & 1         \\
        &             &           &             &          
    \end{tabular}
\end{table}

\lipsum[1-1]


\end{document}
lukeg
  • 211

2 Answers2

5

Note that \centering is a command and not an environment. Use the center environment instead.

KOMA-Script defines the commands \captionof, \captionaboveof and \captionbelowof for figures or tables etc. outside floats.

I would suggest to use a minipage to ensure that there is no column break or page break between the caption and the table.

 \begin{center}% insert some space before and after the table
    \begin{minipage}{\columnwidth}% ensure that there is no page or column break
      \centering% center the table
      \captionaboveof{table}{My caption}
      \label{my-label}
      \begin{tabular}{lllll}
        & \multicolumn{2}{l}{XYZ} & \multicolumn{2}{l}{ABC} \\
        & Sex         & P         & Sex         & P         \\
        AGE1 & 0.5         & 1         & 0.6         & 1         \\
        &             &           &             &          
      \end{tabular}
    \end{minipage}
  \end{center}

enter image description here

Code:

  \documentclass[10pt,a4paper]{scrartcl}
  \usepackage[utf8]{inputenc}
  \usepackage[margin=2.5cm]{geometry}
  \usepackage{booktabs}
  \usepackage[colaction]{multicol}
  \usepackage{lineno} 
  \usepackage{setspace}
  \usepackage{lipsum}
  \title{Table formatting}
  \author{The geniuses at SE}

  \begin{document}
    \maketitle
    \doublespacing
    \begin{abstract}
      \lipsum[1-2]
    \end{abstract}
    \begin{multicols}{2}
      \section{Introduction}
      \lipsum[1]
     \begin{center}% insert some space before and after the table
        \begin{minipage}{\columnwidth}% ensure that there is no page or column break
          \centering% center the table
          \captionaboveof{table}{My caption}
          \label{my-label}
          \begin{tabular}{lllll}
            & \multicolumn{2}{l}{XYZ} & \multicolumn{2}{l}{ABC} \\
            & Sex         & P         & Sex         & P         \\
            AGE1 & 0.5         & 1         & 0.6         & 1         \\
            &             &           &             &          
          \end{tabular}
        \end{minipage}
      \end{center}
      \lipsum[1-2]
      \begin{center}
        \begin{minipage}{\columnwidth}
          \centering
          \captionaboveof{table}{My caption}
          \label{my-label}
          \begin{tabular}{lllll}
            & \multicolumn{2}{l}{XYZ} & \multicolumn{2}{l}{ABC} \\
            & Sex         & P         & Sex         & P         \\
            AGE1 & 0.5         & 1         & 0.6         & 1         \\
            &             &           &             &          
          \end{tabular}
        \end{minipage}
      \end{center}
      \lipsum[1]
    \end{multicols}
    \lipsum[1]
  \end{document}

If you remove the first minipage in the example above you will see that then there could be a break between caption and table.

enter image description here

esdd
  • 85,675
2

In have in remembrance, that multicol doesn't support floats. A solution seems to be something like this:

\begin{centering}
    \begin{tabular}{lllll}
        & \multicolumn{2}{l}{XYZ} & \multicolumn{2}{l}{ABC} \\
        & Sex         & P         & Sex         & P         \\
        AGE1 & 0.5         & 1         & 0.6         & 1         \\
        &             &           &             &
    \end{tabular}
\captionof{table}{My important table}% <--- for table caption
\end{centering}

enter image description here

Edit: To have caption above tableenter code here, you need to move caption before it:

\begin{center}% not centering as you use in your example and I overtake from your MWE
\captionof{table}{My important table}% <--- for table caption
    \begin{tabular}{lllll}
        & \multicolumn{2}{l}{XYZ} & \multicolumn{2}{l}{ABC} \\
        & Sex         & P         & Sex         & P         \\
        AGE1 & 0.5         & 1         & 0.6         & 1         \\
        &             &           &             &
    \end{tabular}
\end{center}

enter image description here

Zarko
  • 296,517
  • Yep. all look good, thanks for the suggestion. Only one small thing. Captions for tables should appear above the table, how do you format that? – lukeg Jan 13 '16 at 21:25
  • 3
    @lukeg the caption and table do not move they just appear where you put them so just use \captionof before the tabular in that case. – David Carlisle Jan 13 '16 at 21:39