LaTeX forum ⇒ Graphics, Figures & TablesCaptions not centered Topic is solved

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

Captions not centered

Postby norox » Fri Aug 30, 2013 10:38 am

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:

  1. \begin{figure}[]
  2. \centering
  3. \includegraphics[scale=0.45]{mm,m1,mb,iff}
  4. \caption{Interesting rates during the crisis \\ \textit{Source: authors graph based on FRED data}}
  5. \label{mmm1iff}
  6. \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.

Tags:

User avatar
localghost
Site Moderator
Posts: 9204
Joined: Fri Feb 02, 2007 12:06 pm

Postby localghost » Fri Aug 30, 2013 10:45 am

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.
  1. \documentclass[11pt]{article}
  2. \usepackage[T1]{fontenc}
  3. \usepackage[demo]{graphicx} % delete `demo` option in actual document
  4. \usepackage{caption}
  5.  
  6. \begin{document}
  7. \begin{figure}[!ht]
  8. \centering
  9. \includegraphics[scale=0.45]{mmm1mbiff}
  10. \caption{Interesting rates during the crisis}
  11. \captionsetup{aboveskip=0pt,font=it}
  12. \caption*{Source: authors graph based on FRED data}
  13. \label{fig:mmm1mbiff}
  14. \end{figure}
  15. \end{document}

Use this method wherever you have to add a source to a figure.


Best regards and welcome to the board
Thorsten
LaTeX Community Moderator
How to make a "Minimal Example"
Board Rules
Avoidable Mistakes


¹ System: openSUSE 42.2 (Linux 4.4.52), TeX Live 2016 (vanilla), TeXworks 0.6.1

norox
Posts: 3
Joined: Fri Aug 30, 2013 10:34 am

Postby norox » Fri Aug 30, 2013 2:03 pm

thx problem solved!

User avatar
cgnieder
Site Moderator
Posts: 1988
Joined: Sat Apr 16, 2011 7:27 pm

Postby cgnieder » Fri Aug 30, 2013 4:42 pm

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

  1. \documentclass[11pt]{article}
  2. \usepackage[T1]{fontenc}
  3. \usepackage[demo]{graphicx} % delete `demo` option in actual document
  4.  
  5. \begin{document}
  6. \begin{figure}[!ht]
  7. \centering
  8. \includegraphics[scale=0.45]{mmm1mbiff}
  9. \caption{short}
  10. \label{fig:short}
  11. \end{figure}
  12. \begin{figure}[!ht]
  13. \centering
  14. \includegraphics[scale=0.45]{mmm1mbiff}
  15. \caption{this one is so long that it does not fit into a single line any more so it spans two lines}
  16. \label{fig:long}
  17. \end{figure}
  18. \end{document}


What's more: line breaks are not natively supported:
  1. \documentclass[11pt]{article}
  2. \usepackage[T1]{fontenc}
  3. \usepackage[demo]{graphicx} % delete `demo` option in actual document
  4.  
  5. \begin{document}
  6. \begin{figure}[!ht]
  7. \centering
  8. \includegraphics[scale=0.45]{mmm1mbiff}
  9. \caption{I want this to\\ be two lines}
  10. \label{fig:error}
  11. \end{figure}
  12. \begin{figure}[!ht]
  13. \centering
  14. \includegraphics[scale=0.45]{mmm1mbiff}
  15. \caption{I want this to\newline be two lines}
  16. \label{fig:vanish}
  17. \end{figure}
  18. \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:

  1. \documentclass[11pt]{article}
  2. \usepackage[T1]{fontenc}
  3. \usepackage[demo]{graphicx} % delete `demo` option in actual document
  4. \usepackage{caption}
  5.  
  6. \begin{document}
  7. \begin{figure}[!ht]
  8. \centering
  9. \includegraphics[scale=0.45]{mmm1mbiff}
  10. \caption{I want this to\\ be two lines}
  11. \label{fig:works}
  12. \end{figure}
  13. \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:

  1. \documentclass[11pt]{article}
  2. \usepackage[T1]{fontenc}
  3. \usepackage[demo]{graphicx} % delete `demo` option in actual document
  4. \usepackage{caption}
  5. \captionsetup{justification=centering}
  6. \newcommand*\picsource[1]{\textit{#1}}
  7. \begin{document}
  8. \begin{figure}[]
  9. \centering
  10. \includegraphics[scale=0.45]{mm,m1,mb,iff}
  11. \caption{%
  12. Interesting rates during the crisis \\
  13. \picsource{Source: authors graph based on FRED data}}
  14. \label{mmm1iff}
  15. \end{figure}
  16. \end{document}


In this code I also replaced the \textit with a semantically more meaningful command.

Regards
Clemens
------------------------------
chemmacros · chemformula · leadsheets · xsim


Return to “Graphics, Figures & Tables”

Who is online

Users browsing this forum: No registered users and 7 guests