LaTeX table positioning - latex

I have a LaTeX document that contains a paragraph followed by 4 tables followed by a second paragraph. I want the 4 tables to appear between the two paragraphs which from what I've read means I should use the [h] option after beginning the table environment (e.g. \begin{table}[h]).
Using this the first two tables appear after paragraph 1 as expected, however paragraph 2 is then displayed with the last two tables appearing on the following page. How can I get the tables to appear in the correct location?
I've tried various things to correct the positioning such as using [h!] however this doesn't seem to have any effect. Using \clearpage after the tables does have the desired effect of making the tables appear before the second paragraph but it then leaves the last two tables on their own page with loads of white-space, when I would prefer to have the second paragraph begin immediately after the tables.
Paragraph 1...
\begin{table}[h]
table1...
\end{table}
\begin{table}[h]
table2...
\end{table}[h]
...
Paragraph 2...

After doing some more googling I came across the float package which lets you prevent LaTeX from repositioning the tables.
In the preamble:
\usepackage{float}
Then for each table you can use the H placement option (e.g. \begin{table}[H]) to make sure it doesn't get repositioned.

Table Positioning
Available Parameters
A table can easily be placed with the following parameters:
h Place the float here, i.e., approximately at the same point it occurs in the source text (however, not exactly at the spot)
t Position at the top of the page.
b Position at the bottom of the page.
p Put on a special page for floats only.
! Override internal parameters LaTeX uses for determining "good" float positions.
H Places the float at precisely the location in the LATEX code. Requires the float package. This is somewhat equivalent to h!.
If you want to make use of H (or h!) for an exact positioning, make sure you got the float package correctly set up in the preamble:
\usepackage{float}
\restylefloat{table}
Example
If you want to place the table at the same page, either at the exact place or at least at the top of the page (what fits best for the latex engine), use the parameters h and t like this:
\begin{table}[ht]
table content ...
\end{table}
Sources: Overleaf.com

At the beginning with the usepackage definitions include:
\usepackage{placeins}
And before and after add:
\FloatBarrier
\begin{table}[h]
\begin{tabular}{llll}
....
\end{tabular}
\end{table}
\FloatBarrier
This places the table exactly where you want in the text.

Here's an easy solution, from Wikibooks:
The placeins package provides the command \FloatBarrier, which can be used to prevent floats from being moved over it.
I just put \FloatBarrier before and after every table.

What happens if the text plus tables plus text doesn't fit onto a single page? By trying to force the typesetting in this way, you are very likely to end up with pages that run too short; i.e., because a table cannot by default break over a page it will be pushed to the next, and leave a gap on the page before. You'll notice that you never see this in a published book.
The floating behaviour is a Good Thing! I recommend using [htbp] as the default setting for all tables and figures until your document is complete; only then should think about fine-tuning their precise placement.
P.S. Read the FAQ; most other answers here are partial combinations of advice given there.

If you want to have two tables next to each other you can use: (with float package loaded)
\begin{table}[H]
\begin{minipage}{.5\textwidth}
%first table
\end{minipage}
\begin{minipage}{.5\textwidth}
%second table
\end{minipage}
\end{table}
Each one will have own caption and number.
Another option is subfigure package.

In my case I was having an issue where the table was not being displayed right after the paragraph I inserted it, so I simply changed
\begin{table}[]
to
\begin{table}[ht]

You may want to add this to your preamble, and adjust the values as necessary:
%------------begin Float Adjustment
%two column float page must be 90% full
\renewcommand\dblfloatpagefraction{.90}
%two column top float can cover up to 80% of page
\renewcommand\dbltopfraction{.80}
%float page must be 90% full
\renewcommand\floatpagefraction{.90}
%top float can cover up to 80% of page
\renewcommand\topfraction{.80}
%bottom float can cover up to 80% of page
\renewcommand\bottomfraction{.80}
%at least 10% of a normal page must contain text
\renewcommand\textfraction{.1}
%separation between floats and text
\setlength\dbltextfloatsep{9pt plus 5pt minus 3pt }
%separation between two column floats and text
\setlength\textfloatsep{4pt plus 2pt minus 1.5pt}
Particularly, the \floatpagefraction may be of interest.

Not necessary to use \restylefloat and destroys other options, like caption placement. just use [H] or [!h] after \begin{table}.

Related

Figures all over the show in latex in overleaf

I am having issues with a report I am writing. First time using latex and after getting some help on here regarding some tables weirdness I again have a silly frustrating issue. Figures don't stay where I put them. I made a MWE Posted Below
\documentclass{article}
\usepackage{graphicx}
\graphicspath{ {./Figures/} }
%%%%%% Referencing %%%%%%
\usepackage{natbib}
\bibliographystyle{abbrvnat}
\setcitestyle{authoryear,open={(},close={)}}
\title{MWE}
\begin{document}
\maketitle
\section{Introduction}
The average sea surface temperature (SST) trend is shown in Figure \ref{fig:SST}.
\section{Figures}
\begin{figure}
\includegraphics[width = \linewidth]{Figures/SST.png}
\label{fig:SST}
\end{figure}
\section{References}
\bibliography{References.bib}
\bibliographystyle{agsm}
\end{document}
I expect, probably an error, for the figure to be inserted under the Figures subheading however it appears on the next page after references. I have to refer to my figure in the text, hence the \ref{fig:SST} so it is clickable. It's not a size issue as there is more than enough space on the page to accommodate the figure. Even if that was the issue I would expect the references subheading to be after it.
Most latex classes use so called floats for figures, tables etc. The idea is that latex will automatically find a good place for your images and avoid ugly white space.
To make use of the abilities of latex to produce a good looking output, you must specify possible placements with floating specifier such as [htbp], which allows latex to place the image here, at the top, at the bottom or an a separate page.
Also if you want to use the \label-\ref mechanism, your figure must have a caption (and the label inside or after the caption).
\documentclass{article}
\usepackage{graphicx}
\graphicspath{ {./Figures/} }
%%%%%% Referencing %%%%%%
\usepackage{natbib}
\bibliographystyle{abbrvnat}
\setcitestyle{authoryear,open={(},close={)}}
\title{MWE}
\begin{document}
\maketitle
\section{Introduction}
The average sea surface temperature (SST) trend is shown in Figure \ref{fig:SST}.
\section{Figures}
\begin{figure}[htbp]
\includegraphics[width = \linewidth]{example-image-duck}
\caption{test}
\label{fig:SST}
\end{figure}
\section{References}
\bibliography{References.bib}
\bibliographystyle{agsm}
\end{document}
Generally, LaTeX's attitude is ‘you write the text, let me worry about placing the figures’.
Floats (that is, figures and tables) will float to where LaTeX thinks they go best. I can't remember the precise rules in the article style, but generally they float to the top of the nearest following page that has room for them. LaTeX won't fill too much of a page with figures, so if you have a dense sequence of large-ish figures, some of them can end up quite a long way away from the point in the source text where they're written.
This is normal, and how figures and tables appear in the majority of published books and articles.
You'll usually write a caption:
\begin{figure}
\includegraphics{...blah...}
\caption{This is a caption for my figure\label{fig:SST}}
\end{figure}
Then you can write
...this is shown in Figure~\ref{fig:SST}
If you want to, you might add ...on p.\thinspace\pageref{fig:SST} and LaTeX will insert the page number where the figure ends up. Also, see the prettyref package.
As mentioned in the other answer, you can add placement specifiers like [ht], which give LaTeX permission to put the figure ‘here’ or at the top of a page, but that doesn't force anything. It is possible to fight with LaTeX about this (the Overleaf docs give some hints), but it's generally not worth it in my experience.
That said, one bit of guidance it's useful to give LaTeX is something like \renewcommand{\floatpagefraction}{.8}, which says that LaTeX is allowed to use as much as 80% of the page for floats (ie, figures and tables) – the default value is a bit tight (there are more comments on this in this TeX.SE question – tex.stackexchange.com is generally a better site for LaTeX-related questions).
If you do feel compelled to fight/fiddle with float placement, do it at the very end of the process, where you're fine-tuning the punctuation of your document. Small changes in the document can have big effects on where floats end up, and it's simply not worth worrying about this until your document is almost finalised.
Your final puzzlement was about the figure ending up after the references. In the case of your document, the figure goes on p.2, so there's plenty of space on p.1 for LaTeX to carry on setting text, so naturally it puts the references there. Since you generally do want the references to go at the very end of an article, this is a case where it's reasonable to exert a little control over placement, and a good idea would be to put \clearpage before the References section. What that does is to make a new page and force out any floats which are still in the queue waiting for space to appear.

How to make all numbers and letter take up the same amount of space in latex?

I'm trying to show step by step how to sort an array. But since the numbers and letters take up different amounts of space, the columns get staggered. I'm very new to latex and am not sure if there's an easy fix for this.
If that's not possible, is there some kind of grid system I can use to keep them in straight lines? (without showing the grid).
You can place each of the elements in a box that are of similar width and height. This will ensure that each take up the same amount of space. It's possible using eqparbox's \eqmakebox[<tag>][<align>]{<stuff>}. All \eqmakeboxes with the same <tag> are put in a box of maximum width of <stuff>. You can adjust the <align>ment of every element, if needed (centred, left-aligned or right-aligned).
\documentclass{amsart}
\usepackage{eqparbox}
\newcommand{\swl}[2][nmbr]{\eqmakebox[#1]{\strut #2}}
\begin{document}
\begin{tabular}{c}
\swl{a}\swl{b}\swl{m}\swl{0} \\
\swl{i}\swl{i}\swl{1}\swl{n}
\end{tabular}
\end{document}
The above code provides a wrapper \swl[<tag>]{<stuff>} that provides a similar-width letter macro. If you want a blank space, you can use \swl{~}.
eqparbox uses TeX's \label-\ref system to store the widths and therefore requires at least two compilations for any change (increase in width) in elements under the same <tag>.

Adding caption in LaTex without floats (tables)?

I have a few tables which are not long enough to warrant the use of \longtable, but they always start on the next page which breaks the whole flow and trying to force it with a [!h] in the table did not help. So I stopped using \tables and just have \tabular tag now which seems to have fixed the layout as there are no floats.
But the problem now is I can't get automatic captions for the Tables - any ideas how I can do this and also get auto numbering so when I use \addcontentsline it can show up in the TOC?
To use a caption outside a float environment, one needs to use \captionof which is part of package caption.
Example
\documentclass{article}
\usepackage{caption}
\begin{document}
\begin{center}
\begin{tabular}{rl}
right & left \\
right & left \\
\end{tabular}
\captionof{table}{Your caption here}
\end{center}
\end{document}
There is also a capt-of package if you are just interested in using the command \captionof.
Short answer; you need a \table for the caption. Only using \tabular + captioning is not gonna work.
bit longer
There may be a nasty work-around as people tend to create work-arounds for everything, but my base rule for most of those work-arounds is: "if latex does not provide it by itself, do not try to do it". Most of time those "fixes" will give even more text-flow problems anyway.
Now if you really want it right below the text just do a \clearpage before the table and place the table. Than the rest of the text. This will probably solve it, although it will give an empty space on the rest of your 'previous' page of course.

positions of page numbers, position of chapter headings, chapters AND Table of Contents, References

I am writing my PhD thesis (120+ pages) in latex, the deadline is approaching and I am struggling with layout problems.
I am using the documentstyle book.
I am posting both problems in this one thread because I am not sure if the solution might be related to both problems or not.
Problems are:
1.) The page numbers are mostly located on the top-right of each page (this is correct and where I want them to be).
However, only on the first page of chapters and on the first page of what I call "special chapters", the page number is located bottom-centered.
With "special chapters" I mean: List of Contents, List of Figures, List of Tables, References, Index.
My university will not accept the thesis like this. The page number must ALWAYS be top-right one each page, even if the page is the first page of a chapter or the first page of something like the List of Contents.
How can I fix this?
2.) On the first page of chapters and "special chapters" (List of Contents...), the chapter title is located far too low on the page. This is the standard layout of LaTeX with documentstyle book I think.
However, the chapter title must start at the very top of the page! I.e. the same height as the normal text on the pages that follow.
I mean the chapter title, not the header.
I.e., if there is a chapter called
"Chapter 1
Dynamics of foobar under mechanical stress"
then that text has to start from the top the page, but right now it starts several centimeters below the top.
How can I fix this?
Have tried all kinds of things to no effect, I'd be very thankful for a solution!
Thanks.
A try to answer
problem #1.
Even if you're using the headings pagestyle, or your custom pagestyle, the special pages (chapter beginnings and so on) are formatted with the plain pagestyle.
To avoid this, load the fancyhdr package (as mentioned in the previous answer) with
\usepackage{fancyhdr}
in your preamble. Then, (always in the preamble) define your custom pagestyle.
For normal pages (assuming you're not using twoside as an option of \documentclass[]{}):
\fancypagestyle{phdthesis}{%
\fancyhf %clear all headers and footers fields
\fancyhead[R]{\thepage} %prints the page number on the right side of the header
}
For special pages:
\fancypagestyle{plain}{%redefining plain pagestyle
\fancyhf %clear all headers and footers fields
\fancyhead[R]{\thepage} %prints the page number on the right side of the header
}
After doing this, you can set you page style declaring \pagestyle{phdthesis} right before \begin{document}.
For further details, refer to the fancyhdr package documentation.
Now trying to answer
problem #2
As a first attempt, you can use the titlesec package, using the option compact. In the preamble, type:
\usepackage[compact]{titlesec}
If you're not completely satisfied with this solution, you can specify the spacing above and below the titles with \titlespacing
\usepackage{titlesec}
\titleformat{ command }[ shape ]{ format }{ label }{ sep }{ before }[ after ]
\titlespacing{ command }{ left }{ beforesep }{ aftersep }[ right ]
With \titleformat you can define your own style for chapter titles, and then you can define the spacing with \titlespacing.
I don't know which style of titles you have to use, so it's better for you to have a look to the package documentation (you can recall package documentation typing texdoc NameOfThePackage in a terminal).
Please note that you need to define the chapter title format in order to specify its vertical spacing (page 5 of the documentation). As an example:
\usepackage{titlesec}
\titleformat{\chapter}[hang]{\huge}{\thechapter}{1em}{}
\titlespacing{\chapter}{0pt}{0pt}{1cm}
With these commands you have the chapter title with the number and the chapter name on the same line, a 0 pt space before the title, and a 1 cm space between the title and the follwing text.
To change the page appearance, you can use the fancyhdr package. It's probably a change in the \pagestyle used for the special chapters.
To change the appearance of the chapter title (to have the chapter title printed from the top on the page, you have to use a modified style that change \chapter command rendering.
Here is an example of command I used for my own thesis. It is probably not the appearance you want but gives you an hint about the command you have to redefine.
% modified from book.ltx latex sources
\def\#makechapterhead#1{%
\thispagestyle{empty}%
\vspace*{50\p#}%
\vspace*{10\p#}%
{\parindent \z# \centering \reset#font
\thickhrulefill\quad
\scshape \#chapapp{} \thechapter
\quad \thickhrulefill
\par\nobreak
\vspace*{10\p#}%
\interlinepenalty\#M
\hrule
\vspace*{10\p#}%
\Huge \bfseries #1\par\nobreak
\par
\vspace*{10\p#}%
\hrule
\vskip 40\p#}}
Regarding problem #2 an alternative without additional packages is to change the vertical offset of the page, and then resetting it to the previous value.
\voffset -1in
\chapter{Your chapter}
% text
\voffset 0in

Making LaTeX tables smaller?

I have a LaTeX table that looks like this:
\begin{table}[!ht]
\centering
\small
\caption{
\bf{Caption}}
\begin{tabular}{l|c|c|l|c|c|c|c|c}
field1 & field 2 & ... \\
\hline
...
the problem is that even with "\small" the table is too big, since I use:
\usepackage{setspace}
\doublespacing
in the header. How can I:
Make the table single spaced? and
Make the table smaller?
I'd like it to fit on an entire page.
http://en.wikibooks.org/wiki/LaTeX/Tables#Resize_tables talks about two ways to do this.
I used:
\scalebox{0.7}{
\begin{tabular}
...
\end{tabular}
}
As well as \singlespacing mentioned previously to reduce the height of the table, a useful way to reduce the width of the table is to add \tabcolsep=0.11cm before the \begin{tabular} command and take out all the vertical lines between columns. It's amazing how much space is used up between the columns of text. You could reduce the font size to something smaller than \small but I normally wouldn't use anything smaller than \footnotesize.
if it's too long for one page, use the longtable package. and if it's too wide for the page, use p{width} in place of l,r, or c for the column specifier. you can also go smaller than \small, i.e. \footnotesize and \tiny. I would consult the setspace package for options on how to remove the double space, though it's probably \singlespace or something like that.
There is also the singlespace environment:
\begin{singlespace}
\end{singlespace}
You could add \singlespacing near the beginning of your table. See the setspace instructions for more options.
If you want a smaller table (e.g. if your table extends beyond the area that can be displayed) you can simply change:
\usepackage[paper=a4paper]{geometry} to \usepackage[paper=a3paper]{geometry}.
Note that this only helps if you don't plan on printing your table out, as it will appear way too small, then.

Resources