LaTeX でサブ図の高さを同じにし、全体の線幅を X% にするように強制する

LaTeX でサブ図の高さを同じにし、全体の線幅を X% にするように強制する

異なるサイズの 2 つの画像で構成される図を作成したいと考えています。 2 つの画像を同じ高さで並べ、全体として線幅の 90% を占めるようにします。

同じ固定高さ(たとえば cm 単位)にするのは簡単ですが、この共通の高さを、希望する全体の幅を満たすように自動的に調整するにはどうすればよいでしょうか。手動での試行錯誤は時間がかかり、近似値で堅牢ではありません。

subfigure別の LaTeX パッケージの有無にかかわらず、ソリューションは OK です。

答え1

同じ(多かれ少なかれ任意の)高さに含めてから、希望の幅に拡大縮小することができます。

ここに画像の説明を入力してください

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

答え2

subcaptionパッケージを使用して、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}

ここに画像の説明を入力してください

答え3

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

を使用して、画像の合計幅と画像間の間隔を設定し、環境内で2 つの画像ファイルを引数として\setsubfloatswidths呼び出し、最後に通常どおりサブ図を使用します。\adjincludegraphicsfigure

\firstfactor\secondfactor最初の画像を拡大縮小し、 2 番目の画像に対しても同じことを行う係数が含まれます。

もっと簡単な解決策としては、画像の高さを設定することですが、\subcaption環境はsubfigure引数として受け取りますサブフロートの。

関連情報