Is it possible to write a Latex macro based on \includegraphics so that given two points A and B this will become the lower side of the image (i.e., the image will be translated, scaled, and rotated accordingly)? This would give a functionality equivalent to that provided by Geogebra. It should have 3 arguments - the two pints and the image file name.
Asked
Active
Viewed 127 times
1 Answers
1
With sloped TikZ option you can already go a long way: (read the notes in the code to understand what it does)
%! TEX program = pdflatex
\documentclass{article}
\usepackage{tikz,fp}
\usetikzlibrary{calc}
\makeatletter
... (copy code from https://tex.stackexchange.com/a/38500/250119 and paste here)
\makeatother
\usepackage{graphicx}
\begin{document}
\begin{tikzpicture}
\path % specify the corner points.
(2, 3) coordinate (b)
(4, 5) coordinate (a)
;
\calcLength(a,b){mylen} % store the distance between 2 points into mylen.
% DO NOT include a space e.g. between a and b
\path
(a) to node [midway, sloped, allow upside down, above, inner sep=0] {
\includegraphics[width=\mylen pt]{a.png}
} (b);
\end{tikzpicture}
\end{document}
the remaining part is to compute the distance between two nodes and pass it to the width of includegraphics.
It's the reader's exercise on how to wrap the functionality into a macro.
alternative question title (for searching): How can I include an image in TikZ with corners anchored at two points?
user202729
- 7,143
-
Link to similar question tikz pgf - Draw a square with given points - TeX - LaTeX Stack Exchange – user202729 Apr 21 '22 at 05:41
-
Similar question without rotating the node: positioning - TikZ: position with different anchors - TeX - LaTeX Stack Exchange – user202729 Apr 21 '22 at 05:43
-
Thanks for the solution. It is in the right direction, but there is a problem with the scaling. The image lays on the segment connecting a with b but is smaller than it should be. I think that in order to get the right dimensions we should use some rounding too. Do you have a good documentation of loadgraphics? (BTW, package pretty is missing in overleaf and seems to be superfluous) – bandi May 13 '22 at 02:14
-
@bandi (yes, personal debug package, forgot to remove.) – user202729 May 13 '22 at 13:03
-
@bandi texdoc graphicx page 9, or LaTeX unofficial reference manual (latex2e.pdf) page 201. – user202729 May 13 '22 at 13:20