Tikz, Specifying Exact Dimensions of Shapes on Letterpaper

Am I encountering some strange behavior when trying to draw a rectangle with Tikz.

I'd like to create a 3inch-by-5inch rectangle with rounded corners on a piece of letterpaper that I can later cut out. Below is my MWE.

I understand that the Tikz math parser turns everything into points. An 11x8.5 letterpaper is 612 × 792 points. I'm wondering then why my rectangle runs way off the page when I specify the dimensions in inches. Shouldn't they be converted to points and be well within the dimensions of the page?

I would appreciate any enlightenment on this matter.

\documentclass[11pt,letterpaper,landscape]{article}
\usepackage{tikz}
\pgfmathsetmacro{\cardwidth}{5in}
\pgfmathsetmacro{\cardheight}{3in}

\begin{document}
\centering
\begin{tikzpicture}
\draw[style=help lines] (0,0) grid (\cardwidth,\cardheight);
\draw[rounded corners=0.2cm] (0,0) rectangle (\cardwidth,\cardheight);
\end{tikzpicture}
\end{document}


The command \pgfmathsetmacro will convert your expression to pt and then throw away the unit. If you \show\cardwidth you can see this in your log file:

> \cardwidth=macro:
->361.34999.


This number is, when used in a coordinate, scaled with the TikZ base unit, which is by default 1 cm. You have several options here:

1. Don't use \pgfmathsetmacro but use standard LaTeX lengths (or if you need to give an expression to \setlength use \pgfmathsetlength).

\documentclass[11pt,letterpaper,landscape]{article}
\usepackage{tikz}
\newlength\cardwidth
\newlength\cardheight
\setlength\cardwidth{5in}
\setlength\cardheight{3in}

\begin{document}
\centering
\begin{tikzpicture}
\draw[style=help lines] (0,0) grid (\the\cardwidth,\the\cardheight);
\draw[rounded corners=0.2cm] (0,0) rectangle (\the\cardwidth,\the\cardheight);
\end{tikzpicture}
\end{document}

2. Change the TikZ base unit to 1pt.

\documentclass[11pt,letterpaper,landscape]{article}
\usepackage{tikz}
\pgfmathsetmacro{\cardwidth}{5in}
\pgfmathsetmacro{\cardheight}{3in}

\begin{document}
\centering
\begin{tikzpicture}[x=1pt,y=1pt]
\draw[style=help lines] (0,0) grid (\cardwidth,\cardheight);
\draw[rounded corners=0.2cm] (0,0) rectangle (\cardwidth,\cardheight);
\end{tikzpicture}
\end{document}

3. Add an explicit unit when restoring the saved dimensions, i.e. \cardwidth pt instead of \cardwidth.

\documentclass[11pt,letterpaper,landscape]{article}
\usepackage{tikz}
\pgfmathsetmacro{\cardwidth}{5in}
\pgfmathsetmacro{\cardheight}{3in}

\begin{document}
\centering
\begin{tikzpicture}
\draw[style=help lines] (0,0) grid (\cardwidth pt,\cardheight pt);
\draw[rounded corners=0.2cm] (0,0) rectangle (\cardwidth pt,\cardheight pt);
\end{tikzpicture}
\end{document}

• Nice - Thank you. I believe I follow now. It is better in this case to just define a custom length using \setlength Jan 10 '18 at 2:15