Is there a way to get LaTeX to place figures in the same page as a reference to that figure? - latex

I am using LaTeX and the figure environment.
I am well familiar with the parameters to that environment: [htbp], and I am also generally familiar with the strategies that LaTeX takes, by default, to decide where to put each figure. For example, by placing figures at the top or bottom of the page.
What I am wondering is whether there is a package, macro, or some commands that I can give so that if I have a single-column document and I mostly have a single in-text reference to a figure, that the figure would be more likely to be placed in the same page of the reference?
For example, imagine that I have a long paragraph which in the middle has a \ref{FIG:X}. When rendered, some of the paragraph appears before the page break, and some appears after the page break. I can also place the figure command somewhere before and after the whole paragraph. Is there a way to get it to actually be placed in the same page as the actual reference?

I don't want to sound too negative, but there are occasions when what you want is almost impossible without a lot of "artificial" tuning of page breaks.
If the callout falls naturally near the bottom of a page, and the figure falls on the following page, moving the figure back one page will probably displace the callout forward.
I would recommend (as far as possible, and depending on the exact size of the figures):
Place the figures with [t] (or [h] if you must)
Place the figures as near as possible to the "right" place (differs for [t] and [h])
Include the figures from separate files with \input, which will make them much easier to move around when you're doing the final tuning
In my experience, this is a big eater-up of non-available time (:-)
In reply to Jon's comment, I think this is an inherently difficult problem, because the LaTeX guys are no slouches. You may like to read Frank Mittelbach's paper.

Yes, include float package into the top of your document and H (capital H) as a figure specifier:
\usepackage{float}
\begin{figure}[H]
.
.
.
\end{figure}

You can always add the "!" into your float-options. This way, latex tries really hard to place the figure where you want it (I mostly use [h!tb]), stretching the normal rules of type-setting.
I have found another solution:
Use the float-package. This way you can place the figures where you want them to be.

I solve this problem by always using the [h] option on floats (such as figures) so that they (mostly) go where I place them. Then when I look at the final draft, I adjust the location of the float by moving it in the LaTeX source. Usually that means moving it around the paragraph where it is referenced. Sometimes I need to add a page break at an appropriate spot.
I've found that the default placement of floats is reasonable in LaTeX, but manual adjustments are almost always needed to get things like this just right. (And sometimes it isn't possible for everything to be perfect when there are lots of floats and footnotes.)
The manual for the memoir class has some good information about how LaTeX places floats and some advice for manipulating the algorithm.

If you want force this behaviour on all of your figures try
...
\usepackage{float}
\floatplacement{figure}{H}
...

Maybe this will help you?
\begin{center}
\includegraphics[scale=0.5]{picture_name}
\end{center}
I think is better to use the graphics command when your figures run away.

I have some useful comments. Because I had similar problem with location of figures.
I used package "wrapfig" that allows to make figures wrapped by text.
Something like
...
\usepackage{wrapfig}
\usepackage{graphicx}
...
\begin{wrapfigure}{r}{53pt}
\includegraphics[width=53pt]{cone.pdf}
\end{wrapfigure}
In options {r} means to put figure from right side. {l} can be use for left side.

\setcounter{topnumber}{2}
\setcounter{bottomnumber}{2}
\setcounter{totalnumber}{4}
\renewcommand{\topfraction}{0.85}
\renewcommand{\bottomfraction}{0.85}
\renewcommand{\textfraction}{0.15}
\renewcommand{\floatpagefraction}{0.7}
http://robjhyndman.com/researchtips/latex-floats/

One way I found that helps with this is to use \include{file_with_tex_figure_commands}
(not input)

Related

How to place the figure at the bottom of page in latex?

I am having problem in placing the figure at the bottom of page in latex. When I specify the \begin{figure*}[b], all the figures in my documents goes at the end of document. What I need is just place one specific figure at the bottom of current page or next page.
Please guide me how can I do this?
In order to place a float at the bottom of a two-column document, you can use the dblfloatfix package. From the brief package description:
The pack­age solves two prob­lems: floats in a twocol­umn doc­u­ment come out in the right order and al­lowed float po­si­tions are now [tbp].
So, after \usepackage{dblfloatfix} you can use
\begin{figure*}[btp]
% <your figure here>
\end{figure*}
Like will all floats (especially in a two-column environment), the float may not end up where it is placed. As such, you may have to move around the figure* declaration to achieve the desired location within your document.
stfloats provides a similar functionality.
Reference: Put a table* at the bottom of a page?
Werner's solutions didn't actually work for me.
So I just used this -
\begin{figure*}[b]
% <your figure here>
\end{figure*}
source: https://www.overleaf.com/learn/latex/Positioning_of_Figures
Using \begin{figure*}[!b] worked for me.

How to place a wide figure with subfigures in Latex?

I am trying to write a report and stuck with a wide figure. My document type is PRL using revtex4.1 with two columns. I have a wide figure which consists of 8 subfigures. I am trying to place it bottom of a page but it insists to go next page. Here is code for my wide figure:
\begin{figure*}
\centering
\subfloat[s1]{\label{fig:s1}\includegraphics[width=0.20\textwidth]{s1.png}}\qquad
\subfloat[s2]{\label{fig:s2}\includegraphics[width=0.20\textwidth]{s2.png}}\qquad
\subfloat[s3]{\label{fig:s3}\includegraphics[width=0.20\textwidth]{s3.png}}\qquad
\subfloat[s4]{\label{fig:s4}\includegraphics[width=0.20\textwidth]{s4.png}}\\
\subfloat[s5]{\label{fig:s5}\includegraphics[width=0.20\textwidth]{s5.png}}\qquad
\subfloat[s6]{\label{fig:s6}\includegraphics[width=0.20\textwidth]{s6.png}}\qquad
\subfloat[s7]{\label{fig:s7}\includegraphics[width=0.20\textwidth]{s7.png}}\qquad
\subfloat[s8]{\label{fig:s8}\includegraphics[width=0.20\textwidth]{s8.png}}
\caption{\label{fig:s}Caption}
\end{figure*}
Try also
\begin{figure*}[!hb]
If your figure is too wide, this might help: Centering wide tables or figures.
I don't know whether it will work, but have you tried using placements, ie:
\begin{figure*}[b]
where b stands for bottom?
You could also try using the float package (put this in the preamble, before \begin{document}:
\usepackage{float}
and then using
\begin{figure*}[H]
to specify exactly where you want your figure to be, i.e. put the figure exactly in the position in relation to your text where you want the figure to be placed.
It may be that your figure is just too big and it won't fit there, so you may have to reduce the size of the figures or fiddle with the margins (not recommended, but possible).
Lastly, in 6 days the LaTeX question and answer website will be open to everyone, and you might find it useful.

dealing with large figures in Latex

I have a large figure that appears at the end of my document rather than in the section that I want to be in. Even \begin{figure}[h] doesn't help. Without scaling it down, how can I put it at the end of the section I want it in?
Using the afterpage package can be a good solution. However, using the option here you are trying to tell LaTeX where you want to put the image. Instead, you need to tell LaTeX where the image is good to be put:
use \begin{figure}[tb] for figures that fit well in a page with text (say, half of the text height for the figure and the other half for the text)
use \begin{figure}[p] for floats large enough to require a dedicated page.
Setting a proper option increase your chances to have the image almost where you want, having at the same time a good page layout.
If the figure is still too far from the page where it should be placed, you can set some "barriers" for floats positioning with the packages placeins or afterpage (already mentioned).
Here is a small tutorial for float placement. The thing you want to do is put an \afterpage{\clearpage} command at the end of the section. This will create an additional page after the current one and place the floats that are left in the queque there. If the float still doesn't get placed, you have to resize it. If you really don't want to resize it and it should fit on the page, then you could try changing the margins and text area temporarily (i.e. just for that one page) and see if that lets the float get placed.
i forget if it's the float or array package that provides this, but,
\begin{figure}[H]
...
\end{figure}
The upper case H will put the figure exactly where it is in your code.

Adding full page figures in Latex, how?

I have a full-page figure that LaTeX keeps putting at the end because of its size.
I would like it to be integrated on a separate page in the flow of text. How can I do it?
\begin{figure}[hbtp]
h = here
b = botom
t = top
p = page of floats
Algorithm will try the current position in document first, then bottom, then top and then on a seperate page. If you just specify 'h' you will force placement where the figure command is in the document. The order is encoded by you -- i.e., you could specify ptbh which would force a reverse order of evaluation.
p.s., I would strongly recomend anyone using latex to use lyx. Its just as powerfull as straight latex but its wysiwyg and you can drop to tex anywhere you want.
If you also want the caption to be rotated, you can also use
\usepackage{rotating}
\begin{sidewaysfigure}
That also eliminates the need for using angle=90 or something like that
\begin{figure*}[tb]
\centering
\makebox[\textwidth]{\includegraphics[width=.9\paperwidth]{./fig/fig5.eps}}
\caption{The SAP data model based on an UML class diagram: classes and relationships.}
\label{fig:Fig5}
\end{figure*}

Inserting images in LaTeX

I have a question on inserting images into a LaTeX document. I try to insert images using the keyboard short cut: Ctrl-Alt-G and I'm able to insert images. But the compiled pdf document shows all the images at the end, whereas I want to interleave images with text. Something like the following:
Text1
Image1
Text2
Image2
Text3
Image3
I try to insert images at right positions i.e. in between text, but on compilation, they all appear at the end. I have tried different options provided on the image insertion UI but same result.
Any idea where I'm going wrong.
Related SO question.
You'll have to use graphicx package:
\usepackage{graphicx}
and then you just use \includegraphics
\includegraphics{myfig.pdf}
\includegraphics[width=60mm]{myfig.png}
\includegraphics[height=60mm]{myfig.jpg}
\includegraphics[scale=0.75]{myfig.pdf}
\includegraphics[angle=45,width=52mm]{myfig.jpg}
Try downsizing the images. Maybe they are too large and so they are moved to the end of the document..
Hope it helps.
What code did you use for the \figure environment? In most cases the "h" option should at least help a little bit there.
This is a FAQ: "Moving tables and figures in LaTeX". Note especially the third dot point, which relaxes some of the restrictions LaTeX uses to position floats.
That's the best answer I can give without seeing an example of how large your floats are and how you're inserting them into the document. Provided that they're reasonably-sized, you should have no problem with
\begin{figure}[htbp]
\includegraphics{myfig}
\caption{...}
\label{fig:myfig}
\end{figure}
And note that if the float is too large to fit then it will move to a subsequent page -- this is the whole idea behind getting LaTeX to help you with the formatting. You certainly don't want to end a page prematurely just because there's a figure coming up next that otherwise doesn't fit.
Maybe this can help you in general...I just hate the LaTeX way of making everything too advanced (flexible) at all times.
Beside, the source looks really awful.
It will not solve you initial problem, but since it will be easier to change size of each image you can at least try...
% Easy image insert
% use as \img{imagename}{caption}{label}
% will insert image with with 70% of textwidth
% change below for other width
\newcommand{\img}[3]{
\begin{figure}[!ht]
\centering
\includegraphics[width=0.7\textwidth]{#1}
\caption{#2}
\label{fig:#3}
\end{figure}
}
\img{myimage}{has this caption}{and_this_label}
the label is automatically prefixed with fig:.
Good luck!
/C
Using [!t] and [!h] in the figure environment, rather than [t], [h] etc seems to help, though I've yet to find a surefire way to get large images in sensible places. Sometimes it works half way through a document, othertimes it doesn't.
Sometimes just changing the width or height using an option to the includegraphics statement (square brackets before the filename a.k.a \includegraphics[width=foo]{}) will do the trick.
\begin{figure}[H!]
\includegraphics{myfig}
\caption{...}
\label{fig:myfig}
\end{figure}
Use H! to denote that you want your picture right there.
\documentclass{article}
\usepackage{graphicx}
\begin{document}
text
\begin{figure}[tbh]
\centering
\includegraphics[width=0.4 \textheight]{ & directory path of jpg. width defines the width of the page and one one can put in its place height which is text page height }
\caption{name of the jpg}
\end{figure}
% it worked for me.
\end{document}

Resources