Graphics, Figures & TablesCaptions not centered

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
norox
Posts: 3
Joined: Fri Aug 30, 2013 10:34 am

Captions not centered

Post by norox »

Dear TeX Community,

I am having difficulties getting my captions centered underneath my graphs. I understand captions should be centered automatically. The funny thing is, if the caption is short enough it gets centered. However for example the following one does not:

Code: Select all

\begin{figure}[]
\centering
\includegraphics[scale=0.45]{mm,m1,mb,iff}
\caption{Interesting rates during the crisis \\ \textit{Source: authors graph based on FRED data}}
\label{mmm1iff}
\end{figure}

Can anyone help me fix this?

Best,

norox
Last edited by cgnieder on Fri Aug 30, 2013 10:42 am, edited 1 time in total.

Recommended reading 2024:

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

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

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Captions not centered

Post by localghost »

For an adequate problem description please prepare a self-contained and minimal example in order to avoid speculations and guesswork.

Nevertheless, if I supplement your code snippet to a complete document, I can reproduce the problem. It seems to be better if you use the caption package and typeset the source of your figure as a second caption which is unnumbered.

Code: Select all

\documentclass[11pt]{article}
\usepackage[T1]{fontenc}
\usepackage[demo]{graphicx} % delete `demo` option in actual document
\usepackage{caption}

\begin{document}
  \begin{figure}[!ht]
    \centering
    \includegraphics[scale=0.45]{mmm1mbiff}
    \caption{Interesting rates during the crisis}
    \captionsetup{aboveskip=0pt,font=it}
    \caption*{Source: authors graph based on FRED data}
    \label{fig:mmm1mbiff}
  \end{figure}
\end{document}
Use this method wherever you have to add a source to a figure.


Best regards and welcome to the board
Thorsten
norox
Posts: 3
Joined: Fri Aug 30, 2013 10:34 am

Re: Captions not centered

Post by norox »

thx problem solved!
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Captions not centered

Post by cgnieder »

Captions are only centered if they are shorter than one line:

Code: Select all

\documentclass[11pt]{article}
\usepackage[T1]{fontenc}
\usepackage[demo]{graphicx} % delete `demo` option in actual document

\begin{document}
  \begin{figure}[!ht]
    \centering
    \includegraphics[scale=0.45]{mmm1mbiff}
    \caption{short}
    \label{fig:short}
  \end{figure}
  \begin{figure}[!ht]
    \centering
    \includegraphics[scale=0.45]{mmm1mbiff}
    \caption{this one is so long that it does not fit into a single line any more so it spans two lines}
    \label{fig:long}
  \end{figure}
\end{document}
What's more: line breaks are not natively supported:

Code: Select all

\documentclass[11pt]{article}
\usepackage[T1]{fontenc}
\usepackage[demo]{graphicx} % delete `demo` option in actual document

\begin{document}
  \begin{figure}[!ht]
    \centering
    \includegraphics[scale=0.45]{mmm1mbiff}
    \caption{I want this to\\ be two lines}
    \label{fig:error}
  \end{figure}
  \begin{figure}[!ht]
    \centering
    \includegraphics[scale=0.45]{mmm1mbiff}
    \caption{I want this to\newline be two lines}
    \label{fig:vanish}
  \end{figure}
\end{document}
However, the caption package supports line breaks in captions. If the complete caption is short enough (and the option singlelinecheck=on is active which is the default) they even are centered without any further setup as long as you use \\ and not \newline:

Code: Select all

\documentclass[11pt]{article}
\usepackage[T1]{fontenc}
\usepackage[demo]{graphicx} % delete `demo` option in actual document
\usepackage{caption}

\begin{document}
  \begin{figure}[!ht]
    \centering
    \includegraphics[scale=0.45]{mmm1mbiff}
    \caption{I want this to\\ be two lines}
    \label{fig:works}
  \end{figure}
\end{document}
In your original code this does not work, because the caption is too long and the lines are left-aligned. However, caption has a solution for this, too: \captionsetup{justification=centering}. You can use this for all captions by placing it in the preamble or for one caption only by placing it in the {figure} environment where you want this:

Code: Select all

\documentclass[11pt]{article}
\usepackage[T1]{fontenc}
\usepackage[demo]{graphicx} % delete `demo` option in actual document
\usepackage{caption}
\captionsetup{justification=centering}
\newcommand*\picsource[1]{\textit{#1}}
\begin{document}
    \begin{figure}[]
    \centering
    \includegraphics[scale=0.45]{mm,m1,mb,iff}
    \caption{%
      Interesting rates during the crisis \\
      \picsource{Source: authors graph based on FRED data}}
    \label{mmm1iff}
    \end{figure}
\end{document}
In this code I also replaced the \textit with a semantically more meaningful command.

Regards
site moderator & package author
Post Reply