使用 subfig 垂直居中对齐图片和小页面

使用 subfig 垂直居中对齐图片和小页面

我试图将 3 张带子编号的图片放在 3 张未编号的表格旁边,并使每张图片的中心与其中一张表格对齐。为此,我使用包subfig和 minipages 来放置表格,否则它们会被编号,或者图片的编号为 ace 而不是 ab c。

为了方便我书写,我制作了一个命令,如果可以找到图片,则包含图片;如果找不到,则包含带有错误文本的小页面。当找不到图片时,居中对齐可以正常工作,如图 1 所示,但当找到图片时,图片的底部与表格的中心对齐,如图 2 所示。

我该如何解决这个问题?

\documentclass[a4paper, 10pt]{report}

\usepackage[utf8]{inputenc} 
\usepackage[english]{babel}
\usepackage{subfig}
\usepackage[pdftex]{graphicx}
\usepackage{multicol}

\newcommand{\includegraphicsmaybe}[2]{\IfFileExists{#2}{\includegraphics[width=#1]{#2}}{\fbox{\parbox[c][#1][c]{#1}{\center \textbf{File not found}\\ #2} }}}



\begin{document}

\begin{figure}[h!bt]
\centering
\subfloat[][Single-phase]{\includegraphicsmaybe{.4\textwidth}{../Pictures/SingleTrafo.jpg}\label{fig:SingleTrafo}}
\begin{minipage}[c][][c]{.5\textwidth}
    \hspace{5mm}\begin{tabular}{| r l |}
    \hline
    \multicolumn{2}{| c |}{\textbf{Specifications}} \\
    \hline
    Power & 800 VA \\
    Frequency & 50 -- 60 Hz \\
    Primary & 127 -- 220 V \\
    Secondary & 127 V / 5.2 A \\
    Tertiary & 42 -- 73 V / 1.8 A \\
    \hline
    \end{tabular}
\end{minipage}
\\
\subfloat[][Three-phase three-legged]{\includegraphicsmaybe{.4\textwidth}{../Pictures/3LegTrafo.jpg}\label{fig:3LegTrafo}}
\begin{minipage}[c][][c]{.5\textwidth}
    \hspace{5mm}\begin{tabular}{| r l |}
    \hline
    \multicolumn{2}{| c |}{\textbf{Specifications}} \\
    \hline
    Power & 2400 VA \\
    Frequency & 50 -- 60 Hz \\
    Primary & 127 -- 220 V \\
    Secondary & 127 V / 5.2 A \\
    Tertiary & 42 -- 73 V / 1.8 A \\
    \hline
    \end{tabular}
\end{minipage}
\\
\subfloat[][Three-phase five-legged]{\includegraphicsmaybe{.4\textwidth}{../Pictures/5LegTrafo.jpg}\label{fig:5LegFrafo}}
\begin{minipage}[c][][c]{.5\textwidth}
    \hspace{5mm}\begin{tabular}{| r l |}
    \hline
    \multicolumn{2}{| c |}{\textbf{Specifications}} \\
    \hline
    Power & 2400 VA \\
    Frequency & 50 -- 60 Hz \\
    Primary & 127 -- 220 V \\
    Secondary & 127 V / 5.2 A \\
    Tertiary & 42 -- 73 V / 1.8 A \\
    \hline
    \end{tabular}
\end{minipage}
\caption{transformers.}
\label{fig:Trafos}
\end{figure}


\end{document}

未找到图片,正在对齐 图1

找到图片,但未对齐 图 2

答案1

添加adjustbox包裹在你的序言中添加export选项,然后使用

\includegraphics[valign=c,..]{...}

这是一个简单的例子:

在此处输入图片描述

\documentclass{article}

\usepackage{subfig}
\usepackage[export]{adjustbox}

\newcommand{\includegraphicsmaybe}[2]{%
  \IfFileExists{#2}
    {\includegraphics[valign=c,width=#1]{#2}}
    {\fbox{\parbox[c][#1][c]{#1}{\centering\textbf{File not found}\\ #2} }}}

\begin{document}

\begin{figure}[h!bt]
  \centering
  \subfloat[][Single-phase]{\includegraphicsmaybe{.4\textwidth}{example-image-a}}
  \begin{minipage}[c][][c]{.5\textwidth}
    \hspace{5mm}\begin{tabular}{| r l |}
    \hline
    \multicolumn{2}{| c |}{\textbf{Specifications}} \\
    \hline
    Power & 800 VA \\
    Frequency & 50 -- 60 Hz \\
    Primary & 127 -- 220 V \\
    Secondary & 127 V / 5.2 A \\
    Tertiary & 42 -- 73 V / 1.8 A \\
    \hline
    \end{tabular}
  \end{minipage}

  \subfloat[][Three-phase three-legged]{\includegraphicsmaybe{.4\textwidth}{example-image-b}}
  \begin{minipage}[c][][c]{.5\textwidth}
    \hspace{5mm}\begin{tabular}{| r l |}
    \hline
    \multicolumn{2}{| c |}{\textbf{Specifications}} \\
    \hline
    Power & 2400 VA \\
    Frequency & 50 -- 60 Hz \\
    Primary & 127 -- 220 V \\
    Secondary & 127 V / 5.2 A \\
    Tertiary & 42 -- 73 V / 1.8 A \\
    \hline
    \end{tabular}
  \end{minipage}

  \subfloat[][Three-phase five-legged]{\includegraphicsmaybe{.4\textwidth}{example-image-c}}
  \begin{minipage}[c][][c]{.5\textwidth}
    \hspace{5mm}\begin{tabular}{| r l |}
    \hline
    \multicolumn{2}{| c |}{\textbf{Specifications}} \\
    \hline
    Power & 2400 VA \\
    Frequency & 50 -- 60 Hz \\
    Primary & 127 -- 220 V \\
    Secondary & 127 V / 5.2 A \\
    Tertiary & 42 -- 73 V / 1.8 A \\
    \hline
    \end{tabular}
  \end{minipage}
  \caption{transformers.}
\end{figure}

\end{document}

相关内容