4

I am doing which should be a simple task, but I cannot fathom why it does not work... I just want a simple caption of a figure I centered, aligned to the left of the figure, right down its lower left corner.

This is my MWE:

\documentclass{beamer}

\mode<presentation> {
    \usepackage[labelformat=empty,
    font=scriptsize,
    skip=0pt,
    justification=justified,
    singlelinecheck=false]{caption}
}

\begin{document}

\begin{frame}
    \begin{center}
    \includegraphics[width=.5\linewidth]{example-image}
    \captionof{figure}{my caption here}
    \end{center}
\end{frame}

\end{document}

Which produces:

fig1

How can I have the caption right below the bottom left corner of my image? And why does the code above not work?

Please note the solution should also work within a \figure environment to add captions to tikzpictures...

Thanks!

EDIT: I TRIED justification=justified,singlelinecheck=false AS SUGGESTED HERE, BUT STILL NO SUCCESS...

\documentclass{beamer}

\usepackage[labelformat=empty,font=scriptsize,skip=0pt,
justification=raggedright,singlelinecheck=false]{caption}

\begin{document}

\begin{frame}
    \begin{center}
    \includegraphics[width=.5\linewidth]{example-image}
    \captionof{figure}{my caption here}
    \end{center}
\end{frame}

\end{document}

b

DaniCee
  • 2,217

3 Answers3

5

You can use package threeparttable to restrict the caption width to the width of the image:

\documentclass{beamer}

\usepackage{threeparttable}
\usepackage[labelformat=empty,font=scriptsize,skip=0pt,
justification=raggedright,singlelinecheck=false]{caption}

\begin{document}

\begin{frame}
    \begin{figure}
      %\centering% not needed, because default
      \begin{measuredfigure}
        \includegraphics[width=.5\linewidth]{example-image}
        \caption{my caption here}
      \end{measuredfigure}
    \end{figure}
\end{frame}

\end{document}

caption below image restricted to image width

Usually you do not need explicit center or \centering, because beamer centers the figure by default (see the result in the image above). Instead, if you want left aligned or right aligned figures you have to add \raggedright or \raggedleft just after \begin{figure}. Nevertheless, you can activate the commented \centering.

Schweinebacke
  • 26,336
  • This is it! Isn't the use of "measuredfigure" and "figure" redundant though? Only with "measuredfigure" I get the same result – DaniCee Jan 29 '17 at 11:34
  • @DaniCee Without figure the image wouldn't be centered by default. So you would need an additional environment to center it. – Schweinebacke Jan 29 '17 at 11:37
  • what about without "figure" but with "centering"? – DaniCee Jan 29 '17 at 11:44
  • @DaniCee \centering would center the whole frame not only the picture. center could be used, but why not using figure? center would be physical markup, figure is semantic markup. Semantic markup should be preferred, because you can change all figures at once in the preamble of the document. It would make more sense to redefine figure to be a centered measuredfigure than to put all measuredfigure into a center-environment. But I do not recommend to do so, because if you would have a very small image, a measuredfigure could be very ugly. – Schweinebacke Jan 29 '17 at 11:48
  • for the win: How would you align the caption to the right side instead? I tried with \raggedleft before the caption, but nothing happened... – DaniCee Feb 01 '17 at 11:44
  • @DaniCee justification=raggedleft instead of justification=raggedright. – Schweinebacke Feb 01 '17 at 12:48
  • oh right, but I mean locally; I want all my captions on the left, except one I want on the right – DaniCee Feb 01 '17 at 13:24
  • You can set the option local to the figure environment using \captionsetup or for all measuredfigure environments using the optional argument of \captionsetup in the document preamble. See the caption manual for more information on how to configure it locally or globally. – Schweinebacke Feb 01 '17 at 14:28
3

Wrapping the image and its caption in an additional minipage could be a solution:

\documentclass{beamer}
\setbeamertemplate{caption}{\insertcaption}

\begin{document}

\begin{frame}
\begin{figure}
\begin{minipage}{.4\textwidth}
\includegraphics[width=\textwidth]{example-image}
\caption{Text text text text text text text text text text text text text text text text text}
\end{minipage}
\end{figure}
\end{frame}

\end{document}

enter image description here

  • But a single line caption would be centered without using package caption. AFAIK the OP want it left aligned with the image. – Schweinebacke Jan 30 '17 at 12:19
0

I think, using a floating object like the \begin{figure} ... \end{figure} or \begin{table} ... \end{table} is in conflict of the needs of a presentation. A floating object is something, which LaTeX may move around to get the best possible word, line and page wrapping. Therefore it may move the image to the end of the chapter. In a presentation, you don't want to jump forth and back between your slides. You normally want a continuous flow from one slide to the next.

Having said that, you don't want to use a floating object in beamer, at least not in the presentation forms.

Having said that, you don't want to use \caption-like commands, as you don't have a figure-environment.

The solution in your case: just remove the \caption-command (and even the caption-package) and it works.

MWE:

\documentclass{beamer}

\begin{document}

\begin{frame}
  \begin{center}
    \includegraphics[width=.5\linewidth]{example-image}\\
    my caption here
  \end{center}
\end{frame}

\end{document}

Result:

enter image description here

Jan
  • 5,293
  • I don't really understand why the use of captions is not recommended in Beamer... this is the first time I read something like this. – DaniCee Jan 29 '17 at 08:46
  • The problem is I have way more complicated layouts than that in the MWE, so in those cases what should I do to replace the captions?? put the figure in a tikzpicture node and the caption as text in another node? that sounds really overly complicating things... – DaniCee Jan 29 '17 at 08:47
  • I don't think floats are the problem here. I can't try right now but: have you tried removing the grouping braces around the \usepackage command? \mode does not take arguments afaik – Nicola Gigante Jan 29 '17 at 09:22
  • Right, it's the singlelinecheck=false argument what causes it... But without it the caption is centered below the image; I'd like the caption to be aligned left with the image... I am editing to reflect this – DaniCee Jan 29 '17 at 09:27
  • 1
    figure and table in beamer are not floating. They are placed where they are used. See section 12.6 in the beamer user manual. – Schweinebacke Jan 29 '17 at 10:37
  • @Schweinebacke: \caption is a command, that belongs to floats. Therefore my comments on floating objects used in presentations. But the OP didn't use the figure-environment in his MWE. – Jan Jan 29 '17 at 11:26
  • @Jan \caption in figure of beamer does not belong to floats, because figure does not float. beamer does not provide floating objects at all. – Schweinebacke Jan 29 '17 at 11:31
  • 2
    @Jan In beamer you can just as well use figure, as it is redefinded to do basically not much more then centering ... – samcarter_is_at_topanswers.xyz Jan 29 '17 at 11:38
  • @Jan: BTW: You answer does not left align the caption below the centered image. – Schweinebacke Jan 29 '17 at 11:54