1

I was writing an article and try to insert an image with a caption. So first I used the following codes,

\flashright
\includegraphics[scale=0.25]{cantor.png}

which results in

enter image description here

Then I wanted to add a caption below the image, so I used the following codes

\begin{figure}
\flushright
\includegraphics[scale=0.25]{cantor.png}
\caption{cantor set}
\end{figure}

which gave me,

enter image description here

So my question is how can I fix this large gap and make the things look like the first image with the caption underneath the image?

  • Have you tried putting the \flushright outside the figure ? – Nasser Jun 29 '16 at 02:49
  • 2
    Welcome! Please complete your code so we have a complete minimal example rather than a mere fragment. It is not really possible to help you without this. However, @Nasser 's suggestion will certainly not do what you want ;). – cfr Jun 29 '16 at 02:54
  • 1
    That said, you do not really have that gap. When you write some more text, the gap will fill up with the content. The figure is a float and it is floating to the bottom of the page. If you don't want a float, don't use a float environment i.e. don't use figure. Use \captionof from the caption or capt-of packages if you need a caption. – cfr Jun 29 '16 at 02:56
  • @cfr I was guessing. no MWE so can't try and I am not going to go make a MWE just to try ;) – Nasser Jun 29 '16 at 03:09

1 Answers1

3

i assume you only used figure for the caption, not to float the image. The \captionof macro can be used pretty much anywhere.

The \caption or \captionof macro starts and ends with \par, so it has to go into a \parbox or minipage. Also, the caption isn't just centered; it is centered in a space \textwidth wide (like \makebox[\textwidth][c]{...}), so you need to use a minipage to reduce \textwidth (\parbox won't work)

In this case I made the minipage the same width as the image.

OTOH, if this is for an article you probably want something like wrapfig.

\documentclass{article}
\usepackage{graphicx}
\usepackage{capt-of}% or caption
\usepackage{showframe}% debugging tool, outlines text area

\newsavebox{\tempbox}

\begin{document}

\savebox{\tempbox}{\includegraphics[scale=0.25]{example-image}}
\flushright\begin{minipage}{\wd\tempbox}
\usebox{\tempbox}
\captionof{figure}{cantor set}
\end{minipage}

\end{document}
John Kormylo
  • 79,712
  • 3
  • 50
  • 120
  • \usepackage{mwe} is entirely superfluous ;). Although the code answers the question, the explanation really doesnt :-). Unless I've completely misunderstood the explanation, the question or both. – cfr Jun 29 '16 at 03:17
  • What I mean is that you then find people with \usepackage{mwe} in their real documents. Usually as well as graphicx of course. \usepackage{graphicx} is much more transparent. – cfr Jun 29 '16 at 03:20
  • @cfr - Some people load every package they have in every document, but I see your point. Should I also lose showframe? – John Kormylo Jun 29 '16 at 03:22
  • 1
    For the record, if you are writing "It's easier to show than explain." then you are giving a lousy answer. – Matsmath Jun 29 '16 at 03:45
  • @Mstdmsyh - In terms of time and effort, it WAS easier to show than explain. But thanks for the reminder. – John Kormylo Jun 29 '16 at 14:34
  • I'd at least add a comment saying what showframe does and that it should be commented out in the document. – cfr Jun 29 '16 at 15:08