0

I've been using this code in my preamble to control the float of all my figures and tables in my document. However, I think it adds an unnecassary white space before and after each figure/table. Is there any way to control this and minimise the space between figures/a figure and text?

\usepackage{float}
\let\origfigure\figure
\let\endorigfigure\endfigure
\renewenvironment{figure}[1][2] {
    \expandafter\origfigure\expandafter[H]
} {
    \endorigfigure
}

Tanga94
  • 103
  • latex floats figures to avoid getting bad white space at page breaks. [H] disables floating so by design introduces bad white space that you choose to manually fix by moving the figure by hand in the source so that it appears in a good place with respect to page breaking. You are also adding additional white space tokens due to missing % at ends of lines in the definition, but using [H] would be the main issue. – David Carlisle Feb 10 '21 at 11:27
  • \renewenvironment{figure}[1][2] makes the default value of the optional argument be [2] which would be an error except that you never use the optional argument at all. – David Carlisle Feb 10 '21 at 11:29
  • 1
    \expandafter\origfigure\expandafter[H expands H which is not expandable, you can delete both \expandafter as they are doing nothing. – David Carlisle Feb 10 '21 at 11:31
  • Hi @DavidCarlisle, is there perhaps a better way to keep figures/tables in the position they are in my document and avoid the big white spaces without using a float? All of my figures and tables go to the end of my document without using a float so I'm trying to fix that – Tanga94 Feb 10 '21 at 11:41
  • The only purpose of the figure environment is to specify that the content may be moved. [H] makes sense as an occasional one-off over-ride but it doesn't make sense to make all figures do that, you may as well not use figure at all. I guess this question is a duplicate of https://tex.stackexchange.com/questions/8625/force-figure-placement-in-text – David Carlisle Feb 10 '21 at 11:50
  • Note that [ht] usually does the same thing as [H], and only moves the figure when [H] would leave a big hole in the document. – John Kormylo Feb 10 '21 at 15:54

1 Answers1

0

You have

\renewenvironment{figure}[1][2] {
    \expandafter\origfigure\expandafter[H]
} {
    \endorigfigure
}

and ask about white space. Almost every line could potentially add white space.

\renewenvironment{figure}[1][2]

this defines \begin{figure} to be an environment with one optional argument, if the option is not used then \begin{figure} is equivalent to \begin{figure}[2] (this was presumably not intended as 2 has no defined behaviour here) As the optional argument is not used at all, the default is arbitrary, perhaps [1][] would be clearer.

{

This adds a space token at the start of the definition, this could, depending where the figure is placed may produce a spurious additional line of white space in the resulting document. It is usually better to use {% so no space token is added to the definition.

\expandafter\origfigure\expandafter[H]

This expands the non expandable token H before calling \origfigure so this is equivalent to

\origfigure[H]

As above this adds a space token to the start of the environment, this is less likely to generate white space in the output, but again ending the line with % is good practice.

As #1 is not used the user-supplied or default positioning argument is ignored here and [H] is always used.

H by design produces bad white space at page breaks, as the reason for floating figures is to avoid bad page breaks and this disables that. A better alternative would be just to use \includegraphics directly as figure is only there to specify that the figure is a float (ie, may be moved). H (which was actually my idea originally:-) is just for final manual over-ride and manual positioning in final edit stages of a document, it should never be used as a default mechanism.

} {

Again the end of line here adds a space token and as in the first line this will produce white space in the output in some cases.

David Carlisle
  • 757,742