Forzar que las subfiguras tengan la misma altura y tomen el X% general del ancho de línea en LaTeX

Forzar que las subfiguras tengan la misma altura y tomen el X% general del ancho de línea en LaTeX

Quiero hacer una figura compuesta por dos imágenes que tengan diferentes tamaños. Quiero colocarlos uno al lado del otro para que tengan la misma altura y que en total ocupen, digamos, el 90% del ancho de la línea.

Es fácil hacer que tengan la misma altura fija (digamos en cm), pero ¿cómo permitir que esta altura común se ajuste automáticamente para cumplir con el ancho total deseado? El método de prueba y error manual requiere mucho tiempo, es aproximado y no es sólido.

Una solución con o sin subfigureotro paquete LaTeX está bien.

Respuesta1

Puede incluirlos a la misma altura (más o menos arbitraria) y luego escalarlos juntos al ancho deseado.

ingrese la descripción de la imagen aquí

\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}

Respuesta2

Puede utilizar el subcaptionpaquete y hacer el cálculo 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}

ingrese la descripción de la imagen aquí

Respuesta3

\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}

Estableces el ancho total que deseas que tomen las imágenes y el espacio entre ellas \setsubfloatswidths, luego llamas \adjincludegraphicsdentro del figureentorno con los dos archivos de imagen como argumentos y finalmente usas subfiguras como de costumbre.

\firstfactorcontiene el factor que escala la primera imagen y \secondfactorhace lo mismo con la segunda.

Una solución más sencilla podría haber sido establecer la altura de la imagen, pero \subcaptionel subfigureentorno lo toma como argumento.la anchuradel subflotador.

información relacionada