# Frame (fbox) around all minipages/includegraphics/

To fine-tune figures with many panels etc., or also beamer slides, it would sometimes come in handy to quickly see the outlines of minipages, graphics, etc. This can be achieved by manually wrapping the respective objects in \fbox{} or -- in beamer -- \frame{} commands. However, that's tedious.

I imagine the simplest solution would be to redefine minipage, includegraphics, subfigure, etc. in the header such that they are placed in an \fbox{} and thus are framed throughout the document. These redefinitions could then simply be uncommented whenever frames are desired. Of course, all options etc. should still be supported. How can this be achieved?

Following is an example with two figures, first the original, then with manual fboxes. The goal is that with redefined commands, the first figure looks like the second.

\documentclass{article}
\usepackage[showframe]{geometry}

\usepackage{graphicx}
\usepackage{subcaption}

\setlength{\fboxsep}{0pt}

\newlength{\h}

\begin{document}

\setlength{\h}{0.5\linewidth}

\begin{figure}\centering
%
\begin{subfigure}[t]{0.02\linewidth}
\rotatebox{90}{\begin{minipage}{\h}\centering\textbf{first row}\end{minipage}}\\
\rotatebox{90}{\begin{minipage}{\h}\centering\textbf{first row}\end{minipage}}
\end{subfigure}
%
\begin{subfigure}[t]{0.45\linewidth}\centering
\includegraphics[width=2in]{example-image}\\
\includegraphics[width=2in]{example-image}
\caption*{\textbf{first column}}
\end{subfigure}
\hfill
%
\begin{subfigure}[t]{0.45\linewidth}\centering
\includegraphics[width=2in]{example-image}\\
\includegraphics[width=2in]{example-image}
\caption*{\textbf{second column}}
\end{subfigure}
%
\caption{\ldots}
\end{figure}

\begin{figure}\centering
%
\fbox{
\begin{subfigure}[t]{0.02\linewidth}
\rotatebox{90}{\fbox{\begin{minipage}{\h}\centering\textbf{first row}\end{minipage}}}\\
\rotatebox{90}{\fbox{\begin{minipage}{\h}\centering\textbf{second row}\end{minipage}}}
\end{subfigure}
}
%
\fbox{
\begin{subfigure}[t]{0.45\linewidth}\centering
\fbox{\includegraphics[height=\h]{example-image}}\\
\fbox{\includegraphics[height=\h]{example-image}}
\caption*{\textbf{first column}}
\end{subfigure}
}
\hfill
%
\fbox{
\begin{subfigure}[t]{0.45\linewidth}\centering
\fbox{\includegraphics[height=\h]{example-image}}\\
\fbox{\includegraphics[height=\h]{example-image}}
\caption*{\textbf{second column}}
\end{subfigure}
}
%
\caption{\ldots}
\end{figure}

\end{document}


Edit:

The solution for \includegraphics is as follows (thanks @Skillmon for pointing me to it):

\let\includegraphicsbak\includegraphics
\renewcommand*{\includegraphics}[2][]{\fbox{\includegraphicsbak[#1]{#2}}}


How can the same be achieved for command blocks (minipage, subfigure)?

• Take a look here, there it is done for \includegraphics. Jul 4 '17 at 12:24
• You should use example-image, example-image-a or example-image-b for the images in your MWEs. Those are installed with the mwe-package (included in both TeXLive and MikTeX) and therefore installed on close to every PC running LaTeX. (I've edited your question changing that) Jul 4 '17 at 12:31
• Thanks. I wanted to use these initially, but they didn't work on my computer (aparentlly mwe is not installed). Jul 4 '17 at 12:35
• The \usepackage[export]{adjustbox} shouldn't be necessary if you use the \fbox{\includegraphicsbak...}-approach. Jul 4 '17 at 12:42
• If you really want to use the boxing approach for editing, you should perhaps consider setting the \fboxsep=-\fboxrule (just inside the environments and commands you want to alter), as this way it shouldn't add any extra space. Jul 4 '17 at 14:57

A solution that should work for minipage. The \begin{minipage}...\end{minipage} is (roughly) equivalent to

\begingroup
\minipage
...
\endminipage
\endgroup


so you need to save both \minipageand \endminipage with \let in the same way as for \includegraphics. To make an environment that sets the minipage in a frame you need to put it in a box. For this the lrbox environment puts the contents in a box and removes one level of grouping. Below I first use the default minipage and then redefines it (normally the definitions should be before \begin{document}). It should also work with the optional argument.

\documentclass{article}
\usepackage{lipsum}
\begin{document}

\begin{minipage}[]{0.8\linewidth}
\lipsum[1]
\end{minipage}

\let\minipagebak\minipage
\let\endminipagebak\endminipage
\newsavebox\TestBox
\renewenvironment{minipage}[2][]
{\begin{lrbox}{\TestBox}\begin{minipagebak}[#1]{#2}}
{\end{minipagebak}\end{lrbox}\fbox{\usebox{\TestBox}}}

\begin{minipage}[t]{0.8\linewidth}
\lipsum[1]
\end{minipage}

\end{document}


• You don't necessarily need lrbox, it works with \newbox\TestBox and \setbox\TestBox\hbox\bgroup...\egroup, too Jul 4 '17 at 14:50
• @Skillmon, Yes, that should work also. It is close to the definition of lrbox. Jul 4 '17 at 18:00