Graphics, Figures & TablesHow to force images to stay in order after text

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
Aegidius
Posts: 7
Joined: Wed Dec 25, 2013 2:04 pm

How to force images to stay in order after text

Post by Aegidius »

Hello.

I've got some problems with Latex. For example I have this code.

Code: Select all

Le informazioni provenienti dai sensori mostrano chi si sta allenando e chi no. L'applicazione darà una rappresentazione visiva di ciò mostrando una cornice verde intorno agli avatar degli utenti attivi e rossa intorno a quelli degli utenti inattivi.

\begin{figure}
  \centering
  \includegraphics[width=90mm]{/home/egidio/documents/laurea/img/design-2.png}
  \caption{Design 1: social}
\end{figure}

Dato che i sensori sono impegnati a rilevare chi si sta esercitando, le informazioni sullo stato delle attività vengono inserite manualmente.
I get a PDF file where all the images are placed at the end of the document, one per page.
I need that order of images and text be respected, cause text refers next images.

Do you know how can I get it?

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

User avatar
Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

How to force images to stay in order after text

Post by Johannes_B »

That is a faq. You tell LaTeX to let the figures float in your document to find a good place to put them. If you really don't want your picture to float, just delete the figure environment. Captions won't work, tough (see capt-ot or caption), but you will not need them anyway.

Find out more at Floats, figures and captions.

I strongly recommend using floats, though. Otherwise your text will look lick the hack monster slashed through it.
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
Aegidius
Posts: 7
Joined: Wed Dec 25, 2013 2:04 pm

How to force images to stay in order after text

Post by Aegidius »

Thank you! I solved my problem in this way.

Code: Select all

\centerline{
  \includegraphics[width=90mm]{/home/egidio/documents/laurea/img/design-5.png}
}
Too bad for the captions. I would prefer to have them, but the order of figures and text is more important.

Do you know if it is possible to define default margin for all figures in the pages (in CSS style)?

Thank you again Johannes_B!
User avatar
Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

How to force images to stay in order after text

Post by Johannes_B »

You can have captions, but you don't need them. See caption or capt-of.

What do you mean by default margins?

Btw: Maybe package floatrow is interesting for you.
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
Aegidius
Posts: 7
Joined: Wed Dec 25, 2013 2:04 pm

Re: How to force images to stay in order after text

Post by Aegidius »

Thank you Johannes_B.

Why do you say I do not need the captions?

Now the images are too close to the previous line. How can I add top margin for all of them?
User avatar
Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

How to force images to stay in order after text

Post by Johannes_B »

Captions describe the picture (or table) in a few words/sentences and tag it with a number. In your text you can refer to that number (see figure~\ref{fig:myFig}). One needs those numbers, because in a well typeset book, figures and tables are placed where they best fit (floating). If you prevent your objects from floating, placing the figures under the describing text, there is no need to refer to a number, and there is no need to describe them anymore, since »see the figure below« is sufficient.

Drawback: Remember the hack monster? Your text will look ugly with lot's of whitespace.
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
Aegidius
Posts: 7
Joined: Wed Dec 25, 2013 2:04 pm

How to force images to stay in order after text

Post by Aegidius »

Hello Johannes_B.

I'm inserting inline images in this way

Code: Select all

\centerline{
  \includegraphics[width=90mm]{/home/egidio/Dropbox/laurea/img/nursing-home.jpg}
}
but I need to add captions to images. I'm not able to use this external package. Would you please help me?
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

How to force images to stay in order after text

Post by cgnieder »

Aegidius wrote:I'm inserting inline images in this way

Code: Select all

\centerline{
  \includegraphics[width=90mm]{/home/egidio/Dropbox/laurea/img/nursing-home.jpg}
}
I'd avoid \centerline. This is a Plain TeX command that LaTeX only keeps for compatibility. If you use LaTeX's {center} environment you'll get also some vertical space above and below (what I guess you meant by margins?)
Aegidius wrote:but I need to add captions to images. I'm not able to use this external package. Would you please help me?
Instead of capt-of you can use caption and its \captionof command:

Code: Select all

\documentclass{article}
\usepackage{caption}
\usepackage{mwe}% example images
\usepackage{kantlipsum}% example text
\begin{document}

\kant[1]

\begin{center}
  \includegraphics{example-image-a}
  \captionof{figure}{A non-floating figure \emph{with} a caption!}
\end{center}

\kant[2]

\end{document}
captionof1.png
captionof1.png (29.33 KiB) Viewed 136104 times
Should you happen to use a KOMA-Script class you can use \captionof out of the box:

Code: Select all

\documentclass{scrartcl}
\usepackage{mwe}% example images
\usepackage{kantlipsum}% example text
\begin{document}

\kant[1]

\begin{center}
  \includegraphics{example-image-a}
  \captionof{figure}{A non-floating figure \emph{with} a caption!}
\end{center}

\kant[2]

\end{document}
If your class is memoir you can define something called fixed caption:

Code: Select all

\documentclass{memoir}
\newfixedcaption{\figcaption}{figure}
\usepackage{mwe}% example images
\usepackage{kantlipsum}% example text
\begin{document}

\kant[1]

\begin{center}
  \includegraphics{example-image-a}
  \figcaption{A non-floating figure \emph{with} a caption!}
\end{center}

\kant[2]

\end{document}
If non of that helps you then please provide a Infominimal working example (MWE) similar to the code I posted.

Regards
site moderator & package author
Aegidius
Posts: 7
Joined: Wed Dec 25, 2013 2:04 pm

Re: How to force images to stay in order after text

Post by Aegidius »

Thank you very very much cgnieder! You are an angel!

Now my PDF is much better then the previous one, but I have a little problem. Sometimes, where the figure is close to the bottom of the page, the caption is placed on the next page. Do you know how to avoid it?

Thank you again.

Egidio
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

How to force images to stay in order after text

Post by cgnieder »

Aegidius wrote:Thank you very very much cgnieder! You are an angel!
I simply provided an example. Johannes already mentioned the caption package earlier :)
Aegidius wrote:Now my PDF is much better then the previous one, but I have a little problem. Sometimes, where the figure is close to the bottom of the page, the caption is placed on the next page. Do you know how to avoid it?
You could put everything inside the {center} environment in a {minipage} or a similar box. This somehow defeats the purpose of the {center} environment as the contents of the {minipage} need to be centered again, but...

Code: Select all

\documentclass{article}
\usepackage{caption}
\usepackage{mwe}% example images
\usepackage{kantlipsum}% example text
\begin{document}

\kant[1]

\begin{center}
  \begin{minipage}{\linewidth}
    \centering
    \includegraphics{example-image-a}
    \captionof{figure}{A non-floating figure \emph{with} a caption!}
  \end{minipage}
\end{center}

\kant[2]

\end{document}
Regards
site moderator & package author
Post Reply