[Tex/LaTex] Hatching a rectangle with TikZ leaves margin

patterntikz-pgf

Hej everybody! 🙂

I'm currently taking my first steps with TikZ, and when trying to plot some simple rectangle shapes I noticed a rather weird effect that occurs when I try to fill the rectangles with a hatch pattern. Sometimes (!), the hatch pattern doesn't fill the rectangle all the way and there is some small margin:


Here is a minimal working example that demonstrates the effect. I have compiled it on both on my own system (MacTeX 2014, tikz.sty from 2013/12/13, i.e. v3.0.0 (rcs-revision 1.142)) and on Overleaf.com. The result is the same.

\documentclass{article}

\usepackage{float}
\usepackage{lipsum}
\usepackage{tikz}
\usetikzlibrary{patterns}

\begin{filecontents}{rectangles.tikz} 
    \begin{tikzpicture}
        \draw[] (0,0) rectangle (2,2);
        \draw[pattern=north east lines] (0,0) rectangle (1,2);
    \end{tikzpicture}
\end{filecontents} 

\begin{document}
    \subsection*{Effect, version 1:}
    \lipsum[2]
    \begin{figure}[H]    
        \centering
        \input{rectangles.tikz}
        \caption{Test Caption 1.}
    \end{figure}
    \lipsum[3]    
    \subsection*{Effect, version 2:}
    \begin{figure}[H]
        \input{rectangles.tikz}
        \caption{Test Caption 2.}
    \end{figure}
\end{document}

As I said, the effect does not always occur. For example, if I position my first figure using \begin{figure}[t], everything looks fine. I therefore suspect that the effect depends on the (absolute) position of the tikzpicture on the page?

Any information about why this is happening, or even better, how you can prevent this from happening, would be greatly appreciated 🙂

Edit: I am sorry, perhaps I was not clear enough perviously. By "margin", I mean that the pattern in the hatched part of the rectangle does not reach all the way to the border:

(I marked the critical/questionable area of the image in red.)

Edit 2: After doing some more research on incompatibilities between TikZ and Mac Preview, I also found this thread which basically describes the same problem: Incompatibility with TikZ and Mac OS X Preview [closed]

It therefore really seems like this is an older problem with the PDF rendering engine, and not TikZ. I filed a bug report to Apple and also posted the problem description to the Apple Support Community to maybe give a clue to people with the same or similar problems 🙂

Best Answer

As discussed in the comments above, at appears this is actually a bug in a PDF renderer on OS X used by FileLoupe and the built-in PDF viewer of the TeXPad App.