7

How do I customize my caption that follows this article's setting of the captions on page 26. The following is my attempt to do that, but still doesn't work. Obviously, what I want to achieve is to put the figures' names in small letter/alphabet((a) and (b)) for each of the pictures and there is another global caption below them which is in number/figure. The provided link on the page 26 expresses this better.

\begin{figure}[!ht]
 \begin{minipage}{0.49\linewidth}
  \centerline{\includegraphics[scale=0.2]{figure/figA.png}}
  \caption{Figure on left side}\label{fig:figA}
 \end{minipage}
\hfill
 \begin{minipage}{0.49\linewidth}
  \centerline{\includegraphics[scale=0.2]{figure/figB.png}}
  \caption{Figure on right side}\label{fig:figB}
 \end{minipage}
\end{figure}

Or, am I using the wrong package?

lockstep
  • 250,273
alfa_80
  • 519

3 Answers3

12

You can solve your problem by loading the subcaption package. (The subcaption package is incompatible with packages that provide similar functionality, such as subfigure and subfloat. If you use subcaption, be sure not to load one of these competing packages as well.) The subcaption package provides the environments subfigure and subtable, which take as their argument the desired width of the sub-figure/table in question. In the Minimum Working Example (MWE) below, I've set this width to 0.49\linewidth, as you do in your example code. The MWE also illustrates how many may create cross-references to the entire float as well as to one or more of its components.

Note the use of the \centering commands inside the subfigure environments; it's preferable to use this command rather than \centerline. In addition, since I don't have access to the .png files you list in your code, I've loaded the graphicx package with the demo option -- this will create black "dummy" rectangles. In your "real" code, you should of course omit the demo option.

\documentclass{article}
\usepackage[margin=1in]{geometry}
\usepackage[demo]{graphicx}
\usepackage{subcaption}
\begin{document}
\begin{figure}[t]
\centering
   \begin{subfigure}{0.49\linewidth} \centering
     \includegraphics[scale=0.2]{figure/figA.png}
     \caption{Figure on left side}\label{fig:figA}
   \end{subfigure}
   \begin{subfigure}{0.49\linewidth} \centering
     \includegraphics[scale=0.2]{figure/figB.png}
     \caption{Figure on right side}\label{fig:figB}
   \end{subfigure}
\caption{Overall caption} \label{fig:twofigs}
\end{figure}

\noindent
Some cross-references: First, we refer to Figure~\ref{fig:twofigs}. 
Second, we can also refer to the component figures individually, 
viz., to Figures~\ref{fig:figA} and \ref{fig:figB}.
\end{document}

enter image description here

Addendum For more information on what the parameters \linewidth and \textwidth do, see this answer to a question that was posed some time ago, viz., Difference between \textwidth, \linewidth and \hsize.

Mico
  • 506,678
  • Unfortunately, I received this error: Use of @subfloat doesn't match its definition \begin{subfigure}{ – alfa_80 Feb 27 '12 at 15:42
  • It now works out of the box after disabling a package called "subfigure"..They might clash before..Thanks a lot anyway. – alfa_80 Feb 27 '12 at 15:48
  • The subcaption and subfigure packages are NOT compatible. (You hadn't mentioned in your question that you were loading the subfigure package.) I will note this conflict in the text above. – Mico Feb 27 '12 at 15:51
  • One last question perhaps, what is \linewidth parameter for? Is it for the width of each of sub-image? – alfa_80 Feb 27 '12 at 16:03
  • OK, I'll add some information about the parameter \linewidth to the answer. – Mico Feb 27 '12 at 16:34
  • Rather than using .49\linewidth you can use .5\linewidth provided that you put a % after the first \end{subfigure} – cmhughes Feb 28 '12 at 00:05
  • @Mico: How do I fix my problem of having the second figure's caption is not aligned with the first one? This happens when I change different figure.. – alfa_80 Feb 28 '12 at 13:47
  • @Shah: I suggest you post a new question to raise this follow-up concern. You may want to explain in which sense(s) the two subfigures don't line up with each other. E.g., do they differ in their heights, their widths, or both? – Mico Feb 28 '12 at 13:51
  • @Mico:Thanks a lot for your suggestion. But I think, it's far much easier to manipulate the images to be at the same size using GIMP, or the like than tweaking the latex..Thanks a lot anyway. – alfa_80 Feb 28 '12 at 18:37
  • @Mico, Hi, this worked nicely. But the captions are aligned. I want to place the captions along same the horizontal level below the figures. Any help – learner Aug 26 '21 at 13:39
  • @M.A.SARKAR - Please clarify what you mean by "But the captions are aligned." – Mico Aug 26 '21 at 18:19
  • @Mico, I mean the captions (a) Figure on left side and (b) Figure on right side are not aligned along same horizontal level. One is displaying up while another is displaying little bit down – learner Aug 26 '21 at 18:27
  • @M.A.SARKAR - Have you tried replacing both instances of \begin{subfigure}{0.49\linewidth} with \begin{subfigure}[b]{0.49\linewidth}? – Mico Aug 26 '21 at 18:32
  • @Mico, thanks. It worked. Now I want to remove the overall caption, instead I want Figure numbering replacing (a) and (b) on the subcaptions. How to do it ? – learner Aug 26 '21 at 18:40
  • @M.A.SARKAR - I'm afraid I've lost track of your setup. Please post a new query in which you provide all oertinent information. – Mico Aug 26 '21 at 18:50
  • @Mico, It is alright. Thank you very much so far helping – learner Aug 26 '21 at 18:53
2

For putting figures side by side you can try this one. (captions and everything are handled automatically and you may use more or less number of figures: 2, 3, 4, etc.):

\usepackage{graphicx}
\usepackage{subfigure}
\begin{document}

\begin{figure}[ht]
\centering
\subfigure[Subfigure 1 title]{
    \rule{2.8cm}{2cm}
    \label{fig:subfig1}
}
\subfigure[Subfigure 2 title]{
    \rule{2.8cm}{2cm}
\label{fig:subfig2}
}
\subfigure[Subfigure 3 title]{
    \rule{2.8cm}{2cm}
    \label{fig:subfig3}
}
\subfigure[Subfigure 4 title]{
    \rule{2.8cm}{2cm}
    \label{fig:subfig4}
}
\caption[Optional caption for list of figures]{General Caption of subfigures 1-4}
\label{fig:subfigureExample1}
\end{figure}

Reference to figure1:
    \ref{fig:subfigureExample1} 
and 
    \ref{fig:subfig4}.

\end{document}

enter image description here

And the referencing:

enter image description here

Or this one (you have to provide images subfig5.jpg to subfig8.jpg):

\usepackage{graphicx}
\usepackage{subfigure}
\begin{document}
\begin{figure}[ht]
\centering
\subfigure[Subfigure 5 title]{
    \includegraphics[width=0.4\columnwidth, keepaspectratio]{subfig5.jpg}
    \label{fig:subfig5}
}
\subfigure[Subfigure 6 title]{
    \includegraphics[width=0.4\columnwidth, keepaspectratio]{subfig6.jpg}       
    \label{fig:subfig6}
}
\subfigure[Subfigure 7 title]{
    \includegraphics[width=0.4\columnwidth, keepaspectratio]{subfig7.jpg}    
    \label{fig:subfig7}
}
\subfigure[Subfigure 8 title]{
    \includegraphics[width=0.4\columnwidth, keepaspectratio]{subfig8.jpg}    
    \label{fig:subfig8}
}
\caption[Optional caption for list of figures 5-8]{General Caption of subfigures 5-8}
\label{fig:subfigureExample2}
\end{figure}

Reference to figure:
    \ref{fig:subfigureExample2} 
and 
    \ref{fig:subfig6}.

\end{document}

enter image description here

And the referencing:

enter image description here

Hint: "Subfigure" package may be old, but does anything automatically. I tried several options and found it the most useful one.

Alisa
  • 363
2

What you need is a subfloat. Take a look here