Forçando as subfiguras a terem a mesma altura e ocuparem X% da largura de linha no LaTeX

Forçando as subfiguras a terem a mesma altura e ocuparem X% da largura de linha no LaTeX

Quero fazer uma figura composta por duas imagens de tamanhos diferentes. Quero colocá-los lado a lado para que tenham a mesma altura e que no geral ocupem, digamos, 90% da largura da linha.

É fácil fazer com que tenham a mesma altura fixa (digamos em cm), mas como deixar que esta altura comum seja ajustada automaticamente para cumprir a largura total desejada? A tentativa e erro manual é demorada, aproximada e não robusta.

Uma solução com ou sem subfigureoutro pacote LaTeX está OK.

Responder1

Você pode incluí-los na mesma altura (mais ou menos arbitrária) e depois dimensioná-los juntos para a largura desejada

insira a descrição da imagem aqui

\documentclass{article}

\usepackage{graphicx}% images from mwe package

\begin{document}

\noindent X\dotfill X

\begin{center}
\resizebox{.9\textwidth}{!}{%
\includegraphics[height=3cm]{example-image-a}%
\quad
\includegraphics[height=3cm]{example-image-16x9}%
}
\end{center}

\end{document}

Responder2

Você pode usar o subcaptionpacote e fazer os cálculos conforme sugerido por David.

\documentclass{article}

\usepackage{graphicx}% images from mwe package
\usepackage{subcaption}

\newlength{\twosubht}
\newsavebox{\twosubbox}

\begin{document}

\noindent\hrulefill The text width\hrulefill

\begin{center}
\makebox[.9\textwidth]{\hrulefill 90\% of text width\hrulefill}
\end{center}

\begin{figure}[htp]

% preliminary
\sbox\twosubbox{%
  \resizebox{\dimexpr.9\textwidth-1em}{!}{%
    \includegraphics[height=3cm]{example-image-a}%
    \includegraphics[height=3cm]{example-image-16x9}%
  }%
}
\setlength{\twosubht}{\ht\twosubbox}

% typeset

\centering

\subcaptionbox{First\label{f}}{%
  \includegraphics[height=\twosubht]{example-image-a}%
}\quad
\subcaptionbox{Second\label{s}}{%
  \includegraphics[height=\twosubht]{example-image-16x9}%
}

\caption{The caption}

\end{figure}

\end{document}

insira a descrição da imagem aqui

Responder3

\documentclass[a4paper,12pt]{article}
\usepackage{graphicx}
\usepackage{adjustbox}
\usepackage{fp}
\usepackage{subcaption}

\newlength{\totalimgwidth}
\newlength{\imgspacingwidth}

\newlength{\firstimgorigwidth}
\newlength{\firstimgorigheight}
\newlength{\secondimgorigwidth}
\newlength{\secondimgorigheight}
\newlength{\firstimgwidth}
\newlength{\secondimgwidth}

\newcommand{\setsubfloatwidths}[2]{%set the total width you want the images take and the spacing between them
\setlength{\totalimgwidth}{#1}%
\setlength{\imgspacingwidth}{#2}%
\addtolength{\totalimgwidth}{-\imgspacingwidth}%
}


\begin{document}
\setsubfloatwidths{0.9\textwidth}{1ex} %set the total width of figure and spacing inbetween
\begin{figure}
\adjincludegraphics[gstore width=\firstimgorigwidth,gstore height=\firstimgorigheight,gobble]{img1}%
\adjincludegraphics[gstore width=\secondimgorigwidth,gstore height=\secondimgorigheight,gobble]{img2}%
\makeatletter%
\FPdiv\firstaspectratio{\strip@pt\firstimgorigheight}{\strip@pt\firstimgorigwidth}%
\FPdiv\secondaspectratio{\strip@pt\secondimgorigheight}{\strip@pt\secondimgorigwidth}%
\FPeval\firstfactor{\secondaspectratio / (\firstaspectratio + \secondaspectratio)}%
\FPeval\secondfactor{\firstaspectratio / (\firstaspectratio + \secondaspectratio)}%
\makeatother%
\begin{subfigure}{\firstfactor\totalimgwidth}
\includegraphics[width=\textwidth]{img1}
\end{subfigure}
\hspace*{\imgspacingwidth}
\begin{subfigure}{\secondfactor\totalimgwidth}
\includegraphics[width=\textwidth]{img2}
\end{subfigure}
\end{figure}
\end{document}

Você define a largura total que deseja que as imagens tenham e o espaçamento entre elas com \setsubfloatswidths, depois chama \adjincludegraphicsdentro do figureambiente com os dois arquivos de imagem como argumentos e, finalmente, usa subfiguras como de costume.

\firstfactorcontém o fator que dimensiona a primeira imagem e \secondfactorfaz o mesmo para a segunda.

Uma solução mais simples poderia ter sido definir a altura da imagem, mas \subcaptiono subfigureambiente de toma como argumentoa largurado subflutuador.

informação relacionada