如何自動縮放子圖以使其具有相同的高度?

如何自動縮放子圖以使其具有相同的高度?

我有很多圖形要並排放在文件中,它們的大小都略有不同(即並非都是 4:3 左右的格式)。

我現在正在做的就是調整width=0.XX\textwidth兩者的高度,直到它們看起來差不多相同的高度。

在這裡我提供了一個範例程式碼:我希望能夠做的是有一個巨集或其他東西,我可以簡單地提供兩個數字,它們會自動縮放以具有相同的高度並適合在一行上。那可能嗎?如果是的話,怎麼辦?

編輯:因為人們問為什麼我不提供圖像的具體尺寸:程式碼應該與任何2 張任意長寬比的影像。給它兩個圖像,程式碼將它們縮放以並排放置具有相同的高度,填滿可用的水平空間。

\documentclass{scrreprt}
\usepackage{subfig}
\usepackage{graphicx}
\begin{document}
    \chapter{Figures}
    \begin{figure}[h!]
        \centering
        \subfloat[Figure one]{%
            \centering\includegraphics[width=0.45\textwidth]{example-image-a}}
        \qquad
        \subfloat[Figure two with different side proportions]{%
            \centering\includegraphics[width=0.45\textwidth]{example-image-16x9}}
        \caption{How to get the two figures to same height (respecting proportions)?}
    \end{figure}
\end{document}

我得到什麼:

我得到什麼

我希望自動發生的事情:

我想得到什麼

答案1

對於兩個範例圖以及scrreprt文件類別給定的預設文字寬度和高度,對於這兩個群組來說,用 ,width=...替換就足夠了。height=0.21\textheightsubfig

在此輸入影像描述

對於文字寬度、文字高度以及需要彼此相鄰放置的圖像對的比例的其他組合,您可能需要進行一些實驗才能找出哪個值是height=...正確的。

我假設目標是使圖形對盡可能大,即跨越文字區塊的整個寬度。如果此假設有效,則\centering不需要任何說明。

\documentclass{scrreprt}
\usepackage{subfig}
\usepackage{graphicx}
\begin{document}
\chapter{Figures}

\begin{figure}[h!]
\subfloat[Figure one]{%
\includegraphics[height=0.21\textheight]{example-image-a}}
\hspace*{\fill}
\subfloat[Figure two with different side proportions]{%
\includegraphics[height=0.21\textheight]{example-image-16x9}}
\caption{How to get the two figures to same height (respecting proportions)?}\end{figure}
\end{document}

答案2

(僅)定義它們的高度。喜歡\includegraphics[height=33mm]{example-image-a}}(根據您的意願選擇圖像高度)。

題外話:不要\centering在子浮點內部使用...

編輯:

就您而言,由於您希望在兩個圖像中使用相同的height密鑰Gin,因此在下面編輯的程式碼中如下所示。要盡可能將圖像分開,請使用\hfill代替quad

\documentclass{scrreprt}
\usepackage{subfig}
\usepackage{graphicx}

%---------------- show page layout. don't use in a real document!
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.15pt}
\renewcommand*\ShowFrameColor{\color{red}}
%---------------------------------------------------------------%
\begin{document}
    \chapter{Figures}
    \begin{figure}[h!]
    \setkeys{Gin}{height=44mm}
        \subfloat[Figure one]{\includegraphics{example-image-a}}
        \hfill% push sub images apart, so take all the line
        \subfloat[Figure two with different side proportions]{%
            \includegraphics{example-image-16x9}}
        \caption{How to get the two figures to same height (respecting proportions)?}
    \end{figure}
\end{document}

在此輸入影像描述

(紅線表示文字邊框)

答案3

要使兩個圖像達到相同的高度,您可以將第一個圖像的高度儲存在維度中\imageheight。然後您可以用於height=\imageheight第二張圖像。

這裡的罪魁禍首是您無法自動定義兩個圖像的寬度以盡可能適合文字寬度。也許使用 lua 的程式碼可以做到這一點,但直到現在我還沒有學會在 TeX 中使用 lua ...

所以讓我們半自動地進行...

讓我們看一下程式碼。序言中的一行

\newdimen\imageheight

我們聲明一個新的維度\imageheight來儲存第一個圖像的高度。用程式碼

\settoheight{\imageheight}{% <==========================================
  \includegraphics[width=0.40\textwidth,keepaspectratio]{example-image-a}%
}

您將獲得所選寬度的影像的目前高度width=0.40\textwidth

現在您可以使用程式碼

\subfloat[Figure one]{%
  \centering\includegraphics[height=\imageheight]{example-image-a}}
\qquad
\subfloat[Figure two with different side proportions]{%
  \centering\includegraphics[height=\imageheight]{example-image-16x9}}

以相同的高度列印圖像。要讓它們適合文字寬度,只需調整第一個圖像的寬度:width=0.40\textwidth0.40\textwidth根據您的需求變更 的值。

完整程式碼

\documentclass{scrreprt}

\usepackage{subfig}
\usepackage{graphicx}
\usepackage{showframe}

\newdimen\imageheight % to store the actual image height <==============


\begin{document}

\settoheight{\imageheight}{% <==========================================
  \includegraphics[width=0.40\textwidth,keepaspectratio]{example-image-a}%
}

\chapter{Figures}
\begin{figure}[h!]
  \centering
    \subfloat[Figure one]{%
      \centering\includegraphics[height=\imageheight]{example-image-a}} % <=============
    \qquad
    \subfloat[Figure two with different side proportions]{%
      \centering\includegraphics[height=\imageheight]{example-image-16x9}} % <==========
  \caption{How to get the two figures to same height (respecting proportions)?}
\end{figure}
\end{document}

給你結果:

在此輸入影像描述

相關內容