1

I'm totally new with Latex and I'm facing this problem using tables:

I have a certain number of tables with the exact same structure, when I define subsections between them the subsections do not appear on the right position. In particular in this case what I get is a page with on top the SUBSECTION A then a list of tables, but at the end of the first page of I see written SUBSECTION B... but that was not the right position of the table.

Why does this happen according to you? Thank you.

\newcolumntype{R}[1]{>{\raggedleft}p{#1}}

\subsection{A}
\begin{table}[ht!]
  \caption{Movie 01.01.F}
  \begin{tabular}{R{3cm} p{9cm}} 
    \toprule
    \textbf{ID} & 01.01.F \\
    \midrule
    \textbf{Title} & Movie2\\
    \textbf{Description} & Movie1\\
    \bottomrule
  \end{tabular}
\end{table}

.
.
.

\subsection{B}
\begin{table}[ht!]
  \caption{Movie 01.02.F}
  \begin{tabular}{R{3cm} p{9cm}} 
    \toprule
    \textbf{ID} & 01.02.F \\
    \midrule
    \textbf{Title} & Movie2\\
    \textbf{Description} & Movie2\\
    \bottomrule
  \end{tabular}
\end{table}
M P
  • 13
  • 3
    The table environment defines a float. Floats float, which is to say that they appear in the place which is most aesthetically suitable. You can imagine a big thing like a table or a picture can take up a huge chunk of space, and you'll probably be familiar with tables and figures appearing, sometimes, on their own dedicated pages at the back, or only at the top and/or bottom of a page, and not necessarily where they would logically go in a document, right after the first mention of them, because they often are referred to separately anyway. Word processors of course don't do this – Au101 May 30 '16 at 21:35
  • Welcome to TeX.SX (lot of new people today). Can you add a graphic example what you mean. With some red lines where (you think) the fault is. Paint does the job good enough here. – MaestroGlanz May 30 '16 at 21:36
  • Now, the [ht!] option does the most it can to put it in the output exactly where it is in the input, but for typographical reasons, subsection headers may well appear after tables when they are floated using the table environment, either don't use floats, or use a package such as placeins to help you – Au101 May 30 '16 at 21:37
  • But [ht!] can cause misplacement of the floats. One time my floats floated all to the end of the document. And with one time I mean several times. Better you \label the tables and refer to them by \vref (varioref package - highly recomended; and of course the hyperref package). – MaestroGlanz May 30 '16 at 21:40
  • @Au101 I got it, removing [ht!] did't help anyway, the subsection now went down right in the middle of the page, in a strange position. Would you suggest me how to fix it, or something to read about? Thanks a lot – M P May 30 '16 at 21:56
  • @MP Well no, removing [ht!] won't help, [ht!] is already doing the most you can do with the native table environment. You can just not enclose your tabular within a table environment at all. One of the purposes of the table environment is to give this floating behaviour. However, the environment also does things like captions and the list of tables, which your probably want. Another thing you can do is live with the floating behaviour, which is often desirable. – Au101 May 30 '16 at 21:58
  • However, you might like to try the placeins package. This defines the \FloatBarrier command, which you can issue before the table environment and it will make sure that nothing which appears before the table environment in the code will appear before the table in the output. Or, in other words, the table will be wherever you want it to be. This may produce ugly results, however, use with caution – Au101 May 30 '16 at 22:00
  • @Au101 placeins completely solved the problem. Thank you so much, please answer to this question so that I can solve it giving you points! – M P May 30 '16 at 22:12

1 Answers1

1

The table environment defines a float. Floats float, which is to say that they appear in the place which is most aesthetically suitable. You can imagine a big thing like a table or a picture can take up a huge chunk of space. Often, tables are not to be broken across pages and figures almost never can be. As a result, you may find that figures or tables have to be moved onto a subsequent page. In word processors, it is quite common for an enormous gap on the previous page to be left, with the table starting the next page and then more text continuing afterwards. This is not very nice.

To avoid this, you'll probably be familiar with tables and figures sometimes appearing on their own dedicated pages at the end of a chapter. Alternatively, you may find them only appearing at the top and/or bottom of a page, and not necessarily where they would logically go in a document. So, instead of leaving that enormous gap on the previous page, with the table on the next page and then the rest of the text, the first page is filled up with the text that would have followed the table and the table simply gets placed at the top of the next page. Much prettier, much more efficient, but it doesn't work very well if you write something like as can be seen in the table below:

However, because tables often are referred to separately anyway, this floating behaviour is often preferred.

Let me show you what I'm on about.

Instead of this:

enter image description here

Where we have the red text, a huge gap, the table, and then the blue text following on.

We get this:

enter image description here

Where the blue text carries straight on after the red text, the first page is filled up, and then the table appears at the top of the next page, with any further blue text carrying on after it. But all of the first page is filled up nicely.

The table appears in the code between the red paragraphs and the blue paragraphs.

If you would rather avoid this floating behaviour, the first port of call is using the [ht!] option with the table environment. The optional argument is used to tell LaTeX where to place the float. [ht!] will get LaTeX to do the best it can to put the table in the output exactly where it is in the input. However, things like subsection headers can cause problems due to typographical considerations, which I gather is the difficulty you are experiencing.

So the next thing you can do is not float your tables if you don't want them to float! :P One of the purposes of the table environment is to give this floating behaviour. You can just not enclose your tabular within a table environment at all.

However, the table environment is very important and very useful and also does things like captions and the list of tables, which you probably want.

Another thing you can do is simply live with the floating behaviour, which is often aesthetically desirable. Those weaned on Word processors like me are not used to floats and tend to want to avoid them, but they are quite good practice and their benefits are worth bearing in mind.

If, however, you want your document to retain a perfectly logical flow, with each table appearing right where you want to talk about it, you might like to try the placeins package. This defines the \FloatBarrier command, which you can issue after the table environment and it will make sure that nothing which appears after the table environment in the code will appear before the table in the output. Or, in other words, the table will be wherever you want it to be. This may produce ugly results, however, use with caution.

MWE:

\documentclass{article}
\usepackage{placeins}
\usepackage{lipsum}

\begin{document}

\lipsum[1-4]

\begin{table}[!ht]
  \begin{tabular}{ll} 
    Column A & Column B \\
    1 & 2 \\
    3 & 4 \\
    5 & 6 \\
    7 & 8 \\
    9 & 10 \\
    11 & 12 \\
    13 & 14 \\
    15 & 16 \\
    17 & 18 \\
    19 & 20 \\
    21 & 22 \\
    23 & 24 \\
    25 & 26 \\
    27 & 28 \\
    29 & 30 \\
    31 & 32 \\
    33 & 34 \\
    35 & 36 \\
  \end{tabular}
\end{table}

\FloatBarrier

\lipsum[5-7]

\end{document}

Code for example above:

\documentclass[12pt]{article}
\pagestyle{plain}
\usepackage[margin=1.8cm]{geometry}
\geometry{a4paper}
\usepackage[parfill]{parskip}
\usepackage{amsmath}

\usepackage{lipsum}
\usepackage{booktabs}

\usepackage{placeins}

\usepackage[usenames]{color}
\definecolor{red}{rgb}{1,0,0}
\definecolor{blue}{rgb}{0,0,1}

\newcommand{\red}[1]{\textcolor{red}{#1}}
\newcommand{\blue}[1]{\textcolor{blue}{#1}}

\begin{document}

\red{\lipsum[1-4]}

\begin{table}[!ht]
  \begin{tabular}{ll} 
    \toprule
    Column A & Column B \\
    \midrule
    1 & 2 \\
    3 & 4 \\
    5 & 6 \\
    7 & 8 \\
    9 & 10 \\
    11 & 12 \\
    13 & 14 \\
    15 & 16 \\
    17 & 18 \\
    19 & 20 \\
    21 & 22 \\
    23 & 24 \\
    25 & 26 \\
    27 & 28 \\
    29 & 30 \\
    31 & 32 \\
    33 & 34 \\
    35 & 36 \\
    \bottomrule
  \end{tabular}
\end{table}

\FloatBarrier

\blue{\lipsum[5-7]}

\end{document}
Au101
  • 10,278