1

I'm asking if there are any way to use the subcaptionpackage, without load its own style. To be more precise I have a LaTeX template with the relative .sty file that gives me this type of caption style:

enter image description here

If I try to load the subcaption or subfig packages, I totally lost this style.

There is any way to use these packages maintaining the original template style or to enumerate figures with letters like subfig does without these packages? I'm using the IOP latex template available here: Iop Template.

Thank you so much.

4
  • Have you checked how the local .sty-file does this formatting. Perhaps it's already loading a package which enables you to use their definition in your usecase.
    – Skillmon
    Jan 12, 2018 at 14:57
  • Yeah I have checked, but for me not so obvious to find. There are a lot of command that begins with "caption" or "tablecaption"
    – DjGj
    Jan 12, 2018 at 15:14
  • What happens if you load subcaption or subfig prior to your custom sty?
    – Skillmon
    Jan 12, 2018 at 17:36
  • @Skillmon, Happens the same thing, all caption style change. :(
    – DjGj
    Jan 14, 2018 at 12:28

1 Answer 1

2

If you really want to use something like subfig the following gives at least some close behaviour, though it does need a bit more manual work. I hope this works together with your template (I refrain from visiting third party sites upon answering questions on TeX.SX, therefore didn't test with your template)!

\documentclass[]{article}

\usepackage{blindtext}
\usepackage{graphicx}
\usepackage{etoolbox} % to hook into `figure` and reset the mysubfig counter

% reset the counter (it is unlikely that another figure environment has
% additional subfigures of the previous one
\AtBeginEnvironment{figure}{\setcounter{mysubfig}{0}}


\newcounter{mysubfig}[figure]% also reset after the figure-counter has increased
\makeatletter
\newcommand*\mysubfig{\@ifstar\mysubfig@above\mysubfig@below}
\newcommand*\mysubfig@above{%
  \renewcommand*\themysubfig{\thefigure(\alph{mysubfig})}%
  \refstepcounter{mysubfig}(\alph{mysubfig})}
\newcommand*\mysubfig@below{%
  \renewcommand*\themysubfig{\the\numexpr\c@figure+1\relax(\alph{mysubfig})}%
  \refstepcounter{mysubfig}(\alph{mysubfig})}
\makeatother

\begin{document}
You could place the figures into \texttt{minipage}s with individual
\verb|\caption|s:
\begin{figure}[!htbp]
  \centering
  \begin{minipage}[]{0.4\linewidth}
    \centering
    \includegraphics[width=0.9\linewidth]{example-image-a}
    \caption{left picture}
    \label{fig:1l}
  \end{minipage}
  \begin{minipage}[]{0.4\linewidth}
    \centering
    \includegraphics[width=0.9\linewidth]{example-image-b}
    \caption{right picture}
    \label{fig:1r}
  \end{minipage}
\end{figure}

Or you put some letters semi-manually beneath them. If the caption is below the
figures use the unstarred version:
\begin{figure}[!htbp]
  \centering
  \begin{minipage}[]{0.4\linewidth}
    \centering
    \includegraphics[width=0.9\linewidth]{example-image-a}\\
    \mysubfig\label{fig:2l}
  \end{minipage}
  \begin{minipage}[]{0.4\linewidth}
    \centering
    \includegraphics[width=0.9\linewidth]{example-image-b}\\
    \mysubfig\label{fig:2r}
  \end{minipage}
  \caption{In \ref{fig:2l} we see an A; in \ref{fig:2r} we see a B}
  \label{fig:2}
\end{figure}

Else use the starred version:
\begin{figure}[!htbp]
  \centering
  \caption{In \ref{fig:3l} we see an A; in \ref{fig:3r} we see a B}
  \label{fig:3}
  \begin{minipage}[]{0.4\linewidth}
    \centering
    \includegraphics[width=0.9\linewidth]{example-image-a}\\
    \mysubfig*\label{fig:3l}
  \end{minipage}
  \begin{minipage}[]{0.4\linewidth}
    \centering
    \includegraphics[width=0.9\linewidth]{example-image-b}\\
    \mysubfig*\label{fig:3r}
  \end{minipage}
\end{figure}

Just to see if it works if you use the unstarred after the starred version.
\begin{figure}[!htbp]
  \centering
  \begin{minipage}[]{0.4\linewidth}
    \centering
    \includegraphics[width=0.9\linewidth]{example-image-a}\\
    \mysubfig\label{fig:4l}
  \end{minipage}
  \begin{minipage}[]{0.4\linewidth}
    \centering
    \includegraphics[width=0.9\linewidth]{example-image-b}\\
    \mysubfig\label{fig:4r}
  \end{minipage}
  \caption{In \ref{fig:4l} we see an A; in \ref{fig:4r} we see a B}
  \label{fig:4}
\end{figure}
\end{document}

This is the first page of the output (cropped to interesting parts): enter image description here

Note:

The formatting of the figure counter in the labels to the \mysubfigs have to be adapted to match the formatting of \thefigure. The below code doesn't need that, but I don't guarantee it to be compatible with packages that change \refstepcounter (though I did a short test with hyperref and it seemed to work out).

\documentclass[]{article}


\usepackage{blindtext}
\usepackage{graphicx}
\usepackage{etoolbox} % to hook into `figure` and reset the mysubfig counter

% reset the counter (it is unlikely that another figure environment has
% additional subfigures of the previous one)
\AtBeginEnvironment{figure}{\setcounter{mysubfig}{0}}


\newcounter{mysubfig}[figure]% also reset after the figure-counter has increased
\renewcommand*\themysubfig{\thefigure(\alph{mysubfig})}%
\makeatletter
\newcommand*\mysubfig{\@ifstar\mysubfig@above\mysubfig@below}
\newcommand*\mysubfig@above{%
  \refstepcounter{mysubfig}(\alph{mysubfig})}
\newcommand*\mysubfig@below{%
  \bgroup
  \advance\c@figure by 1
  \xdef\mysubfig@thefigure{\thefigure}%
  \egroup
  \stepcounter{mysubfig}%
  \protected@edef\@currentlabel{%
    \p@mysubfig\mysubfig@thefigure(\alph{mysubfig})}%
  (\alph{mysubfig})%
  }
\makeatother

\begin{document}
You could place the figures into \texttt{minipage}s with individual
\verb|\caption|s:
\begin{figure}[!htbp]
  \centering
  \begin{minipage}[]{0.4\linewidth}
    \centering
    \includegraphics[width=0.9\linewidth]{example-image-a}
    \caption{left picture}
    \label{fig:1l}
  \end{minipage}
  \begin{minipage}[]{0.4\linewidth}
    \centering
    \includegraphics[width=0.9\linewidth]{example-image-b}
    \caption{right picture}
    \label{fig:1r}
  \end{minipage}
\end{figure}

Or you put some letters semi-manually beneath them. If the caption is below the
figures use the unstarred version:
\begin{figure}[!htbp]
  \centering
  \begin{minipage}[]{0.4\linewidth}
    \centering
    \includegraphics[width=0.9\linewidth]{example-image-a}\\
    \mysubfig\label{fig:2l}
  \end{minipage}
  \begin{minipage}[]{0.4\linewidth}
    \centering
    \includegraphics[width=0.9\linewidth]{example-image-b}\\
    \mysubfig\label{fig:2r}
  \end{minipage}
  \caption{In \ref{fig:2l} we see an A; in \ref{fig:2r} we see a B}
  \label{fig:2}
\end{figure}

Else use the starred version:
\begin{figure}[!htbp]
  \centering
  \caption{In \ref{fig:3l} we see an A; in \ref{fig:3r} we see a B}
  \label{fig:3}
  \begin{minipage}[]{0.4\linewidth}
    \centering
    \includegraphics[width=0.9\linewidth]{example-image-a}\\
    \mysubfig*\label{fig:3l}
  \end{minipage}
  \begin{minipage}[]{0.4\linewidth}
    \centering
    \includegraphics[width=0.9\linewidth]{example-image-b}\\
    \mysubfig*\label{fig:3r}
  \end{minipage}
\end{figure}

Just to see if it works if you use the unstarred after the starred version.
\begin{figure}[!htbp]
  \centering
  \begin{minipage}[]{0.4\linewidth}
    \centering
    \includegraphics[width=0.9\linewidth]{example-image-a}\\
    \mysubfig\label{fig:4l}
  \end{minipage}
  \begin{minipage}[]{0.4\linewidth}
    \centering
    \includegraphics[width=0.9\linewidth]{example-image-b}\\
    \mysubfig\label{fig:4r}
  \end{minipage}
  \caption{In \ref{fig:4l} we see an A; in \ref{fig:4r} we see a B}
  \label{fig:4}
\end{figure}
\end{document}
2
  • Perfect!!! It works. The last code you put, works aligning perfectly the image, instead the first one doesn't. You saved me!!!
    – DjGj
    Jan 16, 2018 at 19:38
  • @SimonG that's funny, because the output is exactly the same for me. But if it does work, I'm glad I could help.
    – Skillmon
    Jan 16, 2018 at 23:35

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.