[Tex/LaTex] How to place an image at the right top of a document

positioning

I've been learning LaTeX and this community has been very helpful. Currently, I'm trying to set up corporate letters but as a newbie I'm encountering a variety of challenges, so I'm posting them one by one instead of putting it in the comments… maybe this helps others as well.

Anyway, I am trying to implement this answer that indicates that I can use vbox to float an image to the right of my header. I almost have this working, but I now try to adjust this to get my logo to stick to the top of the page. So I want to have this at the right top of my document. This is quite difficult! When I place the vbox, it creates unwanted spacing, for some odd reason. So I can not get the logo to be at the right top, even though I 'reset' every possible margin to 0 (in my opinion). Here is my distilled code:

\documentclass[a4paper]{article}
\usepackage{graphicx}
\graphicspath{{./img/}}
\usepackage{color}
\usepackage{xcolor}
\definecolor{orange}{HTML}{DD4616}
\definecolor{white}{HTML}{FFFFFF}
\usepackage{fullpage}
\usepackage[margin=0cm, paperwidth=215.9mm, paperheight=30mm,top=0cm]{geometry}
\parindent=0pt

\begin{document}
\fboxsep0pt

\pagecolor{orange}


\fbox{\vbox to 0pt{\hfill\fbox{\includegraphics[height=3cm]{logo}}}}

\end{document}

I've used a full black logo for illustrative purposes. Here you can see this:

enter image description here

The black line is the vbox border. For some reason, it jumps down. The only way to get rid of this that I found out about is this:

Make an invisible vbox

Which means that I need to add:

\edef\theprevdepth{\the\prevdepth}\nointerlineskip
\vbox to 0pt{...}\prevdepth=\theprevdepth

But there are two problems with this:

  • I get an error with I try this (Improper \prevdepth.)
  • More importantly, I really want to understand my code at this point, and get to master the basics of positioning elements. This feels overcomplicated and I do not understand it at all.

Is there any easy way to position an image at the right top of a document?

Best Answer

The \vbox is anchored to the baseline. Also, since there is an additional \topskip at the top of the page between the header and the first line of text. So, jumping vertically -\baselineskip-\topskip pushes the \vbox of height 0pt tight against the upper part of the page boundary.

Here's your MWE:

enter image description here

\documentclass[a4paper]{article}
\usepackage{graphicx}% http://ctan.org/pkg/graphicx
\usepackage{xcolor}% http://ctan.org/pkg/xcolor
\definecolor{orange}{HTML}{DD4616}
\definecolor{white}{HTML}{FFFFFF}
\usepackage{fullpage}% http://ctan.org/pkg/fullpage
\usepackage[margin=0cm, paperwidth=215.9mm, paperheight=30mm]{geometry}
\setlength{\parindent}{0pt}
\setlength{\fboxsep}{-\fboxrule}%
\begin{document}
\pagecolor{orange}%
\vspace*{\dimexpr-\baselineskip-\topskip\relax}

\fbox{\vbox to 0pt{\hfill\fbox{\includegraphics[height=3cm]{html5}}}}
\end{document}

Some comments about the code:

  • You have to use \vspace* for vertical space at the top of the page;
  • \dimexpr...\relax is used as a dimension expression for combining lengths (or dimensions). You can also include the calc package which allows you to forego this notation and use -\baselineskip-\topskip as is;
  • I use \setlength rather than \<len>=<dim> - that's just the LaTeX way; the latter is the TeX way;
  • margin=0cm is equivalent to left=0cm,right=0cm,bottom=0cm,top=0cm in geometry, so I dropped top=0cm;
  • I set \fboxsep to -\fboxrule. This way the rule is actually drawn within the bounding box. Otherwise it would add .4pt to each of the sides (a total of .8pt vertically and horizontally) even if you use \setlength{\fboxsep}{0pt};
  • Loading xcolor is sufficient, so you don't need to load color.