我有一张 4903 × 314 像素的 PDF 图像。如何将节点放置在图像中的 (3903, 251) 位置?图像本身将缩放以适合页面宽度。
答案1
编辑:为多个节点添加第二个宏
描述\PlaceInImageAt
:
中的第一个参数
(#1,#2)
应该是图像的像素大小,第二个参数(#4,#5)
应该是您想要放置节点的像素。#3
应该是要使用的图像,可选的[#6]
是您可以提供的选项\node
,最后#7
是该节点的内容。size 参数(#1,#2)
是可选的。如果未指定,则使用。您可以通过更改中的命令(100,100)
将图像缩放到您想要的任何尺寸(甚至不成比例)。\includegraphics
#3
描述\MultiPlaceInImageAt
:
前三个参数相同。可选参数接受[#4]
应传递给每个节点的选项。最后一个参数是一个逗号分隔的列表。每个节点都应使用以下方案括在括号中
{<x>,<y>=[<options>]<content>}
。
\documentclass[]{article}
\usepackage{tikz,graphicx}
\makeatletter
\newbox\PlaceInImageAtbox
\newcommand\PlaceInImageAt{}% just to check whether it is already defined
\def\PlaceInImageAt
{%
\@ifnextchar(
{\PlaceInImageAt@i}
{\PlaceInImageAt@i(100,100)}
}
\def\PlaceInImageAt@i(#1,#2)#3(#4,#5)%
{%
\@ifnextchar[
{\PlaceInImageAt@ii{#1}{#2}{#3}{#4}{#5}}
{\PlaceInImageAt@ii{#1}{#2}{#3}{#4}{#5}[]}%
}
\def\PlaceInImageAt@ii#1#2#3#4#5[#6]#7%
{%
\setbox\PlaceInImageAtbox\hbox{#3}%
\begin{tikzpicture}
\draw[use as bounding box] node at (0,0) {\usebox\PlaceInImageAtbox};
\pgfmathsetmacro\PlaceInImageAtx{(#4 - #1/2) * (\wd\PlaceInImageAtbox/#1)}
\pgfmathsetmacro\PlaceInImageAty{(#5 - #2/2) * (\ht\PlaceInImageAtbox/#2)}
\node[#6] at (\PlaceInImageAtx pt,\PlaceInImageAty pt) {#7};
\end{tikzpicture}%
}
\def\MultiPlaceInImageAt
{%
\@ifnextchar(
{\MultiPlaceInImageAt@i}
{\MultiPlaceInImageAt@i(100,100)}
}
\def\MultiPlaceInImageAt@i(#1,#2)#3%
{%
\@ifnextchar[
{\MultiPlaceInImageAt@ii{#1}{#2}{#3}}
{\MultiPlaceInImageAt@ii{#1}{#2}{#3}[]}%
}
\def\MultiPlaceInImageAt@ii#1#2#3[#4]#5%
{%
\setbox\PlaceInImageAtbox\hbox{#3}%
\begin{tikzpicture}
\draw[use as bounding box] node at (0,0) {\usebox\PlaceInImageAtbox};
\@for\arg@MultiPlace:={#5}\do
{%
\expandafter
\MultiPlaceInImageAt@each\arg@MultiPlace\@rgend{#1}{#2}{#4}
}
\end{tikzpicture}
}
\def\MultiPlaceInImageAt@each#1,#2=%
{%
\@ifnextchar[
{\MultiPlaceInImageAt@each@i{#1}{#2}}
{\MultiPlaceInImageAt@each@i{#1}{#2}[]}%
}
\def\MultiPlaceInImageAt@each@i#1#2[#3]#4\@rgend#5#6#7%
{%
\pgfmathsetmacro\PlaceInImageAtx{(#1 - #5/2) * (\wd\PlaceInImageAtbox/#5)}
\pgfmathsetmacro\PlaceInImageAty{(#2 - #6/2) * (\ht\PlaceInImageAtbox/#6)}
\node[#7,#3] at (\PlaceInImageAtx pt, \PlaceInImageAty pt) {#4};
}
\makeatother
\begin{document}
\PlaceInImageAt(4903,314){\includegraphics{example-image}}
(3903,251)[draw,circle,red]{foo}
\PlaceInImageAt{\includegraphics[width=5cm,height=2cm]{example-image}}
(50,50)[draw,circle,red]{foo}
\MultiPlaceInImageAt{\includegraphics[scale=0.5]{example-image-a}}
[red]% option for each node
{{20,20=[blue]a},{40,40={b}},{60,60={c}},{80,80={d}}}
\end{document}
答案2
下面是一个代码,如果您仅将图形缩放到\textwidth
(使得垂直缩放成比例),则可以执行此操作。
更新:写了一个小宏,可以重复使用。
\documentclass{article}
\usepackage{tikz}
\newcommand{\PlaceNode}[5][]{
\pgfmathsetmacro{\myx}{(#2-4903/2)*(\the\textwidth/4903)}
\pgfmathsetmacro{\myy}{(#3-314/2)*(\the\textwidth/4903)}
\node[#1] (#4) at (\myx pt,\myy pt) {#5};
}
\begin{document}
\begin{tikzpicture}
\node at (0,0){\includegraphics[width=\textwidth]{example-image-a}};
\PlaceNode[draw,circle,red]{3903}{251}{first}{}
\PlaceNode[draw,circle,blue]{3703}{151}{second}{}
\end{tikzpicture}
\end{document}