Graphics, Figures & TablesSubfig: pictures not aligning

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
JefVL
Posts: 13
Joined: Sat Mar 22, 2014 10:27 pm

Subfig: pictures not aligning

Post by JefVL »

Hi all,

First of all I'd like to thank this community for all the answers that I've already found on this site, however now I've encountered something that I can't find an answer to.

I'm writing my thesis in LaTeX (using MikTex & TeXnicCenter), and I wanted to use the subfigure package to fit 4 pictures into one. However, when I build my file, I had some issues and went looking for answers. Then I found something mentioning other packages called subcaption and subfig. I tried subfig first, but got errors that I couldn't get rid of so I used subcaption, and now I get the result shown in the attached picture. As you can see, the pictures on the right are positioned a bit lower than the ones on the left.

Here's a minimal example code I'm using (hope I didn't forget anything here, never done this before):

Code: Select all

\documentclass[11pt,a4paper,oneside]{article}
\usepackage{a4wide}
\usepackage[dutch]{babel}
\usepackage{amsmath}
\usepackage{url}
\usepackage{graphicx}
\usepackage[latin1]{inputenc}
\usepackage[small,bf,hang]{caption}
\usepackage{xspace}
\usepackage[per-mode=symbol, output-decimal-marker={,}, separate-uncertainty=true, exponent-product=\cdot]{siunitx}
\usepackage{microtype}
\usepackage[fixlanguage]{babelbib}
\selectbiblanguage{dutch}
\usepackage[hidelinks]{hyperref}
\usepackage{booktabs}
\bibliographystyle{babunsrt}
\usepackage{subcaption}
\captionsetup{margin=10pt,font=small,labelfont=bf}
\usepackage[usenames]{xcolor}

\begin{document}
\begin{figure}
        \centering
        \begin{subfigure}[b]{0.3\textwidth}
                \includegraphics[width=\textwidth]{ReactivationA}
                \caption{Visuele cortex in P45 muizen herstelt niet volledig 7 weken na ME}
                \label{fig:ReactivationA}
        \end{subfigure}
        \quad
        \begin{subfigure}[b]{0.3\textwidth}
                \includegraphics[width=\textwidth]{ReactivationB}
                \caption{AMC-muis met normale visuele cortex}
                \label{fig:ReactivationB}
        \end{subfigure}
        
        \begin{subfigure}[b]{0.3\textwidth}
                \includegraphics[width=\textwidth]{ReactivationC}
                \caption{Visuele cortex in P120 muizen herstelt wel volledig 7 weken na ME}
                \label{fig:ReactivationC}
        \end{subfigure}
        \quad
        \begin{subfigure}[b]{0.3\textwidth}
                \includegraphics[width=\textwidth]{ReactivationD}
                \caption{AMC-muis met normale visuele cortex}
                \label{fig:ReactivationD}
        \end{subfigure}
        \caption{Vergelijking tussen de activiteit van de visuele cortex in P45 en P120-muizen met en zonder ME. Bij age match control (AMC)-muizen wordt geen ME uitgevoerd: ze dienen om het resultaat van ME-muizen te vergelijken met de normale situatie in een even oude muis. AMC-muizen zijn dus ook P45 of P120 + 7 weken. Zoals aangegeven op de schaal aan de rechterkant betekent een donkere, groene kleur dat er weinig activiteit is, terwijl rood en geel voor hoge concentraties activiteitsmerker staan~[\ref{src:DoctoraatJulie}].}\label{fig:Reactivation}
\end{figure}
\end{document}
When I use the subfig package instead, I get the following errors:
! Missing number, treated as zero.
<to be read again>
}
l.220 \begin{subfloat}{0.3\textwidth}
! Illegal unit of measure (pt inserted).
<to be read again>
}
l.220 \begin{subfloat}{0.3\textwidth}

That x4, once for each subfloat, + 8 overfull \hbox warnings (which can probably be solved by decreasing image size, I always try to fix errors before moving on to warnings & bad boxes).

Help would be greatly appreciated since I have a deadline monday.
Thanks in advance!

Jef
Attachments
Subfig.jpg
Subfig.jpg (75.53 KiB) Viewed 19675 times

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

hugovdberg
Posts: 133
Joined: Sat Feb 25, 2012 6:12 pm

Subfig: pictures not aligning

Post by hugovdberg »

As you can see in this topic on the forum the subfigure package is obsolete, and should therefore be avoided. So great you already switched to subcaption.

The problem of the misaligned subfigures is caused by the positioning of the subfigures, the optional argument [b] specifies the bottom of the content of the environment, so including the subcaption, should be aligned. In this case you want the top of each figure to be aligned, disregarding the exact height of the subcaption so the top of the figures should be aligned using [t].

Code: Select all

\documentclass[11pt,a4paper,oneside]{article}
\usepackage{a4wide}
\usepackage[dutch]{babel}
\usepackage{amsmath}
\usepackage{url}
\usepackage{graphicx}
\usepackage[latin1]{inputenc}
\usepackage[small,bf,hang]{caption}
\usepackage{xspace}
\usepackage[per-mode=symbol, output-decimal-marker={,}, separate-uncertainty=true, exponent-product=\cdot]{siunitx}
\usepackage{microtype}
\usepackage[fixlanguage]{babelbib}
\selectbiblanguage{dutch}
\usepackage[hidelinks]{hyperref}
\usepackage{booktabs}
\bibliographystyle{babunsrt}
\usepackage{subcaption}
\captionsetup{margin=10pt,font=small,labelfont=bf}
\usepackage[usenames]{xcolor}

\begin{document}
\begin{figure}
        \centering
        \begin{subfigure}[t]{0.3\textwidth}
                \includegraphics[width=\textwidth]{ReactivationA}
                \caption{Visuele cortex in P45 muizen herstelt niet volledig 7 weken na ME}
                \label{fig:ReactivationA}
        \end{subfigure}
        \quad
        \begin{subfigure}[t]{0.3\textwidth}
                \includegraphics[width=\textwidth]{ReactivationB}
                \caption{AMC-muis met normale visuele cortex}
                \label{fig:ReactivationB}
        \end{subfigure}
        
        \begin{subfigure}[t]{0.3\textwidth}
                \includegraphics[width=\textwidth]{ReactivationC}
                \caption{Visuele cortex in P120 muizen herstelt wel volledig 7 weken na ME}
                \label{fig:ReactivationC}
        \end{subfigure}
        \quad
        \begin{subfigure}[t]{0.3\textwidth}
                \includegraphics[width=\textwidth]{ReactivationD}
                \caption{AMC-muis met normale visuele cortex}
                \label{fig:ReactivationD}
        \end{subfigure}
        \caption{Vergelijking tussen de activiteit van de visuele cortex in P45 en P120-muizen met en zonder ME. Bij age match control (AMC)-muizen wordt geen ME uitgevoerd: ze dienen om het resultaat van ME-muizen te vergelijken met de normale situatie in een even oude muis. AMC-muizen zijn dus ook P45 of P120 + 7 weken. Zoals aangegeven op de schaal aan de rechterkant betekent een donkere, groene kleur dat er weinig activiteit is, terwijl rood en geel voor hoge concentraties activiteitsmerker staan~[\ref{src:DoctoraatJulie}].}\label{fig:Reactivation}
\end{figure}
\end{document}
Ubuntu 13.10 + Tex Live 2013 + Texmaker / Windows 7 Pro + MikTex 2.9 + TexnicCenter / Android 4.3 + TexPortal + DroidEdit
JefVL
Posts: 13
Joined: Sat Mar 22, 2014 10:27 pm

Re: Subfig: pictures not aligning

Post by JefVL »

That did the trick! The pictures were of the same height, but apparently it aligned the bottom of the subcaptions... Thanks a lot!
Post Reply