2

I currently have a figure that contains two subfigures. I want to add labels underneath each subfigure but I'm having trouble aligning them. The first subcaption seems to center from the perspective of both subfigures (i.e., the entire page) which leads to the second subcaption starting from what I assume is a new line. More specifically, the code that I have is:

\documentclass[10pt,journal,compsoc]{IEEEtran}

% *** SUBFIGURE PACKAGES ***
\ifCLASSOPTIONcompsoc
 \usepackage[caption=false,font=footnotesize,labelfont=sf,textfont=sf]{subfig}
\else
 \usepackage[caption=false,font=footnotesize]{subfig}
\fi
% subfig.sty, written by Steven Douglas Cochran, is the modern replacement
% for subfigure.sty, the latter of which is no longer maintained and is
% incompatible with some LaTeX packages including fixltx2e. However,
% subfig.sty requires and automatically loads Axel Sommerfeldt's caption.sty
% which will override IEEEtran.cls' handling of captions and this will result
% in non-IEEE style figure/table captions. To prevent this problem, be sure
% and invoke subfig.sty's "caption=false" package option (available since
% subfig.sty version 1.3, 2005/06/28) as this is will preserve IEEEtran.cls
% handling of captions.
% Note that the Computer Society format requires a sans serif font rather
% than the serif font used in traditional IEEE formatting and thus the need
% to invoke different subfig.sty package options depending on whether
% compsoc mode has been enabled.
%
% The latest version and documentation of subfig.sty can be obtained at:
% http://www.ctan.org/pkg/subfig

\usepackage{caption}
\usepackage{subcaption}


\begin{figure*}[!t]
    \begin{subfigure}
        \centering
        \includegraphics[trim={0, 0, 0, 0}, width=\columnwidth]{figures/plot1.pdf}
        \subcaption{Thing}
    \end{subfigure}
    \begin{subfigure}
        \centering
        \includegraphics[trim={0, 0, 0, 0}, width=\columnwidth]{figures/plot2.pdf}
        \subcaption{Thing}
    \end{subfigure}
\caption{Something.}
\label{fig:attention_score}
\end{figure*}

which leads to this: enter image description here

I've read from this answer that the subfigure takes an option for a subcaption anchor, and I accordingly added a [c] to the right of each \begin{subfigure}. That leads to this: enter image description here

Neither image is what I want. How should I configure things so that each subcaption is centered directly beneath each subfigure? Thanks.

2
  • please see if the answer below resolves the problem – js bibra Mar 7 at 7:26
  • The t, c, and b "anchors" affect the vertical, not the horizontal, alignment of the subfigure. – Mico Mar 7 at 7:55
3

Your MWE loads the subfig as well as the subcaption package resulting in error messages such as:

! Package subcaption Error: This package can't be used in cooperation with the subfig package.

To overcome this, remove the subcaption package and use \subfloat instead of the subfigure environment, as shown in the following example:

enter image description here

\documentclass[10pt,journal,compsoc]{IEEEtran}

% *** SUBFIGURE PACKAGES ***
\ifCLASSOPTIONcompsoc
 \usepackage[caption=false,font=footnotesize,labelfont=sf,textfont=sf]{subfig}
\else
 \usepackage[caption=false,font=footnotesize]{subfig}
\fi

\usepackage[demo]{graphicx}% remove the demo option in your actual document.

\begin{document}


\begin{figure*}[!t]
\subfloat[Thing.]{\includegraphics[width=\columnwidth]{figures/plot1.pdf}}
\hfill
\subfloat[Thing.]{\includegraphics[width=\columnwidth]{figures/plot2.pdf}}
\caption{Something.}
\label{fig:attention_score}
\end{figure*}

\end{document}
5

Your test document must be generating several error messages of the following type -- don't ignore them!

./main.tex:10: Missing number, treated as zero.
<to be read again> 
\protect 
l.10         \centering
                     
? 

These messages are generated because of a syntax error in your code: you failed to provide the mandatory width argument of the subfigure environments.

How to fix this? Instead of just \begin{subfigure}, you should write \begin{subfigure}{\columnwidth}.

Once these syntax errors are fixed, the test document compiles without further ado. Just for extra code hygiene, though, I'd remove (or comment out) the two redundant \centering instructions and insert an \hfill directive between the two subfigure environments in order to maximize their horizontal separation.

enter image description here

\documentclass[twocolumn]{article}
\usepackage{subcaption}
\usepackage[demo]{graphicx} % remove 'demo' option in real document

\begin{document}

\begin{figure*}
    \begin{subfigure}{\columnwidth} % <-- added '{\columnwidth}'
        %\centering % redundant
        \includegraphics[trim={0, 0, 0, 0}, width=\columnwidth]{figures/plot1.pdf}
        \subcaption{Thing 1}
    \end{subfigure}%
    \hfill % <-- maximize horizontal separation (subject to text block width constraint)
    \begin{subfigure}{\columnwidth} % <-- added '{\columnwidth}'
        %\centering % redundant
        \includegraphics[trim={0, 0, 0, 0}, width=\columnwidth]{figures/plot2.pdf}
        \subcaption{Thing 2}
    \end{subfigure}
\caption{Something.}
\label{fig:attention_score}
\end{figure*}

\end{document}
5
  • Thanks for the answer. Unfortunately, the error persists. I'm not getting any of the error messages that you mentioned, but I do have a whole bunch of errors that are either "Environment subfigure undefined." or "Illegal unit of measure (pt inserted)." or "\begin{figure*} on input line X ended by \end{subfigure}." All of these are a little perplexing to be because there doesn't seem to be any error when I check them... – Seankala Mar 7 at 8:57
  • I edited in some more details with what I believe may be relevant to the problem from the LaTeX template. – Seankala Mar 7 at 8:59
  • 1
    @Seankala - It would have been really, really helpful if you had mentioned up front that you employ the IEEEtran document class, since its caption setttings are not fully compatible with those of the subcaption package -- which you said you were using. I will have to rewrite my answer completely in light of this highly pertinent additional information. Actually, I just noticed that user @leandriis just posted an answer that employs the subfig package properly. Hence, I won't bother revising my initial answer. – Mico Mar 7 at 9:17
  • Thanks, and sorry for the slip up. I wasn't aware that IEEEtran had such characteristics. I'll keep this answer in mind for future reference. – Seankala Mar 7 at 9:21
  • @Seankala - My initial piece of advice still stands: Never, ever ignore error messages. – Mico Mar 7 at 9:22
2

enter image description here

\documentclass[12pt]{report}
\usepackage[letterpaper,top=1in,bottom=1in,right=1in,left=1in]{geometry}
    \usepackage[demo]{graphicx}
\usepackage{subcaption}
    \begin{document}
        \begin{figure}[h]
                    \begin{subfigure}[t]{0.45\textwidth}
                    \includegraphics[trim={0, 0, 0, 0}, width=\textwidth]{gfx-1}
                    \caption{Caption 1}
                \end{subfigure}
                \hfill
                    \begin{subfigure}[t]{0.45\textwidth}
                        \includegraphics[trim={0, 0, 0, 0}, width=\textwidth]{gfx-1}
                        \caption{Caption 2}
                    \end{subfigure}
        \caption{center caption}
        \end{figure}
    \end{document}
1
  • 1
    thanks for the guidance --edited the code above -- the centering is rightly redundant – js bibra Mar 7 at 8:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.