Positioning an image in the page corner with tikz and text below

graphicsoverlayspositioningtikz-pgf

LaTeX noob here.

I am trying to position images (no fixed height) in the upper right corner of a page. To achieve this I am using tikz with the overlay and remember picture options as suggested elsewhere. However this obviously means that any text I want to include gets overlayed on top of the picture, while I would like the text to appear below as if it was a normal figure.

The following is a minimal working example


\RequirePackage{tikz}
\RequirePackage{geometry}

%Used to make calculations with coordinates for positioning
\RequirePackage{calc}

%Width of border on the left with section title
\newlength{\titleborderwidth}

%Set border width
\setlength{\titleborderwidth}{0.16\paperwidth}

%Set page geometry
\geometry{inner=\titleborderwidth}

\begin{document}
        \begin{tikzpicture}[remember picture, overlay]
            \node[anchor=north east] at (current page.north east){
                \includegraphics[width=\paperwidth-\titleborderwidth]{example-image-a}
            };
        \end{tikzpicture}
\\
I would like this text to appear below the picture


\end{document}

This produces:

Additionally:

  • The reason I need the border width is since the document I am planning has a border on which the title lies vertically on the left side.

  • Currently with this MWE the image does not appear to fit perfectly in the upper right corner, but appears to have some white margins and I am not sure why.

  • I was not able to figure out whether there is a way to obtain the size of the picture (which is not preset and may be changing) to use it as an argument for \vspace.

  • I would prefer to use tikz since in the final document I am using other tikz objects with the shadows library, and I would like to be able to use that same library for this image as well.

I spent already a lot of time trying to figure this out on my own, but did not come to any solution that is not hacky or tacky. I would be really glad if you could bestow some LaTeX knowledge on me. Thanks!

Best Answer

It seems like you want to continue texting after the picture. So the picture is part of your text, not a floating object (in CSS sense). So the only thing unusual here is that the picture should touch the top margin. This is easy as long as you know exactly what's in that margin.

\documentclass{article}
\usepackage{geometry}
    \newlength{\titleborderwidth}
    \setlength{\titleborderwidth}{0.16\paperwidth}
    \geometry{inner=\titleborderwidth}
\usepackage{layout}
\usepackage{tikz}
\begin{document}
    \xdef\exacttopmargin{\the\dimexpr1em+\headsep+\headheight+\topmargin+1in\relax}%
    \vspace*{-\exacttopmargin}%
    \xdef\picturewidth{\the\dimexpr\paperwidth-\titleborderwidth\relax}%
    \noindent\includegraphics[width=\picturewidth]{example-image-a}
    
    I would like this text to appear below the picture.
    And you can.
    As you see, the only thing you need to do
    is to ``unskip'' the the correct amount of top margin
    so that the picture appears right on the top border.
    To obtain the correct amount of top margin,
    use package layout and use the command \texttt{\string\layout}.
    This command will draw the page layout and tell you the commands
    that represents the distances between certain layout-objects.
    Once you see the layout, you know what dimensions to add together.
    And then you add the negative of that dimensions.
    And you are done.
    You may also want to checkout the package showframe,
    which shows the layout on every page.
    
    \layout
\end{document}

the first page of a document, where an example picture touches the top margin