1

When I add a figure and caption, I have two options:

  1. Use figure option

It means that my code is likely

\begin{figure}
    \centering
    \includegraphics{my picture} 
    \caption{my caption here}
    \label{my label here}
\end{figure}
  1. Use caption package

It means that my code is likely

\usepackage{caption}
...
\begin{center}
    \includegraphics{my picture} 
\end{center}
\captionof{figure}{my caption here}
\label{my label here}

Here, should I avoid one of them, or I can just use whatever I prefer? I personally prefer the second option, because it behaves as I expected, whereas the first option put the pictures some wiered positions.

Though I have a preference between them, I'm asking this because I found some ways of writing LaTeX is not recommended, like eqnarray vs align.

It would be appreciated if you let me know any difference between them I haven't noticed.

  • 4
    your question can not really be answered in this form, frame by default is not defined (actually it would clash with the \frame command and give errors. Did you mean figure ? It is usually best to use figure but note the only function of the figure environment is to allow the content to be moved and re-inserted to avoid bad white space at page breaks. So moving to a new position is the expected behaviour not a problem to be solved. – David Carlisle Dec 11 '19 at 08:05
  • Oops, I just realized that I write frame instead of figure in the first option. I just editted it to figure. – user203101 Dec 11 '19 at 08:08
  • 1
    In your second variant the caption can end on a different page. You should at least add a minipage. But beside this there are a lot questions about the pro and cobs of floats. See e.g https://tex.stackexchange.com/questions/370627/why-should-the-h-option-not-be-used-in-floats – Ulrike Fischer Dec 11 '19 at 08:22
  • 1
    the \captionof is misplaced in the second example, it should be inside the center environment (and probably inside a minipage) – David Carlisle Dec 12 '19 at 01:19

1 Answers1

1

There are places were you cannot place a float as figure, like a minipage, but you want a figure with numbered caption. Then have sense the second option.

Otherwise use the first method even if you do not want moving figures. Instead, use the [h] option, or even [H] of package `float. The reason is that floats also take care of spaces above and below the caption, with another floats and the surrounding text, and you can change that setting consistently in the preamble.

While the second approach in the next MWE show two problems:

(1) The risk to end in another page, as pointed Ulrike Fisher

(2) The lack space between the caption and the following text.

\documentclass{article}
\usepackage{caption}
\usepackage{lipsum}
\usepackage{graphicx}
\begin{document}

\lipsum[1]

\begin{figure}[h]
    \centering
    \includegraphics[scale=.2]{example-image} 
    \caption{my caption here}
    \label{my label here}
\end{figure}

\lipsum[2-7][1-38] % wrong result 2: caption in another page

\begin{center}
    \includegraphics[scale=.2]{example-image} 
\end{center}
\captionof{figure}{my caption here}
\label{my label here}

\lipsum[3] % wrong result: No space under caption

\end{document}

With a middle paragraph shorter, you will note still a third problem: a different space above the caption. As you as you can see by the comments, this is however a bad use of \captionof, causing the warning "outside box or environment", so if you use instead as option B:

\begin{center}
    \includegraphics[scale=.2]{example-image} 
    \captionof{figure}{my caption here}
\end{center}

This way you avoid any warning, and the excessive space between the image and the caption, but note, this is still not equivalent to option A:

\begin{figure}[h]
    \centering
    \includegraphics[scale=.2]{example-image} 
    \caption{my caption here}
\end{figure}

Make now a MWE with only A+A+A+B+B+B+B. You can note that:

1) Center is still a breakable environment (in last B).

2) The space under each float (A) is bigger (and look better) that under the caption of each center environment (B).

One option to solve (1) is, as pointed several times, is use a minipage:

Option C:

\noindent\begin{minipage}{\linewidth}
\centering
    \includegraphics[scale=.2]{example-image} 
    \captionof{figure}{my caption here} 
\end{minipage}

But compare now with A+A+A+C+C+C+C: The small space after the captions of minipages is really ugly. But in a real document it could be similar or higher than in floats, depending of \parskip settings. You should also pay attention to \parindent and where is the minipage whitin the paragraph. Just after a blank line, the minipage start a paragraph, and it will be indented, changing the apparent center of the figure, whereas after another minipage will be in the second line of paragrah, thus without indentation.

On the other side, floats never are breakables, and never will be indented, and the spacing of floats will be independent of \parskip, but controlled by \textfloatsep, \floatsep and \intextsep (see article defaults here).

However, consider that a non-float box have some other inherent problems.

1) It cannot move from the desired position. Yes, maybe what you want, but what happens if it is too large for the space still available on the page? The figure pass to the next page leaving all this space empty. With a float with [h], the figure can pass to the top of next page equally (is changed to if needed[ht] automatically) , but filling that space with following text. The document layout will be incomparably better, and if you use labels for make a references in text, this displacement is a minor inconvenient.

2) The float rules have not effect in a non-float (obviously). Floats rules prevent you of use, for instance, put four big figures on top of the page so you can only add 4 or five lines of text at the bottom. But a non-float figure disregard \topfraction and topnumer counter or totalnumber counter, so no one will avoid that horrible layout. This rules can be changed, or even ignored (e.g., [h!]) and how important they are depend of what type of document are doing, but for a book or a thesis the document, most probably their layout will look better with that rules.

If the problem is only that the float is too mobile, you should read first How to influence the position of float environments like figure and table in LaTeX?, then use the h option (e.g.: \begin{figure}[h] ...), or even h!, and if it is still not immobile enough, add the package float and change that option to H. My advice is, however, avoid these options (if fact, all float options) and consider only that in the final draft. First check if worth change the default floats positions, the tune float rules (for instance, allowing one or two more floats per page) and finnally set the options for some float if needed.

Considering all the above, a figure environment is a priori is the preferable choice, imho, and do not need any package, whereas \captionof in a minipage it's a good life saver when you cannot use a float, like using the multicol package.

Fran
  • 80,769
  • 1
    Note that \captionof should usually be placed inside a group. – Werner Dec 11 '19 at 11:59
  • @Werner But nothing prevent you to place it outside of any group, just as showed in the question, while you cannot use a \caption outside a float. For me is also an argument against the routine use of \cantionof and to use it only when really needed. – Fran Dec 11 '19 at 12:07
  • 1
    the spacing of \captionof is the same as that of \caption the bad spacing in your second case is because you have \captionof after the center environment, center there is adding the vertical space to offset the figure from the text just as figure does, so the caption should be inside that environment (better also in a minipage to avoid page break before the caption) – David Carlisle Dec 12 '19 at 01:18
  • @DavidCarlisle I know, but there are only a warning about this in the log ("\captionof outside box or environment on input line 21"). It is not a error preventing doing it as a \caption after a figure, that produce a "LaTeX Error " (aka "sorry, no pdf, hold on and learn"). Notice that the questioner saw no any disadvantage doing in this way. How many people will see the warning, if the PDF is produced and look (more or less) correct? – Fran Dec 12 '19 at 07:35
  • @Fran yes but still why put wrong code in an answer? There are choices to be made but not between correct code and incorrect code. The two downsides that you list in this answer are both downsides of markup error in the document shown not downsides of using a non-float with a captionof caption. – David Carlisle Dec 12 '19 at 08:02
  • @DavidCarlisle Oh, I see. OK, now I don't have time, but I will edit the answer soon to show also the road and not only the stones of the road. – Fran Dec 12 '19 at 09:10