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?

1 Answers1

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.

David Carlisle
  • 757,742
  • 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. – David Carlisle 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. – Klaus Haukenstein 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. – David Carlisle May 16 '20 at 17:31