3

I am trying to understand tikz and coordinates when close to page margins.

I have the following MWE which is a 1cm graph paper, within a 1cm margin a4 page. There are no paragraph indentations.

I am expecting one page of grid.

What I get instead is a two-page document, where the grid is on the second page, as if it couldn't fit on the first page. And the page-numbering on both pages is pushed down so that only half the digit appears.

Is the line width that is making the picture slightly larger than the stated dimensions?

How do I fix this? And what is going wrong?

\documentclass{article}
\usepackage[a4paper, margin=1cm] {geometry}
\setlength{\parindent}{0ex}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\draw[line width=.4pt,draw=black] (0,0) rectangle(\textwidth,\textheight);
\end{tikzpicture}
\end{document}
6

Your rectangle is for \linewidth to big, consequently it is pushed to next page. Try

\documentclass{article}
\usepackage[a4paper, margin=1cm] {geometry}
\setlength{\parindent}{0ex}
\usepackage{tikz}
\usepackage{calc}

\begin{document}
\begin{tikzpicture}
\draw[line width=.4pt,draw=black] (0,0) rectangle(\textwidth-0.4pt,\textheight-0.4pt);
\end{tikzpicture}
\end{document} 

Adendum -- drawing the grid: Drawing grid by grid function is tricky: last line in top of the grid is drawn if the text height is higher form grid for some small amount (so far I didn't figured out the rule, how to determine this lenght):

enter image description here

\documentclass{article}
\usepackage[a4paper, width=19cm, height=270.14mm] {geometry}
\setlength{\parindent}{0ex}
\usepackage{tikz}
\usepackage{calc}

\begin{document}
\begin{tikzpicture}
\draw[line width=.4pt] (0,0) grid(\textwidth,\textheight-0.4pt);
\draw[line width=.2pt,step=1mm,draw=gray] (0,0) grid(\textwidth,\textheight-0.4pt);
\end{tikzpicture}
\end{document}

Less problematic is drawn grid in the loop, but in this case you should calculate in advance the number of lines in the grid [this should not be a problem :-) ]:

\documentclass{article}
\usepackage[a4paper, margin=1cm] {geometry}
\setlength{\parindent}{0ex}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
\foreach    \i in {1,...,18}{\draw[teal] (\i,0) -- + (0,\textheight);}
\foreach    \i in {1,...,26}{\draw[teal] (0,\i) -- ++ (\textwidth,0);}
\draw[line width=.4pt,draw=black] (0,0) rectangle(\textwidth-0.4pt,\textheight-0.4pt);
\end{tikzpicture}
\end{document}
| improve this answer | |
  • This solution works. But I don't understand why the logic of @StefanH is not working. – Tim Jan 11 '17 at 18:45
  • Using grid is tricky: last line is not visible, if the text area is the same width as grid. Here again is problem of some difference dependent on line width. I add to answer two example of drawing grid. – Zarko Jan 11 '17 at 19:44
3

The page numbering does not have anything to do with your picture. It is written below the text block and 1 cm is too narrow for it so only half will show up.

As you guessed it is the line width of your rectangle that makes the picture too high for the page, and hence is drawn on the next page. If you want to draw a line between two points the line width is expanded to both sides of that line. In the example below it can be seen that the first, black rectangle of size 3cm times 3cm expands outside the grid lines. In the second, green, rectangle the extra line width is compensated for.

\documentclass{article}
\usepackage[a4paper, margin=1cm] {geometry}
\setlength{\parindent}{0ex}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
  \draw[gray, thin](0,0) grid (10,10);
  \draw[line width=10pt] (2,2) rectangle (5,5);
  \draw[line width=10pt,green] (6cm+5pt,6cm+5pt) rectangle (9cm-5pt,9cm-5pt);
\end{tikzpicture}
\end{document}

enter image description here

| improve this answer | |
  • If the line width is spread on both sides of the line, I would have expected to adjust my MWE by 0.2pt, but in face I need to adjust by 0.4pt, as per @Zarko's answer. Have i understood correctly? – Tim Jan 11 '17 at 18:43
  • Yes, you have 0.2pt outside the rectangle on each side of it, so in total 0.4pt. – StefanH Jan 11 '17 at 19:27
  • Just to clarify, I remove half the line width on each side to fit the rectangle into the grid. In @Zarko's example he does not have to fit it, and then it is easier to just remove the whole line width (that is, twice half the line width) on one side. So it is the same thing in that case. – StefanH Jan 11 '17 at 19:44
2

If printed line width is not important, you can always clip the rectangle to desired dimensions and it will fit in one page. If line width is important, you can still clip it but using a double line width.

An exagerated example:

\documentclass{article}
\usepackage[a4paper, margin=1cm] {geometry}
\setlength{\parindent}{0ex}
\usepackage{tikz}
\begin{document}

\begin{tikzpicture}
\clip (0,0) rectangle (\textwidth,\textheight);
\draw[line width=1cm,draw=black] (0,0) rectangle(\textwidth,\textheight);
\end{tikzpicture}

\begin{tikzpicture}
\clip (0,0) rectangle (\textwidth,\textheight);
\draw[line width=2cm,draw=black] (0,0) rectangle(\textwidth,\textheight);
\end{tikzpicture}

\end{document}

enter image description here

Update

If the desired result is a grid on certain page position, you can use TiKZ absolute positioning system. As an example, compile twice following code:

\documentclass[a4paper]{article}
\usepackage[margin=1cm] {geometry}
\usepackage{tikz}
\begin{document}

\begin{tikzpicture}[overlay, remember picture]
\begin{scope}[shift={([shift={(1cm,-1cm)}]current page.north west)}]
\draw (0,0) grid ++(\textwidth,-\textheight);
\end{scope}
\end{tikzpicture}

\end{document}

The result will be:

enter image description here

For more complex grids, you can take a look at How can I make a perfect page grid that fits my page for measuring purposes in TikZ?

| improve this answer | |

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.