2

I'm trying to typeset a table of images. If image inserted into cell with \subfloat (subcaption package included), resulting box will be w/o bottom margin, but top, left and right margins will be ok.

Example:

\documentclass[a4paper]{book}
\usepackage[utf8]{inputenc}
\usepackage[demo]{graphicx}
\usepackage{caption}
\usepackage{subcaption}

\begin{document}

\chapter{Sample Chapter}

\begin{table}
  \caption{Sample Table}
  \label{tbl:sample}
  \begin{tabular}{|c|c|c|}
    \hline
    \begin{tabular}{c} \subfloat{\includegraphics[width=0.15\linewidth,height=0.15\linewidth]{image1}} \end{tabular} &
    \begin{tabular}{c} \subfloat{\includegraphics[width=0.15\linewidth,height=0.15\linewidth]{image1}} \end{tabular} &
    \begin{tabular}{c} \subfloat{\includegraphics[width=0.15\linewidth,height=0.15\linewidth]{image1}} \end{tabular}
    \\\hline
    \begin{tabular}{c} \subfloat{\includegraphics[width=0.15\linewidth,height=0.15\linewidth]{image2}} \end{tabular} &
    \begin{tabular}{c} \subfloat{\includegraphics[width=0.15\linewidth,height=0.15\linewidth]{image2}} \end{tabular} &
    \begin{tabular}{c} \subfloat{\includegraphics[width=0.15\linewidth,height=0.15\linewidth]{image2}} \end{tabular}
    \\\hline
  \end{tabular}
\end{table}

\end{document}

If subcaption package replaced with subfig the table looks good.

Is there any way to fix such behavior, since I can't use subfig? Or maybe there is another way to do table with images?

Update: Thanks to Gonzalo Medina, I solved original problem. But I stuck with another one with that approach. I'm trying to put titles in separate column:

\documentclass[a4paper]{book}
\usepackage[demo]{graphicx}
\usepackage{caption}

\begin{document}

\begin{table}
  \centering
  \caption{Sample Table}
  \label{tbl:sample1}
  \renewcommand\arraystretch{2}
  \begin{tabular}{|c|c|c|c|}
    \hline\rule{0pt}{\dimexpr0.15\textwidth+1em\relax}

    imeage 1 &
    \includegraphics[width=0.15\textwidth,height=0.15\textwidth]{image1} &
    \includegraphics[width=0.15\textwidth,height=0.15\textwidth]{image1} &
    \includegraphics[width=0.15\textwidth,height=0.15\textwidth]{image1}            

    \\\hline\rule{0pt}{\dimexpr0.15\textwidth+1em\relax}

    imeage 2 &    
    \includegraphics[width=0.15\linewidth,height=0.15\linewidth]{image2} &
    \includegraphics[width=0.15\linewidth,height=0.15\linewidth]{image2} &
    \includegraphics[width=0.15\linewidth,height=0.15\linewidth]{image2}

    \\ \hline
  \end{tabular}
\end{table}

\end{document} 

But text in first column is aligned at bottom of cell. How to make text in title column to be centered?

TheBug
  • 5,649

1 Answers1

5

\subfloat is not a command implemented in the subcaption package, so it won't be recognised by the package. If you want to use the subcaption package, you can use the either the subtable environment or the \subcaptionbox command. If your subimages don't need a caption, you can simply use \includegraphics.

In the following example I used the tabu package to buid the table with the six images. In the first case, the images won't have a caption: in the second case, each image will have its caption:

\documentclass[a4paper]{book}
\usepackage[demo]{graphicx}
\usepackage{caption}
\usepackage{subcaption}
\usepackage{tabu}

\begin{document}

\begin{table}
  \centering
  \caption{Sample Table}
  \label{tbl:sample1}
  \tabulinesep =_1em^1em
  \everyrow{\tabucline[0.4pt]-}
  \begin{tabu}{|c|c|c|}
    \includegraphics[width=0.15\textwidth,height=0.15\textwidth]{image1} &
    \includegraphics[width=0.15\textwidth,height=0.15\textwidth]{image1} &
    \includegraphics[width=0.15\textwidth,height=0.15\textwidth]{image1}            
    \\
    \includegraphics[width=0.15\linewidth,height=0.15\linewidth]{image2}  &
    \includegraphics[width=0.15\linewidth,height=0.15\linewidth]{image2} &
    \includegraphics[width=0.15\linewidth,height=0.15\linewidth]{image2}
  \end{tabu}
\end{table}

\begin{table}
  \centering
  \caption{Sample Table}
  \label{tbl:sample2}
  \tabulinesep =_1em^0.5em
  \everyrow{\tabucline[0.4pt]-}
  \begin{tabu}{|c|c|c|}
    \subcaptionbox{text}{\includegraphics[width=0.15\textwidth,height=0.15\textwidth]{image1}} &
    \subcaptionbox{text}{\includegraphics[width=0.15\textwidth,height=0.15\textwidth]{image1}} &
    \subcaptionbox{text}{\includegraphics[width=0.15\textwidth,height=0.15\textwidth]{image1}}            
    \\
    \subcaptionbox{text}{\includegraphics[width=0.15\linewidth,height=0.15\linewidth]{image2}}  &
     \subcaptionbox{text}{\includegraphics[width=0.15\linewidth,height=0.15\linewidth]{image2}} &
    \subcaptionbox{text}{\includegraphics[width=0.15\linewidth,height=0.15\linewidth]{image2}}
  \end{tabu}
\end{table}

\end{document}

enter image description here

Without the tabu package, in order to achieve the same result, some manual adjustments will be needed:

\documentclass[a4paper]{book}
\usepackage[demo]{graphicx}
\usepackage{caption}
\usepackage{subcaption}

\begin{document}

\begin{table}
  \centering
  \caption{Sample Table}
  \label{tbl:sample1}
  \renewcommand\arraystretch{2}
  \begin{tabular}{|c|c|c|}
        \hline\rule{0pt}{\dimexpr0.15\textwidth+1em\relax}
    \includegraphics[width=0.15\textwidth,height=0.15\textwidth]{image1} &
    \includegraphics[width=0.15\textwidth,height=0.15\textwidth]{image1} &
    \includegraphics[width=0.15\textwidth,height=0.15\textwidth]{image1}            
    \\ \hline\rule{0pt}{\dimexpr0.15\textwidth+1em\relax}
    \includegraphics[width=0.15\linewidth,height=0.15\linewidth]{image2}  &
    \includegraphics[width=0.15\linewidth,height=0.15\linewidth]{image2} &
    \includegraphics[width=0.15\linewidth,height=0.15\linewidth]{image2}
    \\ \hline
  \end{tabular}
\end{table}

\begin{table}
  \centering
  \caption{Sample Table}
  \label{tbl:sample2}
  \begin{tabular}{|c|c|c|}
     \hline\rule{0pt}{23pt}
     \subcaptionbox{text}{\includegraphics[width=0.15\textwidth,height=0.15\textwidth]{image1}} &
    \subcaptionbox{text}{\includegraphics[width=0.15\textwidth,height=0.15\textwidth]{image1}} &
    \subcaptionbox{text}{\includegraphics[width=0.15\textwidth,height=0.15\textwidth]{image1}}            
    \\[5.5em] \hline\rule{0pt}{23pt}
    \subcaptionbox{text}{\includegraphics[width=0.15\linewidth,height=0.15\linewidth]{image2}}  &
     \subcaptionbox{text}{\includegraphics[width=0.15\linewidth,height=0.15\linewidth]{image2}} &
    \subcaptionbox{text}{\includegraphics[width=0.15\linewidth,height=0.15\linewidth]{image2}}
   \\[5.5em] \hline
   \end{tabular}
\end{table}

\end{document}
Moriambar
  • 11,466
Gonzalo Medina
  • 505,128
  • You said: "\subfloat is not a command implemented in the subcaption package".

    But if I remove \usepackage{subcaption} command I'll get compilation error Undefined control sequence at '\subfloat' command. So it seems that subcaption package defines '\subfloat' command. Or it just uses another package that defines that command?

    – TheBug Sep 19 '11 at 20:36
  • 1
    @TheBug \subfloat is not defined in subcaption; it is defined in the subfig package which is incompatible with subcaption. – Gonzalo Medina Sep 19 '11 at 20:40
  • I'm sorry, maybe I wrote my question not so clear. Code in question use only four packages: inputenc, graphicx, caption and subcaption. As you see subfig is not included. Which one of these four packages does introduce \subfloat command? – TheBug Sep 19 '11 at 21:18
  • @TheBug: none. Compiling your example code in my system (TeX Live2011) produces the error ! Undefined control sequence. l.16 \begin{tabular}{c} \subfloat {\includegraphics[width=0.15\linewidth,.... Do you get something different? – Gonzalo Medina Sep 19 '11 at 21:20
  • Interesting... Code above compiled fine on my system. I'm using TeX Live 2009 (standard texlive package on Ubuntu 10.10). What version of caption and subcaption packages do you use? – TheBug Sep 19 '11 at 21:28
  • @TheBug: caption.sty 2011/08/18 v3.2b Customizing captions (AR), caption3.sty 2011/08/18 v1.3b caption3 kernel (AR), and subcaption.sty 2011/08/18 v1.1a Adding subcaptions (AR). – Gonzalo Medina Sep 19 '11 at 21:32
  • so i have Package: caption 2009/10/09 v3.1k Customizing captions (AR) Package: caption3 2009/10/09 v3.1k caption3 kernel (AR) Package: subcaption 2008/08/31 v1.0b Adding subcaptions (AR) maybe that makes sense. i'll try to update packages from ctan and experiment with it. – TheBug Sep 19 '11 at 21:39
  • @TheBug You should install TeX Live directly from TUG and not from the Ubuntu's repositories. There's a number of questions discussing the advantages; see, for example, http://tex.stackexchange.com/questions/13709/non-repository-management-of-texlive-packages-on-ubuntu/13710#13710 (with the obvious change of TeX Live2010 to the now available TeX Live2011). – Gonzalo Medina Sep 19 '11 at 21:45
  • Gonzalo, Is it possible to get same result w/o tabu package? – TheBug Oct 27 '11 at 16:37
  • @TheBug: see my updated answer. – Gonzalo Medina Oct 28 '11 at 00:02
  • Thanks that works. But I've got another issue when tried to fit your example to my needs. Please, take a look to update to the original question. – TheBug Oct 30 '11 at 10:06