[Tex/LaTex] How to put two pictures exactly above each other without line or space

graphics

How can I put two pictures exactly above each other without line or space. I added picture example below, this picture is composed of two images (the header image and the smartphone image)

enter image description here
I Appreciate any help.

Best Answer

An image inserted with \includegraphics is just like a big letter. You can achieve your aim by removing the interline glue that TeX inserts.

Interacting with paragraphs might be complicated, so it's better to resort to lower level commands, in this case:

\newcommand{\twoobjects}[2]{%
  \leavevmode\vbox{\hbox{#1}\nointerlineskip\hbox{#2}}%
}

Full example:

\documentclass{article}
\usepackage{graphicx}

\newcommand{\twoobjects}[2]{%
  \leavevmode\vbox{\hbox{#1}\nointerlineskip\hbox{#2}}%
}

\begin{document}

\twoobjects
  {\includegraphics[trim=5 5 5 5,clip,width=.2\textwidth]{example-image-a}}
  {\includegraphics[trim=5 5 5 5,clip,width=.2\textwidth]{example-image-b}}

\end{document}

The trim=5 5 5 5,clip part is just for removing the border in the example images provided by the mwe package and make clearer that no vertical space is inserted. You don't need it unless there is a border you want to remove (adjust the amount of trimming).

enter image description here

Nothing prevents you from using \twoobjects in a `figure environment, for instance, as

\begin{figure}
\centering

\twoobjects
  {\includegraphics[trim=5 5 5 5,clip,width=.2\textwidth]{example-image-a}}
  {\includegraphics[trim=5 5 5 5,clip,width=.2\textwidth]{example-image-b}}

\caption{Whatever}
\end{figure}

Actually one can do it with LaTeX tools only, at the cost of efficiency.

\documentclass{article}
\usepackage{graphicx}

\newcommand{\twoobjects}[2]{%
  \leavevmode\vbox{\hbox{#1}\nointerlineskip\hbox{#2}}%
}
\newcommand{\TWOOBJECTS}[2]{%
  {\renewcommand{\arraystretch}{0}%
   \begin{tabular}[b]{@{}c@{}}#1\\#2\end{tabular}}%
}

\begin{document}

\twoobjects
  {\includegraphics[trim=5 5 5 5,clip,width=.2\textwidth]{example-image-a}}
  {\includegraphics[trim=5 5 5 5,clip,width=.2\textwidth]{example-image-b}}
\TWOOBJECTS
  {\includegraphics[trim=5 5 5 5,clip,width=.2\textwidth]{example-image-a}}
  {\includegraphics[trim=5 5 5 5,clip,width=.2\textwidth]{example-image-b}}

\end{document}

You can check that the output is the same. One can consider the tabular approach advantageous, because the [b] option can be changed into [c] or [t]:

\newcommand{\TWOOBJECTS}[3][c]{%
  {\renewcommand{\arraystretch}{0}%
   \begin{tabular}[#1]{@{}c@{}}#2\\#3\end{tabular}}%
}

and the calls

\TWOOBJECTS
  {\includegraphics[trim=5 5 5 5,clip,width=.2\textwidth]{example-image-a}}
  {\includegraphics[trim=5 5 5 5,clip,width=.2\textwidth]{example-image-b}}
\TWOOBJECTS[t]
  {\includegraphics[trim=5 5 5 5,clip,width=.2\textwidth]{example-image-a}}
  {\includegraphics[trim=5 5 5 5,clip,width=.2\textwidth]{example-image-b}}
\TWOOBJECTS[b]
  {\includegraphics[trim=5 5 5 5,clip,width=.2\textwidth]{example-image-a}}
  {\includegraphics[trim=5 5 5 5,clip,width=.2\textwidth]{example-image-b}}

would yield center, top and bottom alignment respectively. The approach with TeX primitives is equivalent to the [b] option.

Decide if you want to trade efficiency with flexibility.

Related Question