网格尊重图像居中

网格尊重图像居中

我想使用 tikzpicture 在图像上定位一个间距均匀的网格。

以下是一个例子:

\documentclass[table]{beamer}

\usepackage{tikz}

\begin{document}

\begin{frame}{Grid respect image}
    \begin{tikzpicture}[overlay, remember picture, shift={(current page.center)}]
    \node (photo) [anchor=south west, inner sep=0pt] at (0,0) {\includegraphics[height=6cm]{image.jpg}};
    \begin{scope}[x={(photo.south east)}, y={(photo.north west)}]
        \draw[help lines, step=0.1, line width=0.5mm, color=red] (0.0,0.0) grid (1.0,1.0);
    \end{scope}
    \end{tikzpicture}
\end{frame}

\end{document}

此代码将返回以下图像:

enter image description here

如您所见,由于,图像位于画布的右上方anchor=south west

如果我用它替换,anchor=south west将会anchor=center返回一个错误Arithmetic overflow,但我不明白为什么。

谢谢

答案1

您忘记改变范围的原点:

\documentclass{beamer}
\usepackage{tikz}
\begin{document}
\begin{frame}{Grid respect image}
  \begin{tikzpicture}[overlay, remember picture, shift={(current page.center)}]
    \node (photo) [anchor=center, inner sep=0pt] at (0,0) {\includegraphics[height=6cm]{example-image}};
    \begin{scope}[shift={(photo.south west)},x={(photo.south east)}, y={(photo.north west)}]
      \draw[help lines, step=0.1, line width=0.5mm, color=red] (0.0,0.0) grid (1.0,1.0);
    \end{scope}
  \end{tikzpicture}
\end{frame}
\end{document}

enter image description here

答案2

我认为您的代码出现问题是因为您更改了原点,导致[x={(photo.south east)}, y={(photo.north west)}]无法按预期工作。但不要相信我。

作为替代方案,以下代码有效:

\documentclass{beamer}

\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}

\begin{frame}{Grid respect image}
    \begin{tikzpicture}[overlay, remember picture]
    \node[outer sep=0pt, inner sep=0pt] (photo) at (current page.center) {\includegraphics[height=6cm]{example-image}};
 \begin{scope}
        \foreach \i in {0,.2,...,1}{
            \draw ($(photo.south west)!\i!(photo.south east)$)--
            ($(photo.north west)!\i!(photo.north east)$);
            \draw ($(photo.south west)!\i!(photo.north west)$)--
            ($(photo.south east)!\i!(photo.north east)$);}
    \end{scope}
    \end{tikzpicture}
\end{frame}

\end{document}

enter image description here

答案3

删除 MWE 中图像的所有明确定位,并使用简单的\centering图像居中并绘制相对于图像的网格:

\documentclass[table]{beamer}
\usepackage{tikz}

\begin{document}
\begin{frame}{Grid respect image}
\centering
    \begin{tikzpicture}[inner sep=0pt]
\node (photo) {\includegraphics[height=6cm]{example-image}};
\draw[line width=0.5mm, color=red] (photo.south west) grid (photo.north east);
    \end{tikzpicture}
\end{frame}
\end{document}

给出:

enter image description here

答案4

除了@Zarko 的答案之外,您还需要像这样定义步骤,step=1/10 或者xstep=1/8如果ystep=1/9您需要 x 方向上的 8 个步骤,以及 y 方向上的 9 个步骤

\documentclass[table]{beamer}
\usepackage{tikz}

\begin{document}

\begin{frame}{Grid respect image}

\centering
\begin{tikzpicture}
    \node (photo) [anchor=south west,inner sep=0pt] {\includegraphics[height=6cm]{example-image}};
    \begin{scope}[x={(photo.south east)},y={(photo.north west)}]
     \draw[step=1/10, line width=0.5mm, color=red] (0,0) grid (1,1);
    \end{scope}
\end{tikzpicture}

\end{frame}

\end{document}

enter image description here

相关内容