Subfigures: fail to inset 6 pictures in two rows

floatssubfloats

I'm using the Frontier template to insert figures, but I keep coming up with errors. I do upload caption and subcaption packages. only one of the figures can show up and no matter how I adjust the size of them, I still fail to inset them.

The errors include:

Not in outer par mode. Illegal measure of unit (this appears at first \begin(subfigure)

Missing number, treated as zero. Illegal measure of unit ( this appears at all other \begin(subfigure)

The following is my code:

\documentclass[utf8]{frontiersHLTH} % for Health articles

\usepackage{url,hyperref,lineno,microtype,caption,subcaption}
\usepackage[onehalfspacing]{setspace}


\usepackage{fixltx2e}
\usepackage{amsmath}
\usepackage{tabularx, booktabs, ragged2e}
\usepackage{adjustbox}
\usepackage{threeparttable}
\usepackage{graphicx}
\usepackage{array}

\linenumbers

\begin{document}

\begin{figure}
    \centering
    \begin{subfigure}[hbt!]{0.33\textwidth}
    \centering
    \includegraphics[width=0.2\textwidth]{figure2 11.jpg}
    \caption{11}
    \end{subfigure}
    \hfill
    \begin{subfigure}[hbt!]{0.33\textwidth}
    \centering
    \includegraphics[width=0.2\textwidth]{figure2 12.jpg}
    \caption{12}
    \end{subfigure}
    \hfill
    \begin{subfigure}[hbt!]{0.33\textwidth}
    \centering
    \includegraphics[width=0.2\textwidth]{figure2 13.jpg}
    \caption{13}
    \end{subfigure}
    \hfill
    \begin{subfigure}[hbt!]{0.3\textwidth}
    \centering
    \includegraphics[width=0.2\textwidth]{figure2 21.jpg}
    \caption{21}
    \end{subfigure}    
    \begin{subfigure}[hbt!]{0.3\textwidth}
    \centering
    \includegraphics[width=0.2\textwidth]{figure2 22.jpg}
    \caption{22}
    \end{subfigure}    
    \begin{subfigure}[hbt!]{0.3\textwidth}
    \centering
    \includegraphics[width=0.2\textwidth]{figure2 23.jpg}
    \caption{23}
    \end{subfigure}
    \caption{Caption}
    \label{fig:my_label}
\end{figure}
\end{document}

enter image description here

I'd really appreciate if anyone can help me fix this problem! Thanks!!

Best Answer

Your code contains various inaccuracies which, taken together, result in the unwanted layout. Take a look at the following modifications. Observe the uses of % after selected instances of \end{subfigure}, the uses of \hfill, and the purging of all \centering instructions as they do nothing useful.

Incidentally, when I try to recompile the code shown below on Overleaf, with the frontiersHLTH document class instead of article, I get an error message, about the file frontiersHLTH.cls not being found. That's why I use article for the example at hand.

enter image description here

\documentclass{article}     % I don't have access to 'frontiersHLTH'
%% (I've simplified the preamble to the bare minimum)
\usepackage[demo]{graphicx} % remove 'demo' option in real document
\usepackage{subcaption}
\captionsetup[subfigure]{skip=0.25\baselineskip}

\begin{document}

\begin{figure}
    \begin{subfigure}{0.32\textwidth}
    \includegraphics[width=1\textwidth]{figure2 11}
    \caption{11}
    \end{subfigure}% <-- note the '%' comment char.
    \hfill
    \begin{subfigure}{0.32\textwidth}
    \includegraphics[width=1\textwidth]{figure2 12}
    \caption{12}
    \end{subfigure}% <-- note the '%' comment char.
    \hfill
    \begin{subfigure}{0.32\textwidth}
    \includegraphics[width=1\textwidth]{figure2 13}
    \caption{13}
    \end{subfigure} % leave a blank line to assure a par. break

    \medskip % insert a bit of vertical whitespace
    \begin{subfigure}{0.32\textwidth}
    \includegraphics[width=1\textwidth]{figure2 21}
    \caption{21}
    \end{subfigure}% <-- note the '%' comment char.
    \hfill
    \begin{subfigure}{0.32\textwidth}
    \includegraphics[width=1\textwidth]{figure2 22}
    \caption{22}
    \end{subfigure}% <-- note the '%' comment char.
    \hfill  
    \begin{subfigure}{0.32\textwidth}
    \includegraphics[width=1\textwidth]{figure2 23}
    \caption{23}
    \end{subfigure}
    
\caption{Caption}
\label{fig:my_label}
\end{figure}
\end{document}