2

A follow up to this answer:

I am clipping a png image to which I want to specify scaled coordinates for the crop command.

So that

\clip (0,0) rectangle + (1,1);

crops nothing, and

\clip (0,0) rectangle + (0.4,0.6);

crops 40% in the x direction and 60% in the y direction.

How can I do it?

MWE

\documentclass{article}
\usepackage{tikz}
\begin{document}
Lorem ipsum

\begin{tikzpicture} \clip (0,0) rectangle + (1,1); {\includegraphics[width=\textwidth]{image01.png}}; \end{tikzpicture}

\end{document}

where image01.png is here

tush
  • 1,115

1 Answers1

4

First you need to measure the width and height of the image using a savebox (or at least the height if you specify with width).

The only way I know of to use \includegraphics (or \usebox) is inside a node.

\documentclass{article}
\usepackage{tikz}
\begin{document}
Lorem ipsum

\begin{tikzpicture} \sbox0{\includegraphics{example-image}}% get width and height

\begin{scope}[xscale={\wd0/1cm}, yscale={\ht0/1cm}, local bounding box=A] \clip (0,0) rectangle (1,1); \node[above right, inner sep=0pt] at (0,0) {\usebox0}; \end{scope}

\begin{scope}[xscale={\wd0/1cm}, yscale={\ht0/1cm}, shift=(A.north west)] \clip (0,0) rectangle (0.4,0.6); \node[above right, inner sep=0pt] at (0,0) {\usebox0}; \end{scope} \end{tikzpicture}

\end{document}

John Kormylo
  • 79,712
  • 3
  • 50
  • 120