3

There is an undesirable path in my result. enter image description here

However, if I comment out the path intersection code (the fourth line from below), the undesirable path disappears. enter image description here

One of the paths involved in the intersection is a closed path (that is, \path [name path=Part2] (BRM1) -- (BR1) -- (BM1) -- (BL1) -- (BLM1) -- (BLM2) -- (BL2) -- (BM2) -- (BR2) -- (BRM2) -- cycle in my code). I cannot figure out why an intersection involving a closed path can generate some path outside the closed path. What I want to produce is as follows. enter image description here

I would appreciate your help with this situation.

\documentclass[12pt,a4paper]{article}
\usepackage{amssymb, amsmath, amsthm}
\usepackage{tikz}
\usetikzlibrary{angles,calc,intersections,quotes}

\begin{document}
\begin{center}
\begin{tikzpicture}
\coordinate (A) at (3,1.5);
\coordinate (BLM) at (0.5,-2);  %below leftmost
\coordinate (BRM) at (6,-1.5);  %below rightmost
\coordinate (BM) at (3.5,-1.8); %below middle 
\coordinate (BL) at (2,-2.1);   
\coordinate (BR) at (5,-2.5);   
\draw (BR) -- (BRM) -- (A) -- (BLM) -- (BL) -- (BM) -- cycle;
\foreach \pt in {BL,BM,BR}
    \draw (A) -- (\pt);
\foreach \pt/\xshift/\yshift in {BLM/-0.5cm/-0.7cm, BL/-0.2cm/-0.72cm, BM/0.1cm/-0.66cm, BR/0.4cm/-0.8cm, BRM/0.6cm/-0.6cm}
{
    \foreach \i in {1,2,3,4}
        \coordinate (\pt\i) at ($(A)+\i*(\xshift,\yshift)$);
}

\foreach \i in {1,2,3,4}
    \draw (BLM\i) -- (BL\i) -- (BM\i) -- (BR\i) -- (BRM\i);
    
\path [name path=Part2] (BRM1) -- (BR1) -- (BM1) -- (BL1) -- (BLM1) -- (BLM2) -- (BL2) -- (BM2) -- (BR2) -- (BRM2) -- cycle;
\foreach \x in {0,0.25,...,2.5}
{
    \path [overlay,name path=slashpath\x,draw] ($(BLM2)+(\x,0)-(0.35,0.2)$) -- +(50:1.5cm);
    \draw [name intersections={of=Part2 and slashpath\x},very thin] (intersection-1) -- (intersection-2);
}
\end{tikzpicture}
\end{center}

\end{document}
3
  • 1
    Your example is incomplete (\documentclass, document environment and required tikz libraries are all missing) and raises error even after I make it complete. Meanwhile, when x = 0, paths Part2 and slashpath0 (the left most slash line) do not intersect. What drawing effect do you want to achieve by generating intersections? Aug 10 '20 at 3:43
  • @muzimuzhiZ: Thank you for your reply. I have edited my post. Just as you say, x=0 is redundant in this case. Aug 10 '20 at 4:22
  • Still the same error ! LaTeX Error: Missing \begin{document}.
    – AndréC
    Aug 10 '20 at 4:41
9

I've placed % <---- this to highlight each change in your code.

Update 2: Clean up of the code

When performing the clip operation, the search for intersections is useless since the clip does this natively. The clip transforms the path you called name path=Part2 into a pochoir (stencil) and therefore everything that is hatched is colored inside this pochoir (clip area or stencil). So there's no need to search for intersections like you do.

The simplified code is now (same result):

\documentclass[12pt,a4paper]{article}
\usepackage{amssymb, amsmath, amsthm}
\usepackage{tikz}
\usetikzlibrary{angles,calc,intersections,quotes,patterns}

\begin{document}

\begin{center}
\begin{tikzpicture}
\coordinate (A) at (3,1.5);
\coordinate (BLM) at (0.5,-2);  %below leftmost
\coordinate (BRM) at (6,-1.5);  %below rightmost
\coordinate (BM) at (3.5,-1.8); %below middle 
\coordinate (BL) at (2,-2.1);   
\coordinate (BR) at (5,-2.5);   
\draw (BR) -- (BRM) -- (A) -- (BLM) -- (BL) -- (BM) -- cycle;
\foreach \pt in {BL,BM,BR}
    \draw (A) -- (\pt);
\foreach \pt/\xshift/\yshift in {BLM/-0.5cm/-0.7cm, BL/-0.2cm/-0.72cm, BM/0.1cm/-0.66cm, BR/0.4cm/-0.8cm, BRM/0.6cm/-0.6cm}
{
    \foreach \i in {1,2,3,4}
        \coordinate (\pt\i) at ($(A)+\i*(\xshift,\yshift)$);
}

\foreach \i in {1,2,3,4}
    \draw (BLM\i) -- (BL\i) -- (BM\i) -- (BR\i) -- (BRM\i);
    \begin{scope}% <---- add scope here
    
\path [clip%,name path=Part2 <-- add clip delete useless name path
] (BRM1) -- (BR1) -- (BM1) -- (BL1) -- (BLM1) -- (BLM2) -- (BL2) -- (BM2) -- (BR2) -- (BRM2) -- cycle;%<----- add clip here
\foreach \x in {0.25,.5,...,2.5}%<----- delete 0 here
%{ <-- useless group with only one TikZ command
    \path [%name path=slashpath\x, <-- useless name path
    draw] ($(BLM2)+(\x,0)-(0.35,0.2)$) -- +(50:1.5cm);
    %\draw [name intersections={of=Part2 and slashpath\x},very thin] (intersection-1) -- (intersection-2); <--- useless intersections
%}
    \end{scope}%<----- end of scope
\end{tikzpicture}
\end{center}

\end{document}

Update 1: Explanations about clip et scope

What is important here is the clip action. clip makes it possible to virtually cut out the figure as one would do with scissors. Clip acts like a pochoir (in french) (stencil in english). It is like a mask that hides everything that is not cut out. It only leaves the inner part of the clip (pochoir) visible. Once clipped, everything that is drawn only appears within the clip (pochoir).

Everything that is drawn after the clip remains inside this pochoir. Everything else is hidden, the constructions and drawings made remain inside the pochoir.

This scope environment is useful here because it limits the cutting when you want to work on a piece of the figure, but not on the whole figure.

It delimits a part of the figure. Here, it limits the pochoir within this scope environment.

When you get out of this scope, you can draw on the complete figure again.

In the case of your figure, the scope environment is not useful because you don't build anything more after this clip.

screenshot

\documentclass[12pt,a4paper]{article}
\usepackage{amssymb, amsmath, amsthm}
\usepackage{tikz}
\usetikzlibrary{angles,calc,intersections,quotes,patterns}

\begin{document}

\begin{center}
\begin{tikzpicture}
\coordinate (A) at (3,1.5);
\coordinate (BLM) at (0.5,-2);  %below leftmost
\coordinate (BRM) at (6,-1.5);  %below rightmost
\coordinate (BM) at (3.5,-1.8); %below middle 
\coordinate (BL) at (2,-2.1);   
\coordinate (BR) at (5,-2.5);   
\draw (BR) -- (BRM) -- (A) -- (BLM) -- (BL) -- (BM) -- cycle;
\foreach \pt in {BL,BM,BR}
    \draw (A) -- (\pt);
\foreach \pt/\xshift/\yshift in {BLM/-0.5cm/-0.7cm, BL/-0.2cm/-0.72cm, BM/0.1cm/-0.66cm, BR/0.4cm/-0.8cm, BRM/0.6cm/-0.6cm}
{
    \foreach \i in {1,2,3,4}
        \coordinate (\pt\i) at ($(A)+\i*(\xshift,\yshift)$);
}

\foreach \i in {1,2,3,4}
    \draw (BLM\i) -- (BL\i) -- (BM\i) -- (BR\i) -- (BRM\i);
    \begin{scope}% <---- add scope here
    
\path [clip,name path=Part2] (BRM1) -- (BR1) -- (BM1) -- (BL1) -- (BLM1) -- (BLM2) -- (BL2) -- (BM2) -- (BR2) -- (BRM2) -- cycle;%<----- add clip here
\foreach \x in {0.25,.5,...,2.5}%<----- delete 0 here
{
    \path [overlay,name path=slashpath\x,draw] ($(BLM2)+(\x,0)-(0.35,0.2)$) -- +(50:1.5cm);
    \draw [name intersections={of=Part2 and slashpath\x},very thin] (intersection-1) -- (intersection-2);
}
    \end{scope}%<----- end of scope
\end{tikzpicture}
\end{center}

\end{document}
6
  • 1
    Thank you for your kind reply, but I have no idea why I need scope here. Could you give me more clues? Thanks! Aug 10 '20 at 5:37
  • @lightweight I have added some explanations, I hope they are clear enough. If you still don't understand, just say so.
    – AndréC
    Aug 10 '20 at 8:51
  • Thank you for your very informative explanation, but I still have a problem. The path Part2 in my code (that is, (BRM1) -- (BR1) -- (BM1) -- (BL1) -- (BLM1) -- (BLM2) -- (BL2) -- (BM2) -- (BR2) -- (BRM2) -- cycle) is closed. I cannot figure out why an intersection involving a closed path can generate some path outside the closed path. Aug 11 '20 at 2:40
  • Your code on MikTeX with the pdfLaTeX engine does not compile. It does not generate anything. It only compiles when you iterate on {0.25,.5,...,2.5}. What engine do you compile with? pdfLaTeX? LaTeX? Lua LaTeX ? another one ?
    – AndréC
    Aug 11 '20 at 5:21
  • XeLaTeX. I wrote my above codes with other codes in a same document. When I compile, there is no error or warning (imgur.com/KJ2pBq0) except the weird result I described in my question. However, after your most recent comment, I test my codes pertaining to this question in a standalone document, and the compilation tells me No shape named 'intersection-1' is known (same for intersection-2). Now I guess .(period) of \x in slashpath\x may be the cause of problem. I replace {0.25,.5,...,2.5} (after \foreach) with {1,2,...,10} and (\x,0) with (0.25*\x,0), and it works. Aug 11 '20 at 6:48
6

To fill an area by slashing lines in tikz, using pattern is easier than intersections.

Here

  • tikz library patterns.meta is loaded and
  • \path [name path=Part2] is replaced with pattern={Lines[angle=50, distance=5pt]}.
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc, patterns.meta}

\begin{document}
\begin{tikzpicture}
  \coordinate (A) at (3,1.5);
  \coordinate (BLM) at (0.5,-2);  %below leftmost
  \coordinate (BRM) at (6,-1.5);  %below rightmost
  \coordinate (BM) at (3.5,-1.8); %below middle 
  \coordinate (BL) at (2,-2.1);   
  \coordinate (BR) at (5,-2.5);
  
  \draw (BR) -- (BRM) -- (A) -- (BLM) -- (BL) -- (BM) -- cycle;
  \foreach \pt in {BL,BM,BR}
      \draw (A) -- (\pt);
  \foreach \pt/\xshift/\yshift in {BLM/-0.5cm/-0.7cm, BL/-0.2cm/-0.72cm, BM/0.1cm/-0.66cm, BR/0.4cm/-0.8cm, BRM/0.6cm/-0.6cm}
  {
      \foreach \i in {1,2,3,4}
          \coordinate (\pt\i) at ($(A)+\i*(\xshift,\yshift)$);
  }
  
  \foreach \i in {1,2,3,4}
      \draw (BLM\i) -- (BL\i) -- (BM\i) -- (BR\i) -- (BRM\i);
  
  % draw pattern
  \path[pattern={Lines[angle=50, distance=5pt]}] 
    (BRM1) -- (BR1) -- (BM1) -- (BL1) -- (BLM1) -- (BLM2) -- (BL2) -- (BM2) -- (BR2) -- (BRM2) -- cycle;
\end{tikzpicture}
\end{document}

enter image description here

tikz library patterns.meta is documented in pgfmanual v3.1.5b, sec. 63.3. You can learn docs and examples of pattern Lines and its options in that subsection.

PS: pattern Lines is newly added in pgf/tikz v3.1.5, see corresponding release notes.

9
  • +1. This library was not documented in version 3.0.1a. Can you update this wiki? What's new in the latest version 3.1 of TikZ?
    – AndréC
    Aug 10 '20 at 15:47
  • @AndréC Pattern Lines is added in 3.1.5, and I've added a note to my answer. (Package patterns.meta is more complex, it has been there for a long time but not documented until 3.1.4.) The linked question seems to ask for a list of new features added in 3.1. I'm not sure if 3.1.5 features are suited there. Aug 10 '20 at 16:38
  • If it is relevant, because 3.1 means exactly all versions of 3.1, it is enough to specify that it is since 3.1.5 that this exists. The idea is to make the new features public because nobody ever reads the whole documentation (except the person who wrote it).
    – AndréC
    Aug 10 '20 at 16:43
  • 2
    @muzimuzhiZ @AndréC Thanks for all your help. I have encountered some problem using the library patterns.meta before: tex.stackexchange.com/q/549352/125357. My resulting lines generated by method like pattern={Lines[angle=50, distance=5pt]} are not smooth. My pdf reader is Adobe. Aug 11 '20 at 2:06
  • 1
    @lightweight After some tracing, it seems when the pattern cell (the repeated unit of drawing, in this example a square containing slashed lines) contains touching and slashed drawings, caused by both rounding errors of tex and the pdf reader, the inner borders of pattern cells won't look smooth (in some combination of distance and angle). Aug 11 '20 at 20:08

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.