# How to add a box of a fixed size (width and height) around a graphic of varying sizes?

I've found lots of references for ways to create a box around text, but none for around graphics. I want the border frame around the graphic to all be the same size, with a graphic inside the box, centered vertically and horizontally, and a caption outside the box. This box would need to be able to be placed side by side so it effectively makes a grid.

EDIT: Here is a working demo

\documentclass{article}
\usepackage[demo]{graphicx}
\usepackage[letterpaper, margin=1in]{geometry}

\begin{document}
\setlength\unitlength{1in}

\framebox(2,2){\includegraphics[width=1.875in,height=1.875in,keepaspectratio]{infinity_80_gf_2010}}
\framebox(2,2){\includegraphics[width=1.875in,height=1.875in,keepaspectratio]{Plus80_2010}}
\framebox(2,2){\includegraphics[width=1.875in,height=1.875in,keepaspectratio]{comfort_achp_Puron}}

\bigskip

\framebox(2,2){\includegraphics[width=1.875in,height=1.875in,keepaspectratio]{Pref_Vent_VA}}
\framebox(2,2){\includegraphics[width=1.875in,height=1.875in,keepaspectratio]{infinity_98}}
\framebox(2,2){\includegraphics[width=1.875in,height=1.875in,keepaspectratio]{infinity_80_gf_2010}}

\end{document}


This is the result:

-
Please show use a use-case by means of a minimal working example (MWE). –  Werner Apr 30 '14 at 16:36

Anything you have read about boxing text applies to boxing images. The box commands do not care about what is in their content.

Here are two boxes which are the same size each containing an image horizontally and vertically centred:

\documentclass{article}
\usepackage[demo]{graphicx}

\begin{document}
\setlength\unitlength{1cm}

\framebox(4,2){\includegraphics[width=2cm,height=1cm]{aa}}

\bigskip

\framebox(4,2){\includegraphics[width=1cm,height=1.5cm]{aa}}

\end{document}

-
This is getting me close, I'm trying to get the subcaption to work now. It's displaying the images stacked on top of each other, but I'd like to display them side by side (3 on the top, 3 on the bottom). I'm using the subcaption package. I'd like to display the caption outside the box. –  CGRemakes Apr 30 '14 at 17:27
@CGRemakes questions should always include complete documents (as in my or Steven's answers) that makes it much easier to see what you want. just as the frame here doesn't care what you put in the frame the subfigure environment doesn't care what you put in the subfigure, subfigures will go side by side and linewrap if needed so you just need to make them 0.33\textwidth and put 6 in a row and you'll get that 3x2 layout. That is unrelated to if you put framed images in the (sub) figure. –  David Carlisle Apr 30 '14 at 18:51
OK, posted a working demo in my original post. –  CGRemakes Apr 30 '14 at 22:34

Here's one way. The frame size is settable with \fwd and \fht.

\documentclass{article}
\usepackage[demo]{graphicx}
\newlength\fwd
\newlength\fht
\fwd 3cm
\fht 2cm
\newlength\tmplen
\newcommand\inframe[2][]{%
\fboxsep = -\fboxrule\fbox{%
\rule[-.5\fht]{0pt}{\fht}\makebox[\fwd]{%
\setbox0=\hbox{\includegraphics[#1]{#2}}%
\tmplen -.5\ht0\relax\raisebox{\tmplen}{\smash{\box0}}}}\kern-\fboxrule}
\usepackage[usestackEOL]{stackengine}
\setstackgap{S}{-\fboxrule}
\begin{document}
\Shortstack{%
\inframe[width=1cm,height=1.5cm]{A.pdf}%
\inframe[width=1.5cm,height=0.8cm]{B.pdf}%
\inframe[width=2cm,height=1.2cm]{C.pdf}\\
\inframe[width=1.5cm,height=0.8cm]{B.pdf}%
\inframe[width=1cm,height=1cm]{A.pdf}%
\inframe[width=1.8cm,height=1.5cm]{C.pdf}
}
\end{document}


Using the more compact syntax that David's answer provides, if incorporated into this solution, would require a few additional changes to my MWE:

\documentclass{article}
\usepackage[demo]{graphicx}
\usepackage[usestackEOL]{stackengine}
\setlength\unitlength{1cm}
\def\fwd{3}
\def\fht{2}
\newcommand\inframe[2][]{%
\framebox(\fwd,\fht){\includegraphics[#1]{#2}}\kern\fboxrule}
\setstackgap{S}{\fboxrule}
\begin{document}
\Shortstack{%
\inframe[width=1cm,height=1.5cm]{A.pdf}%
\inframe[width=1.5cm,height=0.8cm]{B.pdf}%
\inframe[width=2cm,height=1.2cm]{C.pdf}\\
\inframe[width=1.5cm,height=0.8cm]{B.pdf}%
\inframe[width=1cm,height=1cm]{A.pdf}%
\inframe[width=1.8cm,height=1.5cm]{C.pdf}
}
\end{document}

-