26

I've got table and figure environments, some placed "here" with h and some arranged automatically. But all together I don't like the spacing above and below the figure and table environments.

I searched the internet and found tips to set abovecaptionskip and belowcaptionskip. Well since my tables and figures all have a caption below the actual table/figure belowcaptionskip allows me to set the \vspace between table/figure and the following text. But abovecaptionskip obviously just gives me \vspace between the table/figure and the caption, but not between the above text and the table/figure.

How to set the \vspace above and below table/figure correctly then?

lockstep
  • 250,273
Meinzlein
  • 827

5 Answers5

20

You are probably looking for the length \intextsep (at least). This is the space that is inserted to separate floats from the surrounding text.

13

One way would be to simply add the desired \vspace before and after:

enter image description here

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

\newenvironment{MyFigure}[1][]{\begin{figure}[#1]\vspace{1.0cm}}{\vspace{1.0cm}\end{figure}}
\begin{document}
\lipsum[1]
\begin{MyFigure}[h!]
    \centering
    \includegraphics[width=5cm,height=2cm]{foo}
    \caption{Figure: foo}
\end{MyFigure}
\lipsum[2]
\end{document}
Peter Grill
  • 223,288
9

Try adjusting the parameter \intextsep.

Ian Thompson
  • 43,767
6

etoolbox can be used to add content at the start/end of an environment automatically. The following minimal example adds (up to) 5mm to the top and bottom of the figure environment (via \addvspace{5mm}).

enter image description here

\documentclass{article}
\usepackage[demo]{graphicx}% http://ctan.org/pkg/graphicx
\usepackage{lipsum}% http://ctan.org/pkg/lipsum
\usepackage{etoolbox}% http://ctan.org/pkg/etoolbox
\AtBeginEnvironment{figure}{\addvspace{5mm}}\AtEndEnvironment{figure}{\addvspace{5mm}}
\begin{document}
\lipsum[1]
\begin{figure}[h!]
  \centering\includegraphics{image}
  \caption{This is a caption.}
\end{figure}
\begin{figure}[h!]
  \centering\includegraphics{image}
  \caption{This is a caption.}
\end{figure}
\lipsum[2]
\end{document}

The advantage with using \addvspace is that it only adds enough, making figures that end up on top of one another not add double the amount of space. See the \addvspace{<len>} documentation.

Werner
  • 603,163
  • Hm, I get "undefined control sequenc" when I add the etoolbox package and use the AtBeginEnvironment.. – Meinzlein Dec 15 '11 at 18:22
  • @Meinzlein: You need to be a little more specific than that, since etoolbox recently added those patching commands. The most recent version of etoolbox on CTAN is v 2.1 2011/01/03. To see yours, add \listfiles before \documentclass and find the etoolbox entry after **File List** in your .log file. – Werner Dec 15 '11 at 18:33
  • Not working here either, inside a beamer class, probably interference with some other stuff I have... – PatrickT Apr 16 '18 at 19:57
  • @PatrickT: You can probably forego using figures inside a beamer presentation, as no-one would necessarily remember figures numbers associated with a \caption. Instead, name the figures or just use a center environment to place and caption them. – Werner Apr 16 '18 at 20:13
  • @Werner. Thanks. The figure, table and center environments create too much space when they are immediately below the frametitle. I often use the caption (without numbers), typeset in smaller font, so it's more convenient than a center environment. My workaround is adding \vspace{-1em} but I was looking for a more general fix. Your approach looked very promising. I don't really have time to make a MWE and post a question, so I'll stay with negative vspace for now. Thanks! – PatrickT Apr 16 '18 at 20:35
  • If the floats, well, float to another place(s), the \vskips stay at the point of figure's declaration. See here. – ScumCoder May 28 '19 at 18:10
  • I got an error message telling me I was missing an item. This site gave the solution : http://joshua.smcvt.edu/latex2e/_005caddvspace.html So just add \par in front of \addvspace and it did work. – Eelco van Vliet Sep 09 '19 at 13:25
0

Insert \vspace{} above \begin{table}

The example below adds 10mm of space above the table.

\vspace{10 mm} % add some space above the table

\begin{table}[ht]
\centering % used for centering table
\begin{tabular*}{0.7\textwidth}{ l r } % centered columns (2 columns)
\hline\hline %inserts double horizontal lines
Category & Score \\ [0.5ex] % inserts table heading
\hline % inserts single horizontal line
a & b \\ % inserting body of the table
c & d \\
e & f \\
g &  h \\ [1ex] % [1ex] adds vertical space
\hline %inserts single line
\end{tabular*}
% \caption{What a great table}
% \label{table:scores} % is used to refer this table in the text
\end{table}
ShreevatsaR
  • 45,428
  • 10
  • 117
  • 149
Ron
  • 9
  • 4
    Welcome to TeX.SX! While this can work if the float can respect the h option, it will leave an unwanted vertical space if the float must be moved. – egreg Jul 01 '13 at 09:22