# How can I add vertical space between text and a minipage figure for all minipages?

I'm writing a instruction manual, I can't use figures because the images need to be directly underneath the step they are showing. To get the images to stay underneath the corresponding text, I am using the following code whenever I want to place an image.

\begin{minipage}{1\linewidth}
\vspace{5mm}
\centering
\includegraphics[width=1\textwidth]{database}
\captionof{figure}{Desktop icon of simulator app}
\end{minipage}

The thing that I don't like (as a programmer and a human) is that in order to get a vertical padding/space above the figure, I need to use vspace and specify the space every time.

Is there a way where I can configure all minipages to have the same vspace above them? And preferably not even include the vspace command in the minipage?

Thanks!

• Is there any reason why you don't use the figure environment, which has such a padding? – Bernard Aug 1 '17 at 23:24
• "I can't use figures because the images need to be directly underneath the step they are showing" – Eric Nelson Aug 1 '17 at 23:45
• In this case a simple center environment should be enough. I don't see why a minipage, , and more particularly a minipage which is \linewidth wide. – Bernard Aug 2 '17 at 0:11

I’d recommend that you do not redefine the minipage environment itself: you might still need the original version for something else. Rather, define a command that does what you want, that is, it inserts an illustration with a caption. As @Bernard has remarked, there’s no actual need for a minipage: a center enviroment will do. I make a tiny low-level customization to ensure that a page break does not occur right before the illustration: this could be achieved with the enumitem package too, but I’m somewhat reluctant to load the entire package just for this tiny adjustment…

\documentclass[a4paper]{article} % To avoid confusion, let us explicitly
% declare the paper format.

\usepackage[T1]{fontenc}         % Not always necessary, but recommended.
% End of standard header.  What follows pertains to the problem at hand.

\usepackage{graphicx}
\usepackage{capt-of}

\usepackage{mwe} % for this Minimal Working Example only

\makeatletter

\newcommand*\illustration[2]{%
% keep illustration on the same page as preceding text:
\begingroup
\@beginparpenalty \@M
\begin{center}%
\includegraphics[width=\linewidth]{#1}%
\captionof{figure}{#2}%
\end{center}%
\endgroup
}

\makeatother

\begin{document}

\lipsum[1]
\illustration{image-a}{The letter~A''}
\lipsum[2]
\illustration{image-b}{The letter~B''}
\lipsum[2]
\illustration{image-c}{The letter~C''}
\lipsum[4]

\end{document}

Edit: On second thought, a minipage environment is needed, because of how the \captionof command is implemented, in order to avoid a possible page break between the image and its caption. The following is a different solution that takes this issue into account:

\documentclass[a4paper]{article} % To avoid confusion, let us explicitly
% declare the paper format.

\usepackage[T1]{fontenc}         % Not always necessary, but recommended.
% End of standard header.  What follows pertains to the problem at hand.

\usepackage{graphicx}
\usepackage{capt-of}

\usepackage{mwe} % for this Minimal Working Example only

\makeatletter

\newcommand*\illustration[2]{%
\par % end previous paragraph (if necessary)
\noindent
\begin{minipage}{\linewidth}%
\centering
\includegraphics[width=\textwidth]{#1}%
\captionof{figure}{#2}%
\end{minipage}\par
}

\makeatother

\begin{document}

\lipsum[1]
\illustration{image-a}{The letter~A''}
\lipsum[2]
\illustration{image-b}{The letter~B''}
\lipsum[2]
\illustration{image-c}{The letter~C''}
\lipsum[4]

\end{document}

for space before mini pages you not need to change minipage definition. with help of etoolbox macro \BeforeBeginEnvironment you can add desired space:

\documentclass[12pt,a4paper]{article}
\usepackage[demo]{graphicx}
\usepackage{caption}

\setlength{\parindent}{0in}
\usepackage{etoolbox}                               % <---
\BeforeBeginEnvironment{minipage}{\par\vspace{5mm}} % <---

\usepackage{lipsum}

\begin{document}
\lipsum[11]

\begin{minipage}{\linewidth}
\includegraphics[width=\linewidth]{database}
\captionof{figure}{Desktop icon of simulator app}
\end{minipage}

\end{document}

You can redefine the minipage environment to include that vspace you need by the following three lines:

\let\oldminipage=\minipage
\let\endoldminipage=\endminipage
\renewenvironment{minipage}{\vspace{5mm}\begin{oldminipage}}{\end{oldminipage}}

I've included an example of my own since you didn't provide one. Here is the output of the original minipage and the re-defined one for comparison. In case you temporarily need the original behavior back, just use \begin{oldminipage} .. \end{oldminipage}.

\documentclass[12pt,a4paper]{article}
\usepackage[demo]{graphicx}
\usepackage{capt-of}
\setlength{\parindent}{0in}
\begin{document}

Some text here ...

\begin{minipage}{\linewidth}
\centering
\includegraphics[width=1\textwidth]{database}
\captionof{figure}{Desktop icon of simulator app}
\end{minipage}

\let\oldminipage=\minipage
\let\endoldminipage=\endminipage
\renewenvironment{minipage}{\vspace{5mm}\begin{oldminipage}}{\end{oldminipage}}

\bigskip
Some text here ...

\begin{minipage}{\linewidth}
\centering
\includegraphics[width=1\textwidth]{database}
\captionof{figure}{Desktop icon of simulator app}
\end{minipage}

\end{document}