4

I am facing an issue where using a subfloat causes the alignment to go wrong. Below is a snippet and a figure with and without the subfloat on the bottom left figure. How can I fix this issue without making significant changes to the code i.e. using a completely different environment.

\begin{figure*}
    \centering
    \begin{tikzpicture}
        \node[anchor=south west,inner sep=0] (image) at (0,0) {\includegraphics[width=0.22\textwidth]{example-image-a}}; 
        \begin{scope}[x={(image.south east)},y={(image.north west)}]
            \node[scale=0.75,fill=white,thick,draw=red!80] at (0.3,0.1) {$O^{(k)}$ = green plate};
        \end{scope}
    \end{tikzpicture}
    \begin{tikzpicture}
        \node[anchor=south west,inner sep=0] (image) at (0,0)
        {\includegraphics[width=0.22\textwidth]{example-image-a}};
        \begin{scope}[x={(image.south east)},y={(image.north west)}]
            \node[scale=0.75,fill=white,thick,draw=red!80] at (0.3,0.1) {$O^{(k)}$ = green cup};
        \end{scope}
    \end{tikzpicture}
    \begin{tikzpicture}
        \node[anchor=south west,inner sep=0] (image) at (0,0)
        {\includegraphics[width=0.22\textwidth]{example-image-a}}
        \begin{scope}[x={(image.south east)},y={(image.north west)}]
            \node[scale=0.75,fill=white,thick,draw=red!80] at (0.3,0.1) {$O^{(k)}$ = table};
        \end{scope}
    \end{tikzpicture}
    \begin{tikzpicture}
        \node[anchor=south west,inner sep=0] (image) at (0,0)
        {\includegraphics[width=0.22\textwidth]{example-image-a}}
        \begin{scope}[x={(image.south east)},y={(image.north west)}]
            \node[scale=0.75,fill=white,thick,draw=red!80] at (0.3,0.1) {$O^{(k)}$ = small shelf};
        \end{scope}
    \end{tikzpicture}\\ \vspace{1mm}
    \begin{tikzpicture}
        \node[anchor=south west,inner sep=0] (image) at (0,0)
        {\includegraphics[width=0.22\textwidth]{example-image-a}}
        \begin{scope}[x={(image.south east)},y={(image.north west)}]
            \node[scale=0.75,fill=white,thick,draw=red!80] at (0.3,0.1) {$O^{(k)}$ = red cup};
        \end{scope}
    \end{tikzpicture}
    \begin{tikzpicture}
        \node[anchor=south west,inner sep=0] (image) at (0,0)
        {\includegraphics[width=0.22\textwidth]{example-image-a}}
        \begin{scope}[x={(image.south east)},y={(image.north west)}]
            \node[scale=0.75,fill=white,thick,draw=red!80] at (0.3,0.1) {$O^{(k)}$ = red cup};
        \end{scope}
    \end{tikzpicture}
    \begin{tikzpicture}
        \node[anchor=south west,inner sep=0] (image) at (0,0)
        {\includegraphics[width=0.22\textwidth]{example-image-a}}
        \begin{scope}[x={(image.south east)},y={(image.north west)}]
            \node[scale=0.75,fill=white,thick,draw=red!80] at (0.3,0.1) {$O^{(k)}$ = table};
        \end{scope}
    \end{tikzpicture}
    \begin{tikzpicture}
        \node[anchor=south west,inner sep=0] (image) at (0,0)
         {\includegraphics[width=0.22\textwidth]{example-image-a}}
         \begin{scope}[x={(image.south east)},y={(image.north west)}]
            \node[scale=0.75,fill=white,thick,draw=red!80] at (0.3,0.1) {$O^{(k)}$ = table};
        \end{scope}
    \end{tikzpicture}\\ \vspace{1mm}
    \subfloat[]{\begin{tikzpicture}
        \node[anchor=south west,inner sep=0] (image) at (0,0)
        {\includegraphics[width=0.22\textwidth]{example-image-a}}
        \begin{scope}[x={(image.south east)},y={(image.north west)}]
            \node[scale=0.75,fill=white,thick,draw=red!80] at (0.3,0.1) {$O^{(k)}$ = red cup};
        \end{scope}
    \end{tikzpicture}}
    \subfloat[]{\begin{tikzpicture}
        \node[anchor=south west,inner sep=0] (image) at (0,0)
        {\includegraphics[width=0.22\textwidth]{example-image-a}}
        \begin{scope}[x={(image.south east)},y={(image.north west)}]
            \node[scale=0.75,fill=white,thick,draw=red!80] at (0.3,0.1) {$O^{(k)}$ = green cup};
        \end{scope}
    \end{tikzpicture}}
    \subfloat[]{\begin{tikzpicture}
        \node[anchor=south west,inner sep=0] (image) at (0,0)
        {\includegraphics[width=0.22\textwidth]{example-image-a}}
        \begin{scope}[x={(image.south east)},y={(image.north west)}]
            \node[scale=0.75,fill=white,thick,draw=red!80] at (0.3,0.1) {$O^{(k)}$ = table};
        \end{scope}
    \end{tikzpicture}}
    \subfloat[]{\begin{tikzpicture}
        \node[anchor=south west,inner sep=0] (image) at (0,0)
        {\includegraphics[width=0.22\textwidth]{example-image-a}}
        \begin{scope}[x={(image.south east)},y={(image.north west)}]
            \node[scale=0.75,fill=white,thick,draw=red!80] at (0.3,0.1) {$O^{(k)}$ = small shelf};
        \end{scope}
    \end{tikzpicture}}\\
     \caption{}
     \label{fig:qualitative}
\end{figure*}

enter image description here enter image description here

EDIT: Here is a MWE. It uses ieeeconf.cls that can be downloaded here http://ras.papercept.net/conferences/support/files/ieeeconf.zip I cannot paste the contents because it is too large it will crash the page.

enter image description here

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%2345678901234567890123456789012345678901234567890123456789012345678901234567890
%        1         2         3         4         5         6         7         8

\documentclass[letterpaper, 10 pt, conference]{ieeeconf}  % Comment this line out if you need a4paper

%\documentclass[a4paper, 10pt, conference]{ieeeconf}      % Use this line for a4 paper

\IEEEoverridecommandlockouts                              % This command is only needed if 
                                                          % you want to use the \thanks command

\overrideIEEEmargins                                      % Needed to meet printer requirements.

% The following packages can be found on http:\\www.ctan.org
\usepackage{amssymb}  % assumes amsmath package installed
\usepackage{amsmath}
\usepackage{booktabs}
\usepackage{tikz}
\usepackage{float}
\usepackage{subfig}

\title{\LARGE \bf
Preparation of Papers for IEEE Sponsored Conferences \& Symposia*
}

\begin{document}

\begin{figure*}
    \centering
    \begin{tikzpicture}
        \node[anchor=south west,inner sep=0] (image) at (0,0) {\includegraphics[width=0.22\textwidth]{example-image-a}}; 
        \begin{scope}[x={(image.south east)},y={(image.north west)}]
            \node[scale=0.75,fill=white,thick,draw=red!80] at (0.3,0.1) {$O^{(k)}$ = green plate};
        \end{scope}
    \end{tikzpicture}
    \begin{tikzpicture}
        \node[anchor=south west,inner sep=0] (image) at (0,0)
        {\includegraphics[width=0.22\textwidth]{example-image-a}};
        \begin{scope}[x={(image.south east)},y={(image.north west)}]
            \node[scale=0.75,fill=white,thick,draw=red!80] at (0.3,0.1) {$O^{(k)}$ = green cup};
        \end{scope}
    \end{tikzpicture}
    \begin{tikzpicture}
        \node[anchor=south west,inner sep=0] (image) at (0,0)
        {\includegraphics[width=0.22\textwidth]{example-image-a}};
        \begin{scope}[x={(image.south east)},y={(image.north west)}]
            \node[scale=0.75,fill=white,thick,draw=red!80] at (0.3,0.1) {$O^{(k)}$ = table};
        \end{scope}
    \end{tikzpicture}
    \begin{tikzpicture}
        \node[anchor=south west,inner sep=0] (image) at (0,0)
        {\includegraphics[width=0.22\textwidth]{example-image-a}};
        \begin{scope}[x={(image.south east)},y={(image.north west)}]
            \node[scale=0.75,fill=white,thick,draw=red!80] at (0.3,0.1) {$O^{(k)}$ = small shelf};
        \end{scope}
    \end{tikzpicture}\\ \vspace{1mm}
    \begin{tikzpicture}
        \node[anchor=south west,inner sep=0] (image) at (0,0)
        {\includegraphics[width=0.22\textwidth]{example-image-a}};
        \begin{scope}[x={(image.south east)},y={(image.north west)}]
            \node[scale=0.75,fill=white,thick,draw=red!80] at (0.3,0.1) {$O^{(k)}$ = red cup};
        \end{scope}
    \end{tikzpicture}
    \begin{tikzpicture}
        \node[anchor=south west,inner sep=0] (image) at (0,0)
        {\includegraphics[width=0.22\textwidth]{example-image-a}};
        \begin{scope}[x={(image.south east)},y={(image.north west)}]
            \node[scale=0.75,fill=white,thick,draw=red!80] at (0.3,0.1) {$O^{(k)}$ = red cup};
        \end{scope}
    \end{tikzpicture}
    \begin{tikzpicture}
        \node[anchor=south west,inner sep=0] (image) at (0,0)
        {\includegraphics[width=0.22\textwidth]{example-image-a}};
        \begin{scope}[x={(image.south east)},y={(image.north west)}]
            \node[scale=0.75,fill=white,thick,draw=red!80] at (0.3,0.1) {$O^{(k)}$ = table};
        \end{scope}
    \end{tikzpicture}
    \begin{tikzpicture}
        \node[anchor=south west,inner sep=0] (image) at (0,0)
         {\includegraphics[width=0.22\textwidth]{example-image-a}};
         \begin{scope}[x={(image.south east)},y={(image.north west)}]
            \node[scale=0.75,fill=white,thick,draw=red!80] at (0.3,0.1) {$O^{(k)}$ = table};
        \end{scope}
    \end{tikzpicture}\\ \vspace{1mm}
    \subfloat[$s$ = Set the table for 2 persons \\ $a$ = pick ]{\begin{tikzpicture}
        \node[anchor=south west,inner sep=0] (image) at (0,0)
        {\includegraphics[width=0.22\textwidth]{example-image-a}};
        \begin{scope}[x={(image.south east)},y={(image.north west)}]
            \node[scale=0.75,fill=white,thick,draw=red!80] at (0.3,0.1) {$O^{(k)}$ = red cup};
        \end{scope}
    \end{tikzpicture}}
    \subfloat[$s$ = Put all cups on the big shelf \\ $a$ = pick]{\begin{tikzpicture}
        \node[anchor=south west,inner sep=0] (image) at (0,0)
        {\includegraphics[width=0.22\textwidth]{example-image-a}};
        \begin{scope}[x={(image.south east)},y={(image.north west)}]
            \node[scale=0.75,fill=white,thick,draw=red!80] at (0.3,0.1) {$O^{(k)}$ = green cup};
        \end{scope}
    \end{tikzpicture}}
    \subfloat[$s$ = Set the table for 3 persons \\ $a$ = place]{\begin{tikzpicture}
        \node[anchor=south west,inner sep=0] (image) at (0,0)
        {\includegraphics[width=0.22\textwidth]{example-image-a}};
        \begin{scope}[x={(image.south east)},y={(image.north west)}]
            \node[scale=0.75,fill=white,thick,draw=red!80] at (0.3,0.1) {$O^{(k)}$ = table};
        \end{scope}
    \end{tikzpicture}}
    \subfloat[$s$ = Put jug on small shelf \\ $a$ = place]{\begin{tikzpicture}
        \node[anchor=south west,inner sep=0] (image) at (0,0)
        {\includegraphics[width=0.22\textwidth]{example-image-a}};
        \begin{scope}[x={(image.south east)},y={(image.north west)}]
            \node[scale=0.75,fill=white,thick,draw=red!80] at (0.3,0.1) {$O^{(k)}$ = small shelf};
        \end{scope}
    \end{tikzpicture}}\\
     \caption{}
     \label{fig:qualitative}
\end{figure*}

\end{document}
3
  • I tried to complete this to a document but get the error ! Package tikz Error: Giving up on this path. Did you forget a semicolon?. please post a complete error free document that shows the issue. Aug 21, 2021 at 16:39
  • @campa Hello, I have posted a MWE that is error free.
    – Kong
    Aug 21, 2021 at 16:51
  • The usual practice is to put \hfill or \hfil between \subfigs in one row. I like to add \par\vskip\floatsep between rows as well. Aug 22, 2021 at 15:52

2 Answers 2

5

The code of \subfloat ends (after a couple of expansions) with \ignorespaces, so the space after it is gobbled. If you want to preserve it, you should add e.g. an empty group.

\documentclass{article}

\usepackage{graphicx,subfig}

\begin{document}

\begin{figure}
\centering

% space between pictures
\includegraphics[width=.2\linewidth]{example-image}
\includegraphics[width=.2\linewidth]{example-image}
\caption{}

% space is gobbled away
\subfloat[]{\includegraphics[width=.2\linewidth]{example-image}}
\subfloat[]{\includegraphics[width=.2\linewidth]{example-image}}
\caption{}

% empty group reintroduces space
\subfloat[]{\includegraphics[width=.2\linewidth]{example-image}}{}
\subfloat[]{\includegraphics[width=.2\linewidth]{example-image}}
\caption{}
\end{figure}

\end{document}

enter image description here

2
  • thank you. Latex is something i will never understand till the day i retire.
    – Kong
    Aug 21, 2021 at 17:19
  • @Kong Well, that's not LaTeX itself; it's a choice of the package's author, and one that I find a wee bit puzzling too ;-)
    – campa
    Aug 21, 2021 at 17:24
6

The solution to your problem is what @campa said in his/her answer. With that in mind, I'd suggest you some additional changes.

  1. Why not include in the subfloat and the tikzpicture all the items in the same column?
  2. Use variables for the distances. That way if you need to change them you don't have to do it one by one.
  3. Declare some styles for the nodes. Same as above, you don't repeat the code and it's easier to change.
  4. (Edit) Add \struts to the labels. This improves the text vertical alignment.

Something like this:

\documentclass{article}
\usepackage   {lipsum}
\usepackage   {showframe} % just for this example
\usepackage   {subfig}
\usepackage   {tikz}

% styles, to avoid repeating the same code in all the nodes
\tikzset
{%
  image/.style={anchor=south west,inner sep=0},
  label/.style={scale=0.75,fill=white,thick,draw=red!80,anchor=south west}
}

% if you need to change dimensions better not to have to change them one by one, so:
\def\wdt {0.22\textwidth} % width of the figures
\def\vsep{2.2}            % vertical distance between figures
\def\lsep{0.1}            % label distance (you can define differnt x and y distance)

\begin{document}
\lipsum[1]

\begin{figure}[h]
    \centering
    \subfloat[]{% we don't want spaces here
    \begin{tikzpicture}
        \node[image] at (0,0)                 {\includegraphics[width=\wdt]{example-image-a}};
        \node[label] at (\lsep,\lsep)         {\strut$O^{(k)}$ = green plate};
        \node[image] at (0,-\vsep)            {\includegraphics[width=\wdt]{example-image-a}};
        \node[label] at (\lsep,\lsep-\vsep)   {\strut$O^{(k)}$ = red cup};
        \node[image] at (0,-2*\vsep)          {\includegraphics[width=\wdt]{example-image-a}};
        \node[label] at (\lsep,\lsep-2*\vsep) {\strut$O^{(k)}$ = red cup};
    \end{tikzpicture}}{} % <-- see campa's solution
    \subfloat[]{%
    \begin{tikzpicture}
        \node[image] at (0,0)                 {\includegraphics[width=\wdt]{example-image-a}};
        \node[label] at (\lsep,\lsep)         {\strut$O^{(k)}$ = green cup};
        \node[image] at (0,-\vsep)            {\includegraphics[width=\wdt]{example-image-a}};
        \node[label] at (\lsep,\lsep-\vsep)   {\strut$O^{(k)}$ = red cup};
        \node[image] at (0,-2*\vsep)          {\includegraphics[width=\wdt]{example-image-a}};
        \node[label] at (\lsep,\lsep-2*\vsep) {\strut$O^{(k)}$ = green cup};
    \end{tikzpicture}}{}
    \subfloat[]{%
    \begin{tikzpicture}
        \node[image] at (0,0)                 {\includegraphics[width=\wdt]{example-image-a}};
        \node[label] at (\lsep,\lsep)         {\strut$O^{(k)}$ = table};
        \node[image] at (0,-\vsep)            {\includegraphics[width=\wdt]{example-image-a}};
        \node[label] at (\lsep,\lsep-\vsep)   {\strut$O^{(k)}$ = table};
        \node[image] at (0,-2*\vsep)          {\includegraphics[width=\wdt]{example-image-a}};
        \node[label] at (\lsep,\lsep-2*\vsep) {\strut$O^{(k)}$ = table};
    \end{tikzpicture}}{}
    \subfloat[]{%
    \begin{tikzpicture}
        \node[image] at (0,0)                 {\includegraphics[width=\wdt]{example-image-a}};
        \node[label] at (\lsep,\lsep)         {\strut$O^{(k)}$ = small shelf};
        \node[image] at (0,-\vsep)            {\includegraphics[width=\wdt]{example-image-a}};
        \node[label] at (\lsep,\lsep-\vsep)   {\strut$O^{(k)}$ = table};
        \node[image] at (0,-2*\vsep)          {\includegraphics[width=\wdt]{example-image-a}};
        \node[label] at (\lsep,\lsep-2*\vsep) {\strut$O^{(k)}$ = small shelf};
    \end{tikzpicture}}
    \caption{}
    \label{fig:qualitative}
\end{figure}

\lipsum[2]
\end{document}

enter image description here

3
  • Wow! I just spent an hour trying to figure out why my text boxes aren't nicely aligned, searching through the forums for a solution, and you just posted it. Thank you very much.
    – Kong
    Aug 21, 2021 at 17:47
  • Nice (+1). Note that pinging users with @ works only in comments, not in questions/answers. And BTW, it's "his" ;-)
    – campa
    Aug 21, 2021 at 19:08
  • @campa, thanks, I take note of everything, :-) Aug 21, 2021 at 19:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .