2

Why are these two diagrams different?

\documentclass{amsart}
\usepackage{amsmath}
\usepackage{amsfonts}

\usepackage{tikz}
\usetikzlibrary{calc,intersections}


\begin{document}


\begin{tikzpicture}

\foreach \y in {0,2,...,8}
{\foreach \x in {0,2,...,8}
{\draw[fill=black!50, xshift={(1/4)*sqrt(2)/2*\x*1cm}, yshift={(1/4)*sqrt(2)/2*\y*1cm}]
(0:{(1/4)*sqrt(2)/2}) -- (90:{(1/4)*sqrt(2)/2}) -- (180:{(1/4)*sqrt(2)/2}) -- (270:{(1/4)*sqrt(2)/2}) -- cycle;}
}

\draw ({-(1/4)*sqrt(2)/2},{-(1/4)*sqrt(2)/2}) rectangle ({(1/4)*9*sqrt(2)/2},{(1/4)*9*sqrt(2)/2});


\end{tikzpicture}


\begin{tikzpicture}[x=0.25cm, y=0.25cm]
\foreach \y in {0,2,...,8}
{\foreach \x in {0,2,...,8}
{\draw[fill=black!50, xshift={sqrt(2)/2*\x*1cm}, yshift={sqrt(2)/2*\y*1cm}] let \n1={sqrt(2)/2} in
(0:\n1) -- (90:\n1) -- (180:\n1) -- (270:\n1) -- cycle;}
}

\draw let \n1={sqrt(2)/2} in (-\n1,-\n1) rectangle ({9*\n1},{9*\n1});


\end{tikzpicture}


\end{document}

1 Answers1

4

The second picture has a [x=0.25cm, y=0.25cm] which the first one does not.

  • This is not equivalent to scale=0.25, which you may be looking for.
  • You tell TikZ explicitly in the second picture xshift={sqrt(2)/2*\X*1cm}, yshift={sqrt(2)/2*\Y*1cm}, so it does not care about the lengths of the unit vectors but just follows your instructions.
  • These lengths of the unit vectors do, however, have an impact on the sizes of the gray rectangles, and the rectangle at the end of the picture.
  • TikZ treats dimensionless and dimensionful coordinates very differently, we usually do not care about this because we choose the standard unit vectors. If you choose different ones, you feel this. The perhaps clearest explanation of these issues is in this nice answer, which discusses circles, not rectangles, but the qualitative argument is the same.

BTW, it is not a good practice IMHO if you declare the loop variables \x and \y in some code that makes use of the calc syntax, which defines \x and \y on its own. Use e.g. \X and \Y instead.

\documentclass{amsart}
\usepackage{tikz}
\usetikzlibrary{calc}


\begin{document}

\paragraph{Original picture}~\par\noindent
\begin{tikzpicture}

\foreach \Y in {0,2,...,8}
{\foreach \X in {0,2,...,8}
{\draw[fill=black!50, xshift={(1/4)*sqrt(2)/2*\X*1cm}, yshift={(1/4)*sqrt(2)/2*\Y*1cm}]
(0:{(1/4)*sqrt(2)/2}) -- (90:{(1/4)*sqrt(2)/2}) -- (180:{(1/4)*sqrt(2)/2}) -- (270:{(1/4)*sqrt(2)/2}) -- cycle;}
}

\draw ({-(1/4)*sqrt(2)/2},{-(1/4)*sqrt(2)/2}) rectangle ({(1/4)*9*sqrt(2)/2},{(1/4)*9*sqrt(2)/2});
\end{tikzpicture}

\bigskip
\hrule
\bigskip

\paragraph{Second picture} This changes the unit vectors/units but the \texttt{xshift}s and
\texttt{yshift}s are specified explicitly, i.e.\ with explicit lengths, and do
\emph{not} get multiplied by the unit vectors:\par\noindent
\begin{tikzpicture}[x=0.25cm, y=0.25cm]
\foreach \Y in {0,2,...,8}
{\foreach \X in {0,2,...,8}
{\draw[fill=black!50, xshift={sqrt(2)/2*\X*1cm}, yshift={sqrt(2)/2*\Y*1cm}] 
let \n1={sqrt(2)/2} in 
(0:\n1) -- (90:\n1) -- (180:\n1) -- (270:\n1) -- cycle;}
}

\draw let \n1={sqrt(2)/2} in (-\n1,-\n1) rectangle ({9*\n1},{9*\n1});
\end{tikzpicture}

\bigskip
\hrule
\bigskip


\paragraph{Alternative} This is what you may be looking for:\par\noindent
\begin{tikzpicture}[scale=0.25]
\foreach \Y in {0,2,...,8}
{\foreach \X in {0,2,...,8}
{\draw[fill=black!50, xshift={sqrt(2)/2*\X*1cm}, yshift={sqrt(2)/2*\Y*1cm}] 
let \n1={sqrt(2)/2} in 
(0:\n1) -- (90:\n1) -- (180:\n1) -- (270:\n1) -- cycle;}
}

\draw let \n1={sqrt(2)/2} in (-\n1,-\n1) rectangle ({9*\n1},{9*\n1});
\end{tikzpicture}
\end{document}

enter image description here

  • Are you saying that xshift={sqrt(2)/2*\x*1cm} and yshift={sqrt(2)/2*\y*1cm} ignore the options x=0.25cm, y=0.25cm in the tikzpicture environment? – A gal named Desire May 06 '19 at 21:27
  • What is the difference between the options x=0.25cm, y=0.25cm and scale=0.25 in the tikzpicture environment? – A gal named Desire May 06 '19 at 21:30
  • 1
    @AgalnamedDesire Yes, x=0.25cm, y=0.25cm and scale=0.25 are very different, at least in principle. So are the coordinates, say, (0,1) and (0,1cm). Apart from this answer maybe this answer may provide some additional insights. –  May 06 '19 at 22:51