2

I write a python script and generate a latex script in which multiple images are arranged using \subcaption. Core code :

....
\usepackage{subcaption}
....
\begin{figure}
    \begin{subfigure}[31]{0.2\textwidth}
        \includegraphics[width = \textwidth, height = \textwidth]
        {ly1_31.jpg}
        \caption{Channel 31}
        \label{fig: 31}
    \end{subfigure}
    \begin{subfigure}[97]{0.2\textwidth}
        \includegraphics[width = \textwidth, height = \textwidth]
        {ly1_97.jpg}
        \caption{Channel 97}
        \label{fig: 97}
    \end{subfigure}
    .......
    \caption{ Features maps manually selected from 512 maps }
    \label{fig} 
 \end{figure}

The code of subfigures is same except the channel number and file name generated by python. Here I have 14 .jpg images. And the width of the white space between images in the last row is obviously shorter than the previous 3 rows.(The last row is not full.) How can I control the width of that space?

And I'm also not sure if the width of white space is the reason that I set {0.2\textwidth} in 5th line and later lines but only get 4 images in one row when the row is full.(I think so, but any detailed explanation?)

The last question is quite weird. See the screenshot. enter image description here

There is one image goes to the wrong place(channel 227). And I'm sure there is no "7b" in my tex file. All the images are of same size. So what's the problem?

Any ideas are welcome.

1

Here's a minimal example reproducing your problem:

\documentclass{article}
\usepackage{subcaption}
\usepackage{graphicx}

\begin{document}

\begin{figure}
\centering

\begin{subfigure}[31]{0.2\textwidth}
\includegraphics[width = \textwidth]{example-image-a}
\caption{Channel 31}\label{fig:31}
\end{subfigure}
\begin{subfigure}[227]{0.2\textwidth}
\includegraphics[width = \textwidth]{example-image-b}
\caption{Channel 227}\label{fig:227}
\end{subfigure}

\end{figure}

\end{document}

enter image description here

The error is in the wrong syntax used: the optional argument to \begin{subfigure} should be a single character among b, c or t.

Fix: remove the wrong bits

\documentclass{article}
\usepackage{subcaption}
\usepackage{graphicx}

\begin{document}

\begin{figure}
\centering

\begin{subfigure}{0.2\textwidth}
\includegraphics[width = \textwidth]{example-image-a}
\caption{Channel 31}\label{fig:31}
\end{subfigure}
\begin{subfigure}{0.2\textwidth}
\includegraphics[width = \textwidth]{example-image-b}
\caption{Channel 227}\label{fig:227}
\end{subfigure}

\end{figure}

\end{document}

enter image description here

What happens?

The code for \begin{subfigure} looks whether an optional argument is supplied, otherwise assumes it is b; then it does code like

\if<optional argument>b%
  <code when test returns true>%
\else
  <code when test returns false>%
\fi

In the 227 case, the text becomes

\if227b
  <code when test returns true>%
\else
  <code when test returns false>%
\fi

and the test returns true because \if compares the next two unexpandable tokens, in this case 2 and 2. By rule, the <true code> starts just after the test and ends at \else (or \fi if no \else comes along), so in this case it also contains 7b.

How to get five images per row with no space?

You should remove the space between two subfigures, generated by the end-of-line after \end{subfigure}.

You can exploit the fact that TeX removes spaces after control sequences:

\documentclass{article}
\usepackage{subcaption}
\usepackage{graphicx}

\newcommand{\subfiguresep}{\hspace{0pt}}

\begin{document}

\begin{figure}
\centering

\begin{subfigure}{0.2\textwidth}
\includegraphics[width = \textwidth]{example-image-a}
\caption{Channel 31}\label{fig:31}
\end{subfigure}\subfiguresep
\begin{subfigure}{0.2\textwidth}
\includegraphics[width = \textwidth]{example-image-a}
\caption{Channel 31}\label{fig:31}
\end{subfigure}\subfiguresep
\begin{subfigure}{0.2\textwidth}
\includegraphics[width = \textwidth]{example-image-a}
\caption{Channel 31}\label{fig:31}
\end{subfigure}\subfiguresep
\begin{subfigure}{0.2\textwidth}
\includegraphics[width = \textwidth]{example-image-a}
\caption{Channel 31}\label{fig:31}
\end{subfigure}\subfiguresep
\begin{subfigure}{0.2\textwidth}
\includegraphics[width = \textwidth]{example-image-a}
\caption{Channel 31}\label{fig:31}
\end{subfigure}\subfiguresep
\begin{subfigure}{0.2\textwidth}
\includegraphics[width = \textwidth]{example-image-a}
\caption{Channel 31}\label{fig:31}
\end{subfigure}\subfiguresep
\begin{subfigure}{0.2\textwidth}
\includegraphics[width = \textwidth]{example-image-a}
\caption{Channel 31}\label{fig:31}
\end{subfigure}\subfiguresep
\begin{subfigure}{0.2\textwidth}
\includegraphics[width = \textwidth]{example-image-a}
\caption{Channel 31}\label{fig:31}
\end{subfigure}\subfiguresep
\begin{subfigure}{0.2\textwidth}
\includegraphics[width = \textwidth]{example-image-b}
\caption{Channel 227}\label{fig:227}
\end{subfigure}\subfiguresep

\end{figure}

\end{document}

enter image description here

The \hspace{0pt} allows for a line break, adding no space. If you prefer the last row flush left, use \raggedright instead of \centering.

If you want some space between the subfigures, you can do

\documentclass{article}
\usepackage{subcaption}
\usepackage{graphicx}

\newcommand{\subfiguresep}{\hspace{2pt}}
\captionsetup[subfigure]{font=footnotesize}

\begin{document}

\begin{figure}
\centering

\begin{subfigure}{\dimexpr 0.2\textwidth-8pt}
\includegraphics[width = \textwidth]{example-image-a}
\caption{Channel 31}\label{fig:31}
\end{subfigure}\subfiguresep
\begin{subfigure}{\dimexpr 0.2\textwidth-8pt}
\includegraphics[width = \textwidth]{example-image-a}
\caption{Channel 31}\label{fig:31}
\end{subfigure}\subfiguresep
\begin{subfigure}{\dimexpr 0.2\textwidth-8pt}
\includegraphics[width = \textwidth]{example-image-a}
\caption{Channel 31}\label{fig:31}
\end{subfigure}\subfiguresep
\begin{subfigure}{\dimexpr 0.2\textwidth-8pt}
\includegraphics[width = \textwidth]{example-image-a}
\caption{Channel 31}\label{fig:31}
\end{subfigure}\subfiguresep
\begin{subfigure}{\dimexpr 0.2\textwidth-8pt}
\includegraphics[width = \textwidth]{example-image-a}
\caption{Channel 31}\label{fig:31}
\end{subfigure}\subfiguresep
\begin{subfigure}{\dimexpr 0.2\textwidth-8pt}
\includegraphics[width = \textwidth]{example-image-a}
\caption{Channel 31}\label{fig:31}
\end{subfigure}\subfiguresep
\begin{subfigure}{\dimexpr 0.2\textwidth-8pt}
\includegraphics[width = \textwidth]{example-image-a}
\caption{Channel 31}\label{fig:31}
\end{subfigure}\subfiguresep
\begin{subfigure}{\dimexpr 0.2\textwidth-8pt}
\includegraphics[width = \textwidth]{example-image-a}
\caption{Channel 31}\label{fig:31}
\end{subfigure}\subfiguresep
\begin{subfigure}{\dimexpr 0.2\textwidth-8pt}
\includegraphics[width = \textwidth]{example-image-b}
\caption{Channel 227}\label{fig:227}
\end{subfigure}\subfiguresep

\end{figure}

\end{document}

Note I had to reduce the font size for the subcaption, otherwise it would exceed the image width.

enter image description here

  • Exactly where the error is. I guess I mixed syntax with some other methods I saw previously. And could you help explain the wrong white space width in the last row? – lincr Nov 14 '16 at 9:24
  • @ArthurTian I added how you can get no space between the images – egreg Nov 14 '16 at 9:34
  • So there is somthing like macro command in latex? Really cool. I tried your method. It works fine but kind of crowded with no space. If I choose to use my previous code with space, is it possible to control the white space in last row? – lincr Nov 14 '16 at 9:45
  • @ArthurTian I added the version with fixed space between the figures – egreg Nov 14 '16 at 9:55
  • Works pretty good! It's really really nice of you to explain and give solutions. – lincr Nov 14 '16 at 10:08

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.