[Tex/LaTex] Put a photo on the top right corner of a page

wrapfigure

I have the following code to show a picture around by some textes.

\documentclass{article}
\usepackage{wrapfig}
\usepackage{lipsum}

\begin{document}

\begin{wrapfigure}{r}{4cm}
  \begin{center}
    \includegraphics[width=0.2\textwidth]{mickey}
  \end{center}
\end{wrapfigure}

\lipsum[1-4]

\end{document}

The problem is that I want to put the photo on (exact) top right of the page. So I want to remove the blank marge above the photo and on the right of the photo. Also I feel that the blank marge below the photo is too much.

Does anyone know how to amend the code?

enter image description here

Best Answer

  1. Don't use the center environment. Use \centering instead.

  2. To adjust the horizontal spacing, you need to adjust the length \columnsep (see section 2 Sizing and optional overhang of the wrapfig documentation).

enter image description here

\documentclass{article}
\usepackage{wrapfig,graphicx,lipsum}

\begin{document}

\setlength{\columnsep}{1pt}%
\begin{wrapfigure}{r}{4cm}
  \centering
  \includegraphics[width=\linewidth]{example-image}
\end{wrapfigure}

\lipsum[1-4]

\end{document}

If the vertical spacing at the bottom doesn't suit your needs, then you could use the adjustbox package to visually trim the content, or reduce the image's height from TeX's point of view:

enter image description here

\documentclass{article}
\usepackage{wrapfig,lipsum,graphicx}

\begin{document}

\setlength{\columnsep}{1pt}%
\begin{wrapfigure}{r}{4cm}
  \centering
  \includegraphics[trim=0pt 12pt 0pt 0pt,width=\linewidth]{example-image}
\end{wrapfigure}

\lipsum[1-4]

\end{document}

Alternatively, specify the number of rows to re-align

% # of rows -------|
%                  v
\begin{wrapfigure}[8]{r}{4cm}
  \centering
  \includegraphics[width=\linewidth]{example-image}
\end{wrapfigure}