5

I want to insert an image at the bottom of a page, using the full \paperwidth. I found the textblock environment from the textpos package and defined a command in this way:

\newcommand*{\grafikunten}[1]{
    \begin{textblock*}{\paperwidth}(0mm, 80mm)
        \includegraphics[width=\paperwidth, keepaspectratio]{#1}
    \end{textblock*}
}

This is working so far, assuming that the vertical starting position of the picture is always "80mm", which of course is not. It depends on the height of the image.

How can I get the height of the image to calculate the exact vertical position?

Or is there another way to position images at the bottom of a page, or at every other side, even in a corner?

2

3 Answers 3

5

Environment textblock has also an optional parameter that specifies the anchor point of the text block: [0,1] means, the left bottom corner. Then you do not need to know the height of the image:

\documentclass{article}
\usepackage[a6paper,hmargin=10mm,vmargin={10mm,20mm}]{geometry}
\usepackage[absolute]{textpos}
\usepackage{graphicx}
\usepackage{lipsum}

\newcommand*{\grafikunten}[1]{
    \begin{textblock*}{\paperwidth}[0,1](0mm, \paperheight)
        \noindent
        \includegraphics[width=\paperwidth, keepaspectratio]{#1}
    \end{textblock*}
}

\begin{document}
\grafikunten{redline}
\lipsum[1-2]
\end{document}

Result

Measurements

In this case the height of the image does not need to be known. In other circumstances it can be useful to get the height.

The height of the image can be measured by \settoheight:

\newdimen\imageheight % goes into the preamble

\settoheight{\imageheight}{%
  \includegraphics[width=\paperwidth,keepaspectratio]{myimage}%
}

Or the image can be put into a box:

\newsavebox\imagebox % goes into the preamble

\sbox{\imagebox}{%
  \includegraphics[width=\paperwidth,keepaspectratio]{myimage}%
}%

The height, depth, and width of the box are available via: \ht\imagebox, \dp\imagebox or \wd\imagebox. (LaTeX purists can use package settobox instead.)

1
  • That works perfectly well, thanks! And thanks for the \noindent, as I did wonder about the white space on the left. Didn't mention it in this question, but I'm glad about an answer on a question I didn't ask :-) Jun 17, 2014 at 14:05
2

As mentioned in my comment, my answer at What are the ways to position things absolutely on the page? provides a way to do what you ask. I've modified the example to lay out images (instead of text) on my paper.

If you don't know the size of the image in advance (which I pretend for the blue box), set it in a savebox, and use its measurements to guide the placement.

\documentclass{article}
\usepackage{everypage}
\usepackage{xcolor}
\usepackage{lipsum}
\usepackage[demo]{graphicx}
% THESE ARE LaTeX DEFAULTS; CAN CHANGE IF NEEDED.
\def\PageTopMargin{1in}
\def\PageLeftMargin{1in}
\newcommand\atxy[3]{%
 \AddThispageHook{\smash{\hspace*{\dimexpr-\PageLeftMargin-\hoffset+#1\relax}%
  \raisebox{\dimexpr\PageTopMargin+\voffset-#2\relax}{#3}}}}
% VERIFIED THAT SETTING \hoffset AND \voffset DO NOT BREAK SOLUTION.
%\hoffset=0.4in
%\voffset=0.2in
\begin{document}
\lipsum[1]
\atxy{0in}{\paperheight}{\includegraphics[width=\paperwidth,height=2cm]{test}}
\atxy{0in}{\paperheight}{\textcolor{red}{\includegraphics[width=1cm,height=1cm]{test}}}
\atxy{\dimexpr\paperwidth-1.5cm}{\paperheight}{\textcolor{yellow}{%
  \includegraphics[width=1.5cm,height=1.5cm]{test}}}

\newsavebox\myfig
\savebox{\myfig}{\textcolor{blue}{\includegraphics[width=3cm,height=3cm]{test}}}
\atxy{\dimexpr\paperwidth-\wd\myfig}{\ht\myfig}{\usebox{\myfig}}
\end{document}

enter image description here

0

Using package calc:

\newlength{\mylength}
\setlength{\mylength}{\heightof{\includegraphics{myimage.png}}}

Length \mylength now contains the height of your image, which you can use to perform optimal positioning.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .