1

I often put two floats side by side using the minipage environment: it can be two texts (typically excerpts from books or press articles), two pictures (a map, a photography or a chart) or one of each (eg. a map and an excerpt from press article).

One recurring problem I encounter is to set the height of those two minipages so that the whole \textwidth is used and have as close as possible heights. This is a tedious trial and error process that I would like to automate.

I have seen several solutions on this website solving this problem for figures of which you know the dimensions and ratio in advance (typically pictures) but not for texts.

Here is a MWE with two examples where heights are in my opinion similar enought.

\documentclass {article}
\usepackage{mwe}
\begin{document}
\begin{figure}
    \begin{minipage}[t]{.58\linewidth}
            \lipsum[1][1-8]
        \caption{Some caption}
    \end{minipage}\hfill
    \begin{minipage}[t]{.38\linewidth}
            \lipsum[1][1-5]
        \caption{Some other caption}
    \end{minipage}\hfill
\end{figure}

\begin{figure}
    \begin{minipage}[t]{.53\linewidth}
        \strut\vspace*{-\baselineskip}\newline\includegraphics[width=\linewidth]{example-image-a}
        \caption{Third caption}
    \end{minipage}\hfill
    \begin{minipage}[t]{.43\linewidth}
        \lipsum[1][1-8]
        \caption{Fourth caption}    
    \end{minipage}
\end{figure}
\end{document}
0

1 Answer 1

2

If you only want to rescale images, no problem. Just measure the height of the images and solve a*\ht0=b*\ht1 and a*\wd0+b*\wd1=\textwidth.

\documentclass {article}
\usepackage{adjustbox}
\usepackage{graphicx}
\usepackage{pgfmath}

\begin{document}
\begin{figure}
  \sbox0{\includegraphics{example-image-16x10}}%
  \sbox1{\includegraphics{example-image-10x16}}%
  \pgfmathsetmacro{\sclB}{\textwidth/((\ht1/\ht0)*\wd0+\wd1)}%
  \pgfmathsetmacro{\sclA}{\textwidth/(\wd0+(\ht0/\ht1)*\wd1)}%
  \begin{minipage}[t]{\sclA\wd0}
    \scalebox{\sclA}{\usebox0}
    \caption{Some caption}
  \end{minipage}%
  \begin{minipage}[t]{\sclB\wd1}
    \scalebox{\sclB}{\usebox1}
    \caption{Some other caption}
  \end{minipage}
\end{figure}
\end{document}

Text is nastier. We assume the the total area of the text remains (approximately) constant. Therefore the heights and width are inversely proportional. However the process is very non-linear and a loop will be needed for convergence.

\documentclass {article}
\usepackage{pgfmath}
\usepackage{lipsum}
\usepackage{showframe}

\begin{document}
\begin{figure}
\setlength{\dimen0}{\dimexpr 0.5\textwidth-0.5\columnsep}%
\dimen1=\dimen0
\loop
  \sbox0{\begin{minipage}[b]{\dimen0}
    \lipsum[1]\strut
    \caption{Some caption}
  \end{minipage}}%
  \sbox1{\begin{minipage}[b]{\dimen1}
    \lipsum[2]\strut
    \caption{Some other caption}
  \end{minipage}}%
  \count1=\numexpr \ht0/\baselineskip\relax
  \count2=\numexpr \ht1/\baselineskip\relax
\ifnum\count1=\count2 \else
  \pgfmathsetmacro{\areaA}{(\ht0/\baselineskip)*\wd0}%
  \pgfmathsetmacro{\areaB}{(\ht1/\baselineskip)*\wd1}%
  \pgfmathparse{(\textwidth-\columnsep)/(1+(\areaA/\areaB))}%
  \dimen1=\pgfmathresult pt\relax
  \pgfmathparse{(\textwidth-\columnsep)/(1+(\areaB/\areaA))}%
  \dimen0=\pgfmathresult pt\relax
  \addtocounter{figure}{-2}%
\repeat
\usebox0\hfill\usebox1
\end{figure}
\end{document}

The third case is where one attempts to scale an image to the same height as text. One must solve ra=A/b where a and b are the widths for the image and text (a+b=\mywidth), r is the aspect ratio of the image (height/width) and A is the area of the text. This means solving a quadratic. If a solution exists, we always want the smaller one.

\documentclass {article}
\usepackage{adjustbox}
\usepackage{graphicx}
\usepackage{pgfmath}
\usepackage{lipsum}
\usepackage{showframe}

\newif\ifnosolution
\newlength{\mywidth}

\begin{document}
\begin{figure}
  \sbox0{\includegraphics{example-image-10x16}}%
  \pgfmathsetmacro{\aspect}{\ht0/\wd0}%
  \setlength{\mywidth}{\dimexpr \textwidth-\columnsep}%
  \dimen0=0.5\mywidth
  \dimen1=\dimen0
  \nosolutionfalse
\loop
  \sbox1{\begin{minipage}[b]{\dimen1}
    \lipsum[2]\strut
  \end{minipage}}%
  \dimen3=\aspect\dimen0 
  \count1=\numexpr \dimen3/\baselineskip\relax
  \count2=\numexpr \ht1/\baselineskip\relax
  \ifnum\count1=\count2
    \nosolutiontrue
  \else
    \pgfmathsetmacro{\area}{(\ht1/\mywidth)*(\wd1/\mywidth)}%
    \pgfmathsetmacro{\square}{1-4*\area/\aspect}%
    \ifdim\square pt<0pt\relax
      \nosolutiontrue
    \else\pgfmathparse{(1-sqrt(\square))*0.5\mywidth}%
      \setlength{\dimen0}{\pgfmathresult pt}%
      \setlength{\dimen1}{\dimexpr \mywidth-\dimen0}%
    \fi
  \fi
\ifnosolution\else\repeat
  \begin{minipage}[t]{\dimen1}% align bottom of box1 and box0
    \usebox1
    \caption{Some caption}
  \end{minipage}\hfill
  \begin{minipage}[t]{\dimen0}
    \resizebox{\linewidth}{!}{\usebox0}
    \caption{Some other caption}
  \end{minipage}
\end{figure}
\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .