[Tex/LaTex] Remove vertical space between two includegraphics, without random negative vspaces

graphicslengthsspacing

everyone.
I've lost hours on this dumb problem: if I write something like this

\includegraphics{img}\\*
\includegraphics{img}

a noticeable vertical space appears between the two images when I'm using documentclass "report" or "article". However, with the "minimal" class, this space disappears, so I thought there was some weird length defining this. I tried setting lots of lengths to zero, but with no luck so far.

There exist lots of questions of this type already, but most of theme deal with using figures, centering commands and such. I'm actually using a center environment in my real document, but removing it did not solve the problem. Furthermore, I think resorting to a negative vspace to solve such problems is a bit dirty: if its length were set incorrectly, the images might overlap, and I would not want to lose a single pixel, especially because it would be very surprising if there were no other way to solve this.

Here's an almost minimal working example, with lots of random setlengths that did not solve the problem:

\documentclass{article}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\usepackage[demo]{graphicx}

\setlength{\parindent}{0pt}
\setlength{\parskip}{0pt}
\setlength{\textfloatsep}{0pt}
\setlength{\dbltextfloatsep}{0pt}
\setlength{\fboxsep}{0pt}
\setlength{\baselineskip}{0pt}
\setlength{\tabcolsep}{0pt}

\begin{document}

\includegraphics{img}\\*
\includegraphics{img}

\end{document}

Changing the class to "minimal" turns the vertical space into an almost unnoticeable paler line, which is a bit better, but of course I cannot use "minimal" for my real documents…

This might actually have something to do with the depth of the box created by includegraphics (I already noticed that rotations, with angle = 90orsomething, sometimes do weird things with the depth; even the documentation says so), but this command does not appear to have a "depth =" key.

Actually, the ultimate goal is to append four images:

AB
CD

with absolutely no space between them, and keeping them on the same page. I thought it would be a piece of pie, but it seems I'm even more a noob than I thought.

You're my last hope of not resorting to Bash and stuff (I like Bash, but it would be better to handle this with LaTeX alone); I might end up using:

$ convert \( IMG_1 IMG_2 +append \) \( IMG_3 IMG_4 +append \) -append OUTPUT

(At least this issue allowed me to learn cool stuff about convert…)

Thanks.

Best Answer

The space you see is produced by \lineskip, which by default is 1pt (but in the minimal class is 0pt). Since most likely your images are higher than the baseline skip, the parameter will be used by the line breaking and boxing mechanism.

See https://tex.stackexchange.com/a/96944/4427 for more information about \lineskip and when it comes into action.

You can set it to zero for the particular environment; I used figure, you can also use center, if you like.

\documentclass{article}
\usepackage{graphicx}

\begin{document}

\begin{figure}[htp]
\setlength{\lineskip}{0pt}
\centering

\includegraphics[width=4cm]{example-image}%
\includegraphics[width=4cm]{example-image-a}\\
\includegraphics[width=4cm]{example-image-b}%
\includegraphics[width=4cm]{example-image-c}

\caption{Some caption}

\end{figure}

\end{document}

enter image description here

A “safer” solution, as the setting to \lineskip may have adverse effects on the caption if it has unusually big objects:

\documentclass{article}
\usepackage{graphicx}

\newenvironment{noverticalspace}
 {%
  \par % ensure we're in vertical mode
  \offinterlineskip % don't do the baselineskip calculations
 }
 {\par}% be sure to finish up

\begin{document}

\begin{figure}[htp]
\centering

\begin{noverticalspace}
\includegraphics[width=4cm]{example-image}%
\includegraphics[width=4cm]{example-image-a}\\
\includegraphics[width=4cm]{example-image-b}%
\includegraphics[width=4cm]{example-image-c}
\end{noverticalspace}

\caption{Some caption}

\end{figure}

\end{document}
Related Question