36

Below is an excerpt from my thesis, showing a figure with a caption.

I'd like to give the figure a title too, that is, a few words written above the figure.

Can I do that in latex? If so, how?

enter image description here

\begin{figure}
\includegraphics[width=\linewidth]{./graphics/chapter6/mouse.pdf}
\caption{Blablabla}
\label{fig:length_eight_mouse}
\end{figure}
3
  • Did you create the plot with matplotlib? Then you could also add a title there, though the font wouldn't match the document font. Aug 14, 2013 at 11:42
  • Yes, that's why I did not include one there. The fonts did not match and it all became so tacky. Aug 14, 2013 at 11:44
  • The fonts don't match on your axis scales either (and presumably your axis labels) - your are labelling your axes I assume? I've just been testing an alternative approach for my thesis - see my answer below.
    – Chris H
    Aug 15, 2013 at 11:51

4 Answers 4

37

The {figure} environment isn't limited to contain only figures etc. You can add anything so just type in your title above the figure.

manually

\begin{figure}
    \centering
    \textbf{Your title}\par\medskip
    \includegraphics[scale=0.3]{example-image}
    \caption{Your caption}
\end{figure}

You can define a new command like \figuretitle to make the formatting and spacing consistent.

\newcommand*{\figuretitle}[1]{%
    {\centering%   <--------  will only affect the title because of the grouping (by the
    \textbf{#1}%              braces before \centering and behind \medskip). If you remove
    \par\medskip}%            these braces the whole body of a {figure} env will be centered.
}

Looks the same as above, but can be used as

\begin{figure}
    \centering
    \figuretitle{Your title}
    \includegraphics[scale=0.3]{example-image}
    \caption{Your caption}
\end{figure}

Or you can use the \caption above the figure. In this case you may use the caption package to adjust the spacing.

caption above

\documentclass{article}

\usepackage{graphicx}

\usepackage{caption}
\captionsetup[figure]{
    position=above,
}

\begin{document}
\begin{figure}
    \centering
    \caption{Your caption}
    \includegraphics[scale=0.3]{example-image}
\end{figure}
\end{document}
4
  • Thanks. Ps. for other newbs: \newcommands should be inserted in your config-file to get it to work in every chapter. In my case this is classicthesis-config.tex. Aug 14, 2013 at 11:54
  • 2
    This is not wrong but neither correct ;-) You can place the definition anywhere in sour document and use it frome there on. It is common sense to put definition in the preamble, i.e. the part before `\begin{document}, and the definition is scoped by a group, e.g. An environment ...
    – Tobi
    Aug 14, 2013 at 13:06
  • Thanks. Right. Classicthesis has even included a space in the preamble for this. Ps. I guess the \centering does not need to be included when you use the \figuretitle? Aug 15, 2013 at 7:03
  • 1
    @TheUnfunCat: Yes the \centering is kind of redundant in this case, but I used a group, i.e. a pair of braces {}, so \centering will only affect the title and not the image. If you want to center all images I’d do it globally, see tex.stackexchange.com/q/6033/4918.
    – Tobi
    Aug 15, 2013 at 10:47
4

You could try making the matplotlib text look like LaTeX:

%matplotlib inline
from matplotlib import pyplot as plt

# This is the first important line:
from matplotlib import rcParams

plt.plot([1, 2, 3, 4], 
         [2, 4, 6, 8])

# These are the second and third important lines:
plt.rc('text', usetex=True)
plt.rc('font', family='serif')

# Change the fontsize to match your document.
plt.xlabel("This is a cool label.", fontsize=12)
plt.ylabel("This is another cool label.", fontsize=12)
plt.savefig("image.pdf", dpi=200)

LaTeX Style Fonts from matplotlib!

2
  • I found these two web pages very useful to produce the plots of my thesis with Matplotlib: wiki.scipy.org/Cookbook/Matplotlib/LaTeX_Examples and wiki.scipy.org/Cookbook/Matplotlib/UsingTex . However I would not do it again; instead I would let LaTeX produce the plots itself (see ticz package for instance). Advantages: (1) actually looks like LaTeX; (2) fix typos, resize and restyle without having to rerun your 4-years-old python code and (3) no need to deal with Matplotlib's maddening API and documentation.
    – Niriel
    May 22, 2015 at 11:04
  • Just want to add for anyone considering this, matplotlib is very good for making titles but if you need different colours in your title (I need coloured numbers that correspond to colours in my plot), do not use matplotlib. It is quite painful and inconsistent, hence why I am here to do in within latex.
    – JamesT
    Oct 17, 2022 at 12:10
3

An alternative method is to output from Matplotlib as .svg, with or without a title then read in to Inkscape. You can save from Inkscape as .pdf+.tex (or .eps+.tex I think), where the .pdf(.eps) contains the graphics, and the .tex overlays the text, in your current document font. All this can be done from the command line - Inkscape supports that.

A couple of links:

Setting up matplotlib to output text as text

Adjusting sizing of figure and text

0

I used the \tabular-environment.

This is useful for multiple figures with different (centered) titles especially.

\begin{figure}
\centering
    \begin{tabular}{c c}
        \textbf{\underline{Title One}} & \textbf{\underline{Title Two}} \\
        \includegraphics{picture1.png} & \includegraphics{picture2.png}
    \end{tabular}
\caption{Caption Zero}
\end{figure}

enter image description here

1
  • Welcome to TeX.SE! Can you please make your code snippet compilable and add an screenshot of your result to your answer?
    – Mensch
    Jun 24, 2019 at 8:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .