GeneralStrip of subfigures: difficult to have?

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
lalelale
Posts: 6
Joined: Fri Sep 19, 2008 3:02 pm

Strip of subfigures: difficult to have?

Post by lalelale »

Hello,
I frequently have to introduce several subfigures in my articles. Usually a figure is in form of a single horizontal "strip" of images, each with the same height.
Since each image has a different width/height ratio, it's not easy to know the correct width for each image so that their height is the same.

So what I'm after is an easy way to insert some images in a strip: ideally, I should not need to specify any size, and the software automatically choosing a correct scale for each of the images so that they all have the same height and the total width is \textwidth, also accounting a given (possibly fixed) spacing between the images.

AFAIK the subfigure package does not have this functionality

Recommended reading 2024:

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

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

Ted
Posts: 94
Joined: Sat Jun 23, 2007 4:11 pm

Strip of subfigures: difficult to have?

Post by Ted »

lalelale wrote:So what I'm after is an easy way to insert some images in a strip: ideally, I should not need to specify any size, and the software automatically choosing a correct scale for each of the images so that they all have the same height and the total width is \textwidth, also accounting a given (possibly fixed) spacing between the images.

AFAIK the subfigure package does not have this functionality
If you're willing to manually adjust the height, you can use the "height" option of graphicx's includegraphics to do what you need. Consider something like...

Code: Select all

% In preamble (at least the \newlength line)
\newlength{\subfigheight}
\setlength{\subfigheight}{1in}
and then...

Code: Select all

% In main document
\begin{figure}\centering
\subfloat{ \includegraphics[height=\subfigheight]{image1} }
\subfloat{ \includegraphics[height=\subfigheight]{image2} }
\subfloat{ \includegraphics[height=\subfigheight]{image3} }
\subfloat{ \includegraphics[height=\subfigheight]{image4} }
\subfloat{ \includegraphics[height=\subfigheight]{image5} }
\end{figure}
You can use the \setlength line above to tweak the height of the images, and includegraphics will keep all of the heights the same. When things get too wide, the figures should wrap to the next line.

Note that \setlength can be called within the document. If you call it outside of a group, it will change the value permanently from that point on. However, if you change it within a group (like a figure), then it will only change inside that group. So I imagine seeing lots of ...

Code: Select all

\begin{figure}\centering
\setlength{\subfigheight}{1.5in}
\subfloat{ \includegraphics[height=\subfigheight]{image1} }
\subfloat{ \includegraphics[height=\subfigheight]{image2} }
\subfloat{ \includegraphics[height=\subfigheight]{image3} }
\subfloat{ \includegraphics[height=\subfigheight]{image4} }
\subfloat{ \includegraphics[height=\subfigheight]{image5} }
\end{figure}
and EVEN

Code: Select all

\begin{figure}\centering
\setlength{\subfigheight}{1.5in}
\subfloat{ \includegraphics[height=\subfigheight]{image1} }
\subfloat{ \includegraphics[height=\subfigheight]{image2} }
\subfloat{ \includegraphics[height=\subfigheight]{image3} }
\subfloat{ \includegraphics[height=\subfigheight]{image4} }
\subfloat{ \includegraphics[height=\subfigheight]{image5} }\\
\setlength{\subfigheight}{2.5in}
\subfloat{ \includegraphics[height=\subfigheight]{image6} }
\subfloat{ \includegraphics[height=\subfigheight]{image7} }
\subfloat{ \includegraphics[height=\subfigheight]{image8} }
\end{figure}
Further, you can do more interesting things with centering and padding and rubber lengths. For example...

Code: Select all

% Centered and flushed left and right
\begin{figure}
\subfloat{ \includegraphics[height=\subfigheight]{image1} }
\hfill
\subfloat{ \includegraphics[height=\subfigheight]{image2} }
\hfill
\subfloat{ \includegraphics[height=\subfigheight]{image3} }
\hfill
\subfloat{ \includegraphics[height=\subfigheight]{image4} }
\hfill
\subfloat{ \includegraphics[height=\subfigheight]{image5} }
\end{figure}
or...

Code: Select all

% Centered with equal space between and beside
\begin{figure}
\centerline{%
\subfloat{ \includegraphics[height=\subfigheight]{image1} }
\hss
\subfloat{ \includegraphics[height=\subfigheight]{image2} }
\hss
\subfloat{ \includegraphics[height=\subfigheight]{image3} }
\hss
\subfloat{ \includegraphics[height=\subfigheight]{image4} }
\hss
\subfloat{ \includegraphics[height=\subfigheight]{image5} }
}
\end{figure}
Does that help?
-- Ted [home/blog]
lalelale
Posts: 6
Joined: Fri Sep 19, 2008 3:02 pm

Strip of subfigures: difficult to have?

Post by lalelale »

Ted wrote: Does that help?
Yeah, very useful. In combination to kile's preview feature, it may help a lot.
Still, it would be very useful to have a package to compute that height automatically, but I guess it's not there yet :)

Thank you! :)

Only one question: is there any difference in using subfloat or subfigure there?
lalelale
Posts: 6
Joined: Fri Sep 19, 2008 3:02 pm

Re: Strip of subfigures: difficult to have?

Post by lalelale »

Yeah I confirm, it works great with Kile's quickpreview. 2-3 iterations often suffice to find a good-looking value.

Just a comment: what about defining the \subfigheight as a fraction of \textwidth? This should ensure that if \textwidth is changed, the layout of the subfigures is not mangled. True?
Ted
Posts: 94
Joined: Sat Jun 23, 2007 4:11 pm

Strip of subfigures: difficult to have?

Post by Ted »

lalelale wrote:Still, it would be very useful to have a package to compute that height automatically, but I guess it's not there yet :)
Such a package would have to jam everything on the line stretched to a common height, allowing for an overfull hbox, and measure the box_width/columnwidth ratio. It would then scale the height by the ratio. Such a package would have to merge features of subfloat and graphicx, which are two very unrelated packages. So I'm guessing that such a package won't exist in the near future. :)

However, you could do something similar yourself. If I had this problem, I'd make my fixed height very small and use my previewer (Skim on a Mac) to measure the resulting width. I'd then do the scaling based on my measurement. So in two tries, I'd have the right width.
lalelale wrote:Only one question: is there any difference in using subfloat or subfigure there?
You are probably using the old subfigure package. Instead, you should be using the subfig package, which supersedes subfigure. The subfig package uses "subfloats" rather than "subfigs." They're nearly identical, except the language makes more sense. That is, you should be able to have "subtables". Saying "subfloat" is more general.

Similarly, the memoir document class has \subbottom (and \subtop, I think) that works just like subfloat (and subfigure). Additionally, you can use subfig (or subfigure, but you should use subfig) to get features of subfig within memoir.
-- Ted [home/blog]
Ted
Posts: 94
Joined: Sat Jun 23, 2007 4:11 pm

Strip of subfigures: difficult to have?

Post by Ted »

lalelale wrote:Just a comment: what about defining the \subfigheight as a fraction of \textwidth? This should ensure that if \textwidth is changed, the layout of the subfigures is not mangled. True?
That's totally fine. I was just keeping things simple to make all of the mechanisms clear.

Personally, I often resize with respect to \columnwidth. Of course, it is going to depend on what you're resizing and where it is.
-- Ted [home/blog]
lalelale
Posts: 6
Joined: Fri Sep 19, 2008 3:02 pm

Re: Strip of subfigures: difficult to have?

Post by lalelale »

Thanks again,
you have been incredibly helpful and instructive :)
bramp
Posts: 10
Joined: Mon Aug 25, 2008 5:33 pm

Re: Strip of subfigures: difficult to have?

Post by bramp »

This is a very useful feature, and one I wanted very recently. In the end I solved it in the by varying the height until it looked good.

Perhaps one day this thread will be updated with a package which solves this :)
this-is-me
Posts: 1
Joined: Tue Jun 03, 2014 5:07 pm

Re: Strip of subfigures: difficult to have?

Post by this-is-me »

hey,

if i understand the original question right, the solution is given here by herbert:

http://tex.stackexchange.com/questions/ ... ame-height

that is only for two figures next to each other; i have added two things in order to have subfloats instead (\subfloat[]{...} and \setcounter{subfigure}).

cheers, maybe this helps someone :)

\begin{figure}
\setbox0\hbox{%
\subfloat[]{\includegraphics[width=.45\textwidth]{fig1.png}}%
}%
\setbox2\hbox{%
\subfloat[]{\includegraphics[width=.45\textwidth]{fig2.png}}%
}%

\ifdim\ht0>\ht2
\setbox0\hbox{%
\setcounter{subfigure}{0}
\subfloat[]{\includegraphics[height=\ht2]{fig1.png}}%
}%
\else
\setbox2\hbox{%
\setcounter{subfigure}{1}
\subfloat[]{\includegraphics[height=\ht0]{fig2.png}}%
}%
\fi
\noindent
\parbox{.45\textwidth}{%
\centering
\unhbox0
% \caption{This is figure 1.}
\label{fig:fig1}
}%
\hfil
\parbox{.45\textwidth}{%
\centering
\unhbox2
% \caption{This is figure 2.}
\label{fig:fig2}
}%
\caption{Two nice figures.}
\end{figure}
User avatar
Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

Strip of subfigures: difficult to have?

Post by Johannes_B »

Thnaks for sharing.

Can you edit your post and make it compilable (MWE, Herbert did the same)? Please also use the code button, this way one can easily test the code unsing the link above the code (»Open in Writelatex«). You also might find the example pictures of package mwe helpful.
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
Post Reply