Graphics, Figures & TablesCaption?

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
the_obs
Posts: 9
Joined: Mon Oct 18, 2010 9:18 pm

Caption?

Post by the_obs »

Hey all,
How can I change default caption behaviour for captions to be centered RELATIVE TO THEIR IMAGE instead of relative to page (see picture)?


Edit by localghost: Preferably no external links (see Board Rules). Attachments go onto the forum server where possible.
Attachments
latexcaption.png
latexcaption.png (478.88 KiB) Viewed 3447 times

Recommended reading 2024:

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

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

gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

Caption?

Post by gmedina »

Hi,

one possible solution would be to use a minipage; of course, now you can't use the floating environment figure, but you can still give a caption to your figure by using the \captionof command provided by the caption package. Alittle example (I used the demo option for the graphicx package just to make my example compilable for everyone; don't use this option in your actual code):

Code: Select all

\documentclass{article}
\usepackage[demo]{graphicx}
\usepackage{caption}

\begin{document}

\noindent\begin{minipage}{7cm}
  \includegraphics[width=7cm]{name}
  \captionof{figure}{Octopus}
  \label{fig:test1}
\end{minipage}

\end{document}
1,1,2,3,5,8,13,21,34,55,89,144,233,...
meho_r
Posts: 823
Joined: Tue Aug 07, 2007 5:28 pm

Caption?

Post by meho_r »

...and another solution may be floatrow package, e.g.:

Code: Select all

\documentclass{book}
\usepackage{lipsum}
\usepackage[demo]{graphicx}
\usepackage{xcolor}

\usepackage{caption}
\captionsetup[figure]{justification=centering}% caption centered

\usepackage{floatrow}
\floatsetup[figure]{margins=raggedright}% figure placed on left

\begin{document}

\chapter{A chapter}

\lipsum[1]

\begin{figure}[!h]
\ffigbox[\FBwidth]
{\color{blue}\includegraphics[width=6cm,height=1cm]{test}}
{\caption{A caption when \textbackslash ffigbox and \textbackslash FBwidth are used (blue)}}
\end{figure}

\begin{figure}[!h]
{\color{red}\includegraphics[width=6cm,height=1cm]{test}}
\caption{A caption without \textbackslash ffigbox and \textbackslash FBwidth (red)}
\end{figure}

\end{document}
the_obs
Posts: 9
Joined: Mon Oct 18, 2010 9:18 pm

Re: Caption?

Post by the_obs »

So standard behaviour in Latex documents for inserting graphics is centering the graphics, so as to have the caption neatly centered underneath?
User avatar
gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

Re: Caption?

Post by gmedina »

No. Standard behaviour is to center captions spanning only one line.
1,1,2,3,5,8,13,21,34,55,89,144,233,...
the_obs
Posts: 9
Joined: Mon Oct 18, 2010 9:18 pm

Re: Caption?

Post by the_obs »

What?
My caption spans only one line, but isn't centered (by default) with the overlying image, hence my original problem and post.
User avatar
gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

Caption?

Post by gmedina »

In standard LaTeX, captions spanning only one line are centered with respect to the page not with respect to the image. Your image was not centered (since you didn't use any centering mechanism for the image), and that caused your problem.

I misunderstood this question:
the_obs wrote:So standard behaviour in Latex documents for inserting graphics is centering the graphics, so as to have the caption neatly centered underneath?
and thought (erroneously) that you meant that images automatically got centered by LaTeX when included. Now that I reread the question, I noticed the misunderstanding. Sorry for that.
1,1,2,3,5,8,13,21,34,55,89,144,233,...
Post Reply