8

In my figure, I have a thick line that connects with the side of a box. However, the line does not connect completely with the box. Also, the line covers part of the box (which I don't want).

So, I want the lower edge of the line to connect completely with the box, and prevent the upper edge of the line to cover part of the box. It probably says how to deal with this in the PGF manual but I can't find it (and since I don't know the terminology for this, I can't search for it either).

enter image description here

Here is minimal example for the figure above.

\documentclass{standalone}

\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
  \node [draw, minimum size = 1cm] (box) {};
  \draw [line width = 5pt] (2cm, 2cm) -- (box.east);
\end{tikzpicture}
\end{document}
10
\documentclass{standalone}   
\usepackage{tikz}
\usetikzlibrary{calc} 
\begin{document} 

  \tikzset{%
    add/.style args={#1 and #2}{to path={%
 ($(\tikztostart)!-#1!(\tikztotarget)$)--($(\tikztotarget)!-#2!(\tikztostart)$)%
  \tikztonodes}}
}                

\begin{tikzpicture}  
   \node [ minimum size = 1cm] (box) {};  
   \draw [line width = 5pt,add=0 and .1] (2cm, 2cm) to (box.east);   
   \node [draw, fill=white,minimum size = 1cm]  {};

\end{tikzpicture}
\end{document} 

enter image description here

second solution

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc} 
\begin{document} 

  \tikzset{%
    add/.style args={#1 and #2}{to path={%
 ($(\tikztostart)!-#1!(\tikztotarget)$)--($(\tikztotarget)!-#2!(\tikztostart)$)%
  \tikztonodes}}
}                

\begin{tikzpicture}  
   \node [draw, fill=white,minimum size = 1cm] (box) {};  
   \clip (box.south east) rectangle (2.1cm, 2.1cm);   
   \draw [line width = 5pt,add=0 and .1] (2cm, 2cm) to (box.east);   
\end{tikzpicture}
\end{document} 

Third solution

\documentclass{article}   
\usepackage{tikz}
\usetikzlibrary{calc,backgrounds} 
\begin{document} 

  \tikzset{%
    add/.style args={#1 and #2}{to path={%
 ($(\tikztostart)!-#1!(\tikztotarget)$)--($(\tikztotarget)!-#2!(\tikztostart)$)%
  \tikztonodes}}
}                

\begin{tikzpicture}
   \draw[help lines] (0,0) grid (2,2);    
   \node [draw,minimum size = 1cm,fill=white] (box) {}; 
   \begin{pgfonlayer}{background}
      \draw [line width = 5pt,add=0 and .1] (2cm, 2cm) to (box.east);      
   \end{pgfonlayer}
\end{tikzpicture}

\end{document} 

Fourth version With reverseclip

\documentclass{article}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}[remember picture]

% A path that follows the edges of the current page
\tikzstyle{reverseclip}=[insert path={(current page.north east) --
  (current page.south east) --
  (current page.south west) --
  (current page.north west) --
  (current page.north east)}
]

\node [draw, minimum size = 1cm,inner sep=0pt,ultra thick,red] (box) at (.5cm,.5cm){};
\coordinate (D) at (box.north west);
\coordinate (C) at (box.north east);
\coordinate (B) at (box.south east);
\coordinate (A) at (box.south west);

\begin{pgfinterruptboundingbox} 
\path [clip] (A) -- (B) -- (C) -- (D) -- cycle [reverseclip];
\end{pgfinterruptboundingbox}

\draw [line width = 15pt] (.5cm, 1.5cm) to (.8cm,-.5cm);
\end{tikzpicture}
\end{document}

enter image description here

  • There are other possibilities like the use of reversclip or perhaps the use of layers. – Alain Matthes Jan 26 '12 at 16:58
  • I went with the third solution. It achieved what I wanted in the most intuitive way and with the least amount of code. Thanks. – gablin Jan 27 '12 at 8:58
  • @gablin yes it is the simplest and most natural. I use add because this option is in my packages but it's interesting in your case to use shorten >=-2mm] in \draw [line width = 5pt,shorten >=-2mm] (2cm, 2cm) to (box.east);. add is interesting if you want to add a percent of the length at each extremity. – Alain Matthes Jan 27 '12 at 9:07
3

A slightly more compact version can be achieved via

\begin{tikzpicture}
  \draw [line width=5pt,shorten >=-2mm] 
        node [fill=white,
        draw,
        line width=1pt,
        minimum size=1cm,
        append after command={(2cm, 2cm) -- (box.east) }] 
        (box) {};
\end{tikzpicture}

In the manual it says that this is for experts only; so I am committing a crime. Hence keep it between us. (or it is really not meant for such usage :P )

enter image description here

  • More compact but not necessarily clearer. You lost the line width, if you change line width in your picture, you need to modify your command. shorten >=-2mm in this case is more clearer and compact. I think the method with clip is preferable and it's better to avoid fill=white. – Alain Matthes Jan 26 '12 at 19:25
  • 1
    @Altermundus I fully agree with you. It's just a stretch of a command that I always wanted to use :) – percusse Jan 26 '12 at 19:27
0

As far as I am aware this is not possible. I had the same problem a while back, and I ended up moving the line slightly into the box and making sure the box was drawn over the line, so that it obscured the part of the line that was inside the box.

You can accomplish this using the background layer:

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{backgrounds}

\begin{document}
\begin{tikzpicture}
  \node [draw, fill=white, minimum size = 1cm] (box) {};
  \begin{pgfonlayer}{background}
    \draw [line width = 5pt, shorten >= -2mm] (2cm, 2cm) -- (box.east);
  \end{pgfonlayer}
\end{tikzpicture}
\end{document}
  • 1
    ([xshift=-2mm] box.east); this is wrong you don't have the good coordinates – Alain Matthes Jan 26 '12 at 17:21

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.