2

I try to draw a grid on image with cm unit. I know many solution for this topic. My idea right now is try to calculate it as below! I guess should use \n1 and \n2 as foreach up limit (currently use \width and \height macro).

\documentclass[border=2mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes,calc}
\begin{document}
    \begin{tikzpicture}[]
    \def\width{6}
    \def\height{8}
    %\tikzstyle{every node}=[ultra thick, draw]
    \node[anchor=south west,opacity=0.5,inner sep=0] (image) at (0,0) {\includegraphics[width=0.9\textwidth]{example-image-a}};
    \draw[line width=2pt]
        let 
        \p1=(image.south west),
        \p2=(image.north east),
        \n1={(\x2-\x1)},
        \n2={(\y2-\y1)}  
        in node[anchor=west] at (0,-2) {\Huge \n1,\n2} 
        \foreach \col in {0,...,\height} {
        \foreach \row in {0,...,\width} {
            (0,\col) -- (\width,\col)
            (\row,0) -- (\row,\height)
        }}
        foreach \x in {0,1,...,\width} {node[anchor=north] at (\x,0) {\x}}
        foreach \y in {0,1,...,\height} {node[anchor=east] at (0,\y) {\y}}
        ;
    \end{tikzpicture}
\end{document}

Currently the unit of \n1 and \n2 is pt, maybe I need to convert it to cm unit but I don't how to do it.

My target is use \n1 and \n2 as the foreach up limit instead of \width and \height. so it will match the image exactly!

enter image description here

1

Here is a proposal. The conversion is in principle very simple: just multiply by the conversion factor

        \n1={(\x2-\x1)*1pt/1cm},
        \n2={(\y2-\y1)*1pt/1cm}  

However, even though you have now cm, TikZ will still claim these are points. So you need to politely convert it to admit these are cm.

\documentclass[border=2mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes,calc}
\begin{document}
    \begin{tikzpicture}[pfft/.code n args={2}{\pgfmathsetmacro{\tmp}{#2}
    \xdef#1{\tmp}}]
    \def\width{6}
    \def\height{8}
    %\tikzstyle{every node}=[ultra thick, draw]
    \node[anchor=south west,opacity=0.5,inner sep=0] (image) at (0,0) {\includegraphics[width=0.9\textwidth]{example-image-a}};
    \draw[line width=2pt]
        let 
        \p1=(image.south west),
        \p2=(image.north east),
        \n1={(\x2-\x1)*1pt/1cm},
        \n2={(\y2-\y1)*1pt/1cm}  
        in [pfft={\myx}{\n1},pfft={\myy}{\n2}]
        node[anchor=west] at (0,-2) {\Huge \myx cm,\myy cm} 
        \foreach \col in {0,...,\height} {
        \foreach \row in {0,...,\width} {
            (0,\col) -- (\width,\col)
            (\row,0) -- (\row,\height)
        }}
        foreach \x in {0,1,...,\width} {node[anchor=north] at (\x,0) {\x}}
        foreach \y in {0,1,...,\height} {node[anchor=east] at (0,\y) {\y}}
        ;
    \end{tikzpicture}
\end{document}

enter image description here

And you can use this to draw an integer grid as follows.

\documentclass[border=2mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes,calc}
\begin{document}
    \begin{tikzpicture}[pfft/.code n args={3}{\pgfmathsetmacro{\tmp}{#3}
    \xdef#1{\tmp}
    \pgfmathtruncatemacro{\tmp}{#3}
    \xdef#2{\tmp}
    }]
    \def\width{6}
    \def\height{8}
    %\tikzstyle{every node}=[ultra thick, draw]
    \node[anchor=south west,opacity=0.5,inner sep=0] (image) at (0,0) {\includegraphics[width=0.9\textwidth]{example-image-a}};
    \draw[line width=2pt]
        let 
        \p1=(image.south west),
        \p2=(image.north east),
        \n1={(\x2-\x1)*1pt/1cm},
        \n2={(\y2-\y1)*1pt/1cm}  
        in [pfft={\myx}{\mywidth}{\n1},pfft={\myy}{\myheight}{\n2}]
        node[anchor=west] at (0,-2) {\Huge \myx cm,\myy cm} 
        \foreach \col in {0,...,\myheight} {
        \foreach \row in {0,...,\mywidth} {
            (0,\col) -- (\mywidth,\col)
            (\row,0) -- (\row,\myheight)
        }}
        foreach \x in {0,1,...,\mywidth} {node[anchor=north] at (\x,0) {\x}}
        foreach \y in {0,1,...,\myheight} {node[anchor=east] at (0,\y) {\y}}
        ;
    \end{tikzpicture}
\end{document}

enter image description here

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.