3

I have searched around for two hours without luck. So I hope someone can help me this issue. Basically I want to place two different figures side by side to save space in my report. So I don't want subfigures with common caption here.

I tried using minipage as follows:

\begin{figure}[ht]
\begin{minipage}[b]{.5\textwidth}
\centering
\includegraphics[width=1\textwidth]{figs/test1.png}
\caption{This is caption of test1.}
\label{fig:HOG_resolution}
\end{minipage}
\hfill
\begin{minipage}[b]{.4\textwidth}
\centering
\includegraphics[width=1\textwidth]{figs/test2.png}
\caption{This caption for test 2 is longer than the caption in test1. It should spread on two lines}
\label{fig:block_overlap}
\end{minipage}
\end{figure}

My figures show up fine, except for the captions. The first caption is missing, the second one shows up at the position of the first one. enter image description here

My preamble is as follows:

\documentclass[11pt]{report}

\usepackage{graphics}
\usepackage{graphicx}
\usepackage{caption}
\usepackage[caption=false]{subfig}
\usepackage{float}

\usepackage{multirow}
\restylefloat{figure}
\usepackage[top=2.5cm, bottom=2.5cm, left=2.75cm, right=2.75cm]{geometry}

\usepackage[titles,subfigure]{tocloft}

\usepackage[bookmarks]{hyperref}

\lstnewenvironment{code}[1][]
  {\minipage{\linewidth} 
   \lstset{#1}}
  {\endminipage}

\newcommand{\myvec}[1]{\bf{#1}}
\newcommand{\mymtrx}[1]{\bf{#1}}

I also tried \captionof instead of \caption (u probably see I put \usepackage{caption} at my preamble) but it doesn't work. I also use package subfig because I want to create subfigures side by side as well, and I dont have problem with that.

  • You can narrow the problem down and encourage people to help by creating a Minimal Working Example i.e. the smallest amount of code you can manage which, when compiled, demonstrates the problem. This is likely to assist people trying to help you - very easy to reproduce the problem and try out possible solutions - and, therefore, increase the usefulness of the answers you receive. Right now you have something closer to a Maximal Non-Working Example :(. – cfr Jan 6 '14 at 3:10
  • @cfr: I thought I did. I showed the full preamble. Though it is quite long I fear it may contain some lines that cause the problem,. I also showed the code part that does not work properly and the result. If someone wants to reproduce it, just put them together plus the line \begin{document} and \end{document} at the end, also change graphicx to [demo]{graphicx}. Then they should see the same result – Tu Bui Jan 6 '14 at 3:19
  • But you can help yourself by testing to see which bits of that preamble are involved and which are irrelevant. Moreover, posting something people can compile means posting something with those bits prepared so that the work only gets done once and doesn't have to be repeated by every person who tries to investigate the problem. – cfr Jan 6 '14 at 3:22
  • \restylefloat definitely seems to be part of the problem. By the way, why do you load both graphics and graphicx? If I take \restylefloat out, it works OK. – cfr Jan 6 '14 at 3:26
  • cfr, you are rite. I will narrow down the post. Also thank you for pointing out the \restylefloat command. It is really the cause of the problem – Tu Bui Jan 6 '14 at 16:25
6

This code demonstrates the problem:

\documentclass{report}

\usepackage[demo]{graphicx}
\usepackage{float}

\restylefloat{figure}

\begin{document}

\begin{figure}[ht]
\begin{minipage}[b]{.5\textwidth}
\centering
\includegraphics[width=1\textwidth]{figs/test1.png}
\caption{This is caption of test1.}
\end{minipage}
\hfill
\begin{minipage}[b]{.4\textwidth}
\centering
\includegraphics[width=1\textwidth]{figs/test2.png}
\caption{This caption for test 2 is longer than the caption in test1. It should spread on two lines}
\end{minipage}
\end{figure}

\end{document}

The rest of the preamble seems not to be implicated.

I am not sure what else you want but using the starred version of \restylefloat apparently tells the package to leave the captions alone and this gets you two captions rather than one:

\documentclass{report}

\usepackage[demo]{graphicx}
\usepackage{float}

\restylefloat*{figure}

\begin{document}

\begin{figure}[ht]
\begin{minipage}[b]{.5\textwidth}
\centering
\includegraphics[width=1\textwidth]{figs/test1.png}
\caption{This is caption of test1.}
\end{minipage}
\hfill
\begin{minipage}[b]{.4\textwidth}
\centering
\includegraphics[width=1\textwidth]{figs/test2.png}
\caption{This caption for test 2 is longer than the caption in test1. It should spread on two lines}
\end{minipage}
\end{figure}

\end{document}

two captions

4

With the help of caption package, a better alignment is attempted. Here two \newline commands are added to the shorter caption, according to the length of the longer caption.

Note: Using [t] alignment can have the same effect, though.

If the figure caption is to be left aligned, use only one newline and add

\captionsetup{labelsep=space,justification=justified,singlelinecheck=off}

will yield the second image. (If the : is preferred, remove labelsep=space.)

enter image description here

enter image description here

Code:

\documentclass[]{article}
\usepackage[margin=1in]{geometry}
\usepackage[demo]{graphicx}
\usepackage{caption}
\begin{document}

\begin{figure}[ht]
\begin{minipage}[b]{.45\textwidth}
\centering
\includegraphics[width=1\textwidth]{figs/test1.png}
\caption{This is caption of test1.\newline\newline}
\label{fig:HOG_resolution}
\end{minipage}
\hfill
\begin{minipage}[b]{.45\textwidth}
\centering
\includegraphics[width=1\textwidth]{figs/test2.png}
\caption{This caption for test 2 is longer than the caption in test1. It should spread on two lines.}
\label{fig:block_overlap}
\end{minipage}
\end{figure}

\end{document}
1

cfr's answer did it for me, although I used tables. Was stuck on this issue for quite some time, and the addition of \restylefloat*{table} solved the issue I was having with side-by-side tables (in a two-column environment) not rendering one of the captions. For brevity's sake, I've attached source code for tables below.

\restylefloat*{table}
\begin{table*}
\parbox{.5\linewidth}{
\centering
\caption{Tabular 1.}
\label{tab:tabular1}
\begin{tabular}{ l c r }
  1 & 2 & 3 \\
  4 & 5 & 6 \\
  7 & 8 & 9 \\
\end{tabular}
}
\hfill
\parbox{.5\linewidth}{
\centering
\caption{Tabular 2.}
\label{tab:tabular2}
\begin{tabular}{ l c r }
  1 & 2 & 3 \\
  4 & 5 & 6 \\
  7 & 8 & 9 \\
\end{tabular}
}
\end{table*}
  • 1
    The original question was about figures and not tables, this does not answer the question really, besides, it is some code borrowed from another answer to the question. – user31729 Jul 20 '14 at 10:02
  • It is most certainly not. The code featured in the answer uses \parbox to accomplish side-by-side float environments, whereas other answers use minipages. In what sense is the code borrowed? While the original question was about figures, I thought the addition of a table example would be complementary to other answers. – Caterpillar Jul 20 '14 at 13:39
  • It was approved, so it seems to alright, the downvote is not by me, however. So, please don't mind. – user31729 Jul 20 '14 at 13:46

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.