Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I am new to Tex and this question maybe is not written well, so I will try to explain it better.

I am writing a reference manual where there are a lot of "blocks" that all have the same structure: a piece of text (maybe one or two short paragraphs) and then an image (tipically a screenshot).

I would like to be able to define such a block in a way that, when exporting document to PDF, the output is on the same page. I understand that there will be some pages half-filled when this "block" begins and does not fit into the current page. In these cases, I expect the block to be placed entirely at the next page.

I have been playing with \begin{samepage}, \begin{minipage} and things like that (searching a lot in this same web) with no luck. Obviously I am doing something wrong, or taking a bad approach to solve the problem.

Take a look at my code:

Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Phasellus leo ipsum, dictum non auctor quis, euismod vitae enim. Mauris varius.

\begin{figure}[h]
  \begin{center} \includegraphics[scale={0.7}]{IMG1} \end{center}
  \caption{Image caption}
  \label{fig:IMG1}
\end{figure}

I would like all this piece of text (2 paragraphs) and one figure to be always in the same page.

Can this be achieved? In fact I thought I got it when I discovered \begin{minipage}! If this is the case, can you please write down a short code showing how to use it? (remember I am a newbie and some obvious things are not so obvious for me right now).

Edit: I will try @Werner tip, but I am already using a \newcommand to short all the previously written \begin{figure} environment. As this:

\newcommand{\figureSF}[4]{
  \begin{figure}[h]
    \begin{center} \includegraphics[scale={#4}]{#1} \end{center}
    \caption{#2}
\label{#3}
\end{figure}
}

So the previous piece of code turns into:

Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Phasellus leo ipsum, dictum non auctor quis, euismod vitae enim. Mauris varius.

\figureSF{IMG1}{Image caption}{fig:IMG1}{0.7}

As this new command accept only 4 parameters, is there an easy way to modify it to handle the situation you are describing?

share|improve this question
add comment

1 Answer

It seems like you're writing a bunch of text and then inbetween there are these "blocks" that you want to be "kept together." For this, seeing as you currently already use a figure environment for the image, you can include the paragraph text within your figure. TeX does not care what's containing inside the environment - it could be a table, an image, some text, whatever. Here's a small example:

enter image description here

\documentclass{article}
\usepackage{lipsum}% http://ctan.org/pkg/lipsum
\usepackage[demo]{graphicx}% http://ctan.org/pkg/graphicx
\begin{document}
\lipsum[1-2]
\begin{figure}
  \hrulefill\par
  \lipsum[3-4]
  \centering\includegraphics[scale={0.7}]{IMG1}
  \caption{Some caption}\label{fig:label1}
  \hrulefill
\end{figure}
\lipsum[5-6]
\lipsum[7-8]
\begin{figure}
  \hrulefill\par
  \lipsum[9-10]
  \centering\includegraphics[scale={0.7}]{IMG2}
  \caption{Some other caption}\label{fig:label2}
  \hrulefill
\end{figure}
\lipsum[11-12]
\end{document}​

I've added an \hrulefill above and below the floating "block" to make it clear that the text content floats with the image. Also, the [demo] option to graphicx is just for this example since I don't have the images you mention/use.

Note that all float-related restrictions (and benefits!) pertain to this usage. For example, the portion of text/float allowed at the top/bottom of a page, etc. More on this is available from the layouts package documentation (section 6 Float layouts, p 21).

In terms of the figure placement, see How to influence the position of float environments like figure and table in LaTeX? (and Keeping tables/figures close to where they are mentioned).

Bundling this into a single command is also possible:

\documentclass{article}
\usepackage{lipsum}% http://ctan.org/pkg/lipsum
\usepackage[demo]{graphicx}% http://ctan.org/pkg/graphicx
\newcommand{\figureSF}[5]{%
  \begin{figure}[ht]
    \hrulefill\par
    #1\par\bigskip
    \centering\includegraphics[scale=#5]{#2}
    \caption{#3}\label{#4}
    \hrulefill
  \end{figure}
}
\begin{document}
\lipsum[1-2]
\figureSF{\lipsum[3-4]}{IMG1}{Some caption}{fig:label1}{0.7}
\lipsum[5-6]
\lipsum[7-8]
\figureSF{\lipsum[9-10]}{IMG2}{Some other caption}{fig:label2}{0.7}
\lipsum[11-12]
\end{document}​
share|improve this answer
    
Thanks! (I still can not upvote you). I have edited my original question (addition at the end). Is your tip still useful in that situation? –  ismdiego Aug 30 '12 at 18:21
    
Nice! I have just tried it and it worked well. Now the problem is related to the text next to this "group", which is rendered before it. I need to read your links and better understand float placement –  ismdiego Aug 30 '12 at 18:26
    
@ismdiego: Yes, you can still use your approach. I've updated your \figureSF command to now take 5 arguments, where the first is the preceding text, followed by the regular content you have. –  Werner Aug 30 '12 at 18:26
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.