1

I'm trying to create a LaTeX document that contains mostly images (but occasionally some text after an image). I currently have the following in the preamble:

\def \ifempty#1{\def\temp{#1} \ifx\temp\empty }

\newcommand{\image}[2]{
    \ifempty{#2}
        \begin{figure}[H]
            \includegraphics[width=\textwidth]{#1}
        \end{figure}
    \else
        \begin{figure}[H]
            \includegraphics[width=\textwidth]{#1}
            \caption*{\textit{\Large{#2}}}
        \end{figure}
    \fi
}

and then when I add an image I use:

\image{imageName}{optionalText}

This inserts a lot of whitespace in between images. What I want is for each image to be right after the next unless the image cannot fit on the page, in which case a new page is created (basically, I want the images to flow the same way anything else would by default).

I have tried changing the [H] option on the caption as suggested in this question as well as tried out various suggestions from OverLeaf as well as this WikiBooks article, but have been unable to get the images to flow properly.

How can I get images to flow normally?

5
  • Possible duplicate: How to change the spacing between figures/tables and text?
    – Werner
    Feb 18 '20 at 19:23
  • @Werner Maybe I should add that \setlength{\floatsep}{0pt} at the beginning of the document has no effect either, which is what that answer seems to say. Feb 18 '20 at 19:32
  • you can delete the \begin{figure} environments they are doing nothing useful and adding vertical space Feb 18 '20 at 19:34
  • size commands do not take an argument and need to include end of para so \textit{\Large{#2}} should be {\Large\textit{#2}\par} Feb 18 '20 at 19:35
  • You will always get large gaps if you force an image to the next page. You could use \raggedbottom to make sure the gaps always occur at the bottom of the page instead of being spread around, or insert \vfill where you want the gaps to occur. Feb 19 '20 at 15:39
1
\def \ifempty#1{\def\temp{#1} \ifx\temp\empty }

always adds a space token to the output each time you use it, you intended

\def \ifempty#1{\def\temp{#1}\ifx\temp\empty }

However it is not generally safe to define an \if this way as it is a macro not a tex \if.. so will not work naturally if combined with other if for example

\iffalse
  \ifempty{}
    yes
   \else
    no
   \fi
\fi

Looks well nested but the \ifempty will not be seen as an if so \iffalse will match the \else typeset no and the second \fi will generate an error.

For your main macro I would omit the figure environments that are just adding vertical space.

\newcommand{\image}[2]{% 
  \par
   \noindent
   \begin{minipage}{\textwidth}%
            \includegraphics[width=\textwidth]{#1}%
  \par
   {\Large\itshape #2\par}%
   \end{minipage}%
  \par
}

with this form you don't need to test for #2 being empty as it will do nothing in that case.

4
  • This may need to be expanded to include a proper caption (with label Figure X:) and not have it possibly broken from the figure.
    – Werner
    Feb 18 '20 at 19:54
  • @Werner on caption the OP was using \caption* so i assumed didn't want numbers, although \captionof could be used. I'll add a minipage though. Feb 18 '20 at 19:55
  • When there are many images in a row (with no text), this does not remove all space between the images. The space is usually about 1px or more and I cannot for the life of me figure out where it's coming from. It happens even if I remove all code but the \includegraphics command as well. May 16 '20 at 17:27
  • @AveryGreen it's better to ask a new question than add comments on old answers. \includegraphics never adds space, if you have a row of them there will be no space between, if it linebreaks then you will get \lineskip space which is1pt by default but you can set it to 0pt. May 16 '20 at 17:31

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.