6

I would like to specify minimum vertical margins equal to the height of three single-spaced lines around all figures in a document. By "minimum" I mean that if the figure already has this amount of space around it, then the margins should not add any more. How do I do this?

MWE

Note that to use this you'll need to download my image (or substitute your own).

\documentclass[11pt]{report}
\usepackage{amsmath}
\usepackage{graphicx}
\begin{document}

is reminiscent of a banana. Because there are separate hippocampi in the
left and right hemispheres, there are also two dentate gyri.

\begin{figure}[htbp]
\centering
\includegraphics{hippocampus_frontal.pdf}
\caption{Hippocampal Formation (Allen Brain Explorer)}
\label{fig:HippocampusFrontal}
\end{figure}

The DG has traditionally been split into three layers visible in

\end{document}

Vertical Margins

enter image description here

  • 1
    I don't really get what do you mean. Could you please: 1) Add a MWE; and 2) make a screenshot of what margin you want to change and how to change? Then it would be much easier to help you ;) – yo' Nov 24 '13 at 1:08
  • I have a feeling you may be asking the wrong question. Without a way to ensure that the bounding box of each image is the smallest it can be, there's no way for TeX to "know" how much white space an image contains. Are the figures included by \includegraphics or are they generated by TikZ (or PSTricks)? What are the vertical margins relative to? Do they include the caption or not? – Alan Munn Nov 24 '13 at 1:44
  • @AlanMunn These are figures included by \includegraphics. They are relative to whatever the top and bottom elements in the figure environment are. So in my example, that means the top of the image and the bottom of the caption. I do not expect LaTeX to account for whitespace included in the image file itself. Thanks! – Sean Mackesey Nov 24 '13 at 2:05
  • \intextsep .... – Nicholas Hamilton Nov 24 '13 at 2:12
  • @ADP Yes! That is all I was looking for. Could you please make it an answer (including the basics about what it is and how to set it) so that I can accept it. Thanks – Sean Mackesey Nov 24 '13 at 2:18
5

As ADP correctly notes, the \intextsep length determines the spacing between the top and bottom of the standard floats, so changing it globally will change it for both tables and figures, which is probably not what you want. However, you don't need to create a custom environment to have a different spacing for each; instead you can hook into the figure environment directly. This has the advantage that if you are using the same code for another requirement, you won't have to adjust anything other than the value. Here's an example. I've used the same 'image' to stand in for the tabular content that the {table} environment would normally contain, since it makes it easier to see the spacing difference.

\documentclass{article}
\usepackage{kantlipsum}
\usepackage[demo]{graphicx}
\usepackage{etoolbox}
\AtBeginEnvironment{figure}{\setlength{\intextsep}{3\baselineskip}}
\begin{document}
\kant[1]
\begin{figure}[h]
\includegraphics[width=\linewidth]{foo}
\caption{A Figure with a wide spacing}
\end{figure}
\kant[3-4]
\begin{table}[h]
\includegraphics[width=\linewidth]{foo}
\caption{A Table with the regular spacing}
\end{table}
\kant[4]
\end{document}

figure part table part

5

The length that you are referring to is \intextsep, therefore, if you want to set it (globally) to something other than the default, this can be achieved via the following:

\setlength{\intextsep}{XYZ}

Where XYZ is your chosen dimension. Assuming you want three single-space lines, this would be achieved via the following:

\setlength{\intextsep}{3\baselineskip}

I believe that \intextsep affects a few other layout arrangements in the document, so perhaps you don't want to change it globally, in this case you could create a new environment which temporarily changes to your desired value, prior to restoring upon leaving the environment:

%DEFINE YOUR CUSTOM LENGTH
\newlength{\myintextsep}\setlength{\myintextsep}{3\baselineskip}
%CUSTOM ENVIRONMENT
\makeatletter
    \newenvironment{myfigure}[1][]{ %
        \let\intextsep@old\intextsep %
        \setlength{\intextsep}{\myintextsep} %
        \figure[#1] %
    }{ %
        \endfigure\setlength{\intextsep}{\intextsep@old} %
    }
\makeatother 

By comparison, the difference can be seen here:

Example

With the full working code as follows:

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

%DEFINE YOUR CUSTOM LENGTH
\newlength{\myintextsep}\setlength{\myintextsep}{3\baselineskip}
%CUSTOM ENVIRONMENT
\makeatletter
    \newenvironment{myfigure}[1][]{ %
        \let\intextsep@old\intextsep %
        \setlength{\intextsep}{\myintextsep} %
        \figure[#1] %
    }{ %
        \endfigure\setlength{\intextsep}{\intextsep@old} %
    }
\makeatother

\begin{document}
        \def\t{The cat sat on the mat }

        \t\t\t\t

        \begin{figure}[h]
            \centering\includegraphics[width=4in,height=1cm]{demo.png}
        \end{figure}

        \t\t\t\t\t

        \begin{myfigure}[h]
            \centering\includegraphics[width=4in,height=1cm]{demo.png}
        \end{myfigure}

        \t\t\t\t\t
\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.