![当我们在编译时不知道网格大小时,如何在 TikZ 网格上创建整数标签?](https://linux22.com/image/246875/%E5%BD%93%E6%88%91%E4%BB%AC%E5%9C%A8%E7%BC%96%E8%AF%91%E6%97%B6%E4%B8%8D%E7%9F%A5%E9%81%93%E7%BD%91%E6%A0%BC%E5%A4%A7%E5%B0%8F%E6%97%B6%EF%BC%8C%E5%A6%82%E4%BD%95%E5%9C%A8%20TikZ%20%E7%BD%91%E6%A0%BC%E4%B8%8A%E5%88%9B%E5%BB%BA%E6%95%B4%E6%95%B0%E6%A0%87%E7%AD%BE%EF%BC%9F.png)
我想在网格上放置整数标签。网格大小取决于加载的图像。
实际场景是矢量化 JPG 图像,但我需要一个网格标签进行导航,如下所示
\documentclass{article}
\usepackage{tikz}
\usepackage{graphicx}
\newsavebox\TempBox
\savebox\TempBox{\includegraphics{batman.jpg}}
\begin{document}
\begin{tikzpicture}
\node[inner sep=0] at (0,0) {\usebox\TempBox};
\draw[white](-0.5\wd\TempBox,-0.5\ht\TempBox) grid (0.5\wd\TempBox,0.5\ht\TempBox);
% I want to put integer labels for navigation purpose. The following code cannot be compiled!
%\foreach \x in {-0.5\wd\TempBox,1,...,0.5\wd\TempBox} { \node [anchor=north] at (\x,0) {\x}; }
%\foreach \y in {-0.5\ht\TempBox,1,...,0.5\ht\TempBox} { \node [anchor=east] at (0,\y) {\y}; }
\draw[line width=2pt,blue] (0,0) ellipse (6.3 and 3.4);
\draw[line width=2pt,blue] (0,0) ellipse (6.9 and 4.0);
\end{tikzpicture}
\end{document}
下图是完成MWE的原始图像。
如何在 TikZ 网格上创建整数标签?
我可能需要类似floor
或ceiling
功能的东西。
答案1
你需要做循环整数值,并根据这些整数值计算位置。下面我用来\pgfmathsetmacro
计算每个方向的步长,并\pgfmathtruncatemacro
计算循环的整数起点和终点:
代码:
\documentclass{article}
\usepackage{graphicx}
\usepackage{tikz}
\newsavebox\TempBox
\savebox\TempBox{\includegraphics{batman.jpg}}
\begin{document}
\begin{tikzpicture}
% Compute step size and number of grid lines in each direction:
\pgfmathsetmacro{\NumberOfXGrid}{\wd\TempBox/1.0cm}%
\pgfmathtruncatemacro{\XGridStart}{-0.5*\NumberOfXGrid}%
\pgfmathtruncatemacro{\XGridEnd}{0.5*\NumberOfXGrid}%
\pgfmathsetmacro{\NumberOfYGrid}{\ht\TempBox/1.0cm}%
\pgfmathtruncatemacro{\YGridStart}{-0.5*\NumberOfYGrid}%
\pgfmathtruncatemacro{\YGridEnd}{0.5*\NumberOfYGrid}%
\foreach \x in {\XGridStart,...,\XGridEnd} {
\node [anchor=north] at (\x,-0.5\ht\TempBox-1ex) {$\x$};
}
\foreach \y in {\YGridStart,...,\YGridEnd} {
\node [anchor=east] at (-0.5\wd\TempBox-0.5em,\y) {$\y$};
}
\node[inner sep=0] at (0,0) {\usebox\TempBox};
\draw[white](-0.5\wd\TempBox,-0.5\ht\TempBox) grid (0.5\wd\TempBox,0.5\ht\TempBox);
\draw[line width=2pt,blue] (0,0) ellipse (6.3 and 3.4);
\draw[line width=2pt,blue] (0,0) ellipse (6.9 and 4.0);
\end{tikzpicture}
\end{document}
答案2
我更新了onimage
“从答案到包”项目的包,以支持绝对间隔的帮助行。您可以.dtx
从 Launchpad下载.然后运行(pdf)latex
它以获取包(.sty
)文件和文档。
要获得间距为 1cm 的网格线,可以按如下方式使用:
\documentclass{article}
\usepackage{onimage}
\begin{document}
\begin{tikzonimage}[width=0.8\textwidth]{some_image.jpg}
[tsx/show help lines=1cm,tsx/scale cs=none]
% Some optional TikZ drawing commands here.
\end{tikzonimage}
\end{document}
的第一个可选参数tikzonimage
被传递给\includegraphics
,然后是文件名,然后是 TikZ 选项。
答案3
这是另一个版本,图片上显示的是数字而不是轴,其代码与彼得的略有不同。
\documentclass{article}
\usepackage{tikz}
\usepackage{graphicx}
\newsavebox\TempBox
\savebox\TempBox{\includegraphics{batman.jpg}}
\begin{document}
\begin{tikzpicture}
\node[inner sep=0] at (0,0) {\usebox\TempBox};
\draw[white](-0.5\wd\TempBox,-0.5\ht\TempBox) grid (0.5\wd\TempBox,0.5\ht\TempBox);
\pgfmathparse{int(-0.5\wd\TempBox/28.4)}%
\edef\ww{\pgfmathresult}
\pgfmathparse{int(-0.5\ht\TempBox/28.4)}%
\edef\hh{\pgfmathresult}
% I want to put integer labels for navigation purpose. The following code cannot be compiled!
\foreach \x in {\ww,...,-\ww} { \node [white,anchor=north] at (\x,0) {\x}; }
\foreach \y in {\hh,...,-\hh} { \node [white,anchor=east] at (0,\y) {\y}; }
\draw[line width=2pt,blue] (0,0) ellipse (6.3 and 3.4);
\draw[line width=2pt,blue] (0,0) ellipse (6.9 and 4.0);
\end{tikzpicture}
\end{document}