0

To put it simply, I just want to add subcaptions to the subfigures showed in the accepted answer of this question:

Forcing subfigures to have same height and take overall X% of linewidth in LaTeX

This is how I've tried to do it:

\documentclass[a4paper,10pt]{book}

\usepackage{subcaption}
\usepackage{graphicx}
\begin{document}

\begin{figure}
\centering

\resizebox{.9\textwidth}{!}{
  \includegraphics[height=3cm]{example-image-a}
%  \subcaption{Example image A.}
  \quad
  \includegraphics[height=3cm]{example-image-16x9}
%  \subcaption{Example image 16x9.}
}
\caption{Example images.} 
\end{figure}
\end{document}

When I uncomment the subcaption lines, I get the following error:

You can't use `\hrule' here except with leaders. \caption@hrule ->\hrule \@height \z@ l.13 }

Edit:

Incidentally, one of the answers (not the accepted one) to Forcing subfigures to have same height and take overall X% of linewidth in LaTeX solves my problem. From the beginning, I cited that question in this question text and said what I want in addition to its accepted answer.

  • See if \begin{subfigure}{<desired width>} ... \caption{...}\end{subfigure} work for you. – Zarko Jan 25 '16 at 17:48
  • The problem is that I don't have a pre-defined "desired width". I want the compiler to calculate the width by considering that the subfigures must have equal height and a defined overall width (width A plus width B). – Leonardo Castro Jan 25 '16 at 18:16
  • If you all this know in advance, why then you not use in includegraphics options, i. e : |[width = .., height= ...]? At least, what you had to do is wrap includegraphic and belonging subcaption into minipage with prescribed width. and don't use resizebox. – Zarko Jan 25 '16 at 18:24
  • @Zarko I'm not sure that I understand you. I don't want to define both width and height because by doing so I would either change images' proportions or have to calculate the right widths myself. Also, I want a general method that will work for any pair of figures in any proportions. Let the widths be w_A, w_B and the heights be h_A, h_B. I want to define an overall width W, then the w's and h's must be calculated so that h_A = h_B and w_A + w_B = W. – Leonardo Castro Jan 25 '16 at 18:44
  • 1
    @Zarko Actually, David Carlisle solution for http://tex.stackexchange.com/questions/218378/forcing-subfigures-to-have-same-height-and-take-overall-x-of-linewidth-in-latex does exactly what I want in regards to subfigures' dimensions. Now, I just need to put subcaptions below each one of them. – Leonardo Castro Jan 25 '16 at 18:48
  • @Zarko To be more precise, my system of equations is h_A = h_B, w_A + w_B = W, h_A = p_A * w_A, h_B = p_B * w_B, where the equation parameters are W (text width), p_A (height/width proportion of the original figure A) and p_B (h/w proportion of the original figure B). – Leonardo Castro Jan 25 '16 at 18:52
  • See, if my answer gives what you looking for. – Zarko Jan 25 '16 at 19:14

1 Answers1

2

See, if the following solution fulfill your expectations:

\documentclass[a4paper,10pt]{book}
    \usepackage{subcaption}
    \usepackage{graphicx}
    \newlength{\limage}
    \newlength{\rimage}

\usepackage{showframe}

    \begin{document}
\begin{figure}
\centering
\settowidth\limage{\includegraphics[height=3cm]{example-image-a}}
\settowidth\rimage{\includegraphics[height=3cm]{example-image-16x9}}
\resizebox{.9\textwidth}{!}{
    \begin{tabular}{p{\limage}p{\rimage}}
\includegraphics[height=3cm]{example-image-a}\newline
\subcaption{Example image A.}
    &   \includegraphics[height=3cm]{example-image-16x9}\newline
        \subcaption{Example image 16x9.}
    \end{tabular}
}
\caption{Example images.}
\end{figure}
    \end{document}

enter image description here

As you can see, your requirements can be fulfilled by measure wisth of your images at given heights. For this you need define two (or more new lenght registers (in preamble) and in figure first measure image width, this width use in tabular environments (or in \subfigure or in minipage) for determining column width. Further is straightforward.

Note: Defined length you can reuse in each figure environment. I you have in it more than two sub figures, than you need to define adequate number of length registers.

Zarko
  • 296,517
  • While we were discussing, I noted that the answer of "egreg" in
    http://tex.stackexchange.com/questions/218378/forcing-subfigures-to-have-same-height-and-take-overall-x-of-linewidth-in-latex also does the job, but I thought we could do that without that much preliminary code, because I want to reuse it many times in a document. Your solution still have preliminary code, but, as it's only two length definitions, I think I can live with that. :-)
    – Leonardo Castro Jan 25 '16 at 19:36
  • It is up to you, which solution you estimate to be more appropriate to you. It seems that your question is duplicate. – Zarko Jan 25 '16 at 19:57
  • The subcaption part is what makes my question different. One of the answers incidentally also answers my question. – Leonardo Castro Jan 26 '16 at 11:53