在 TikZ 中,如何使粗线与框的侧面完全连接?

在 TikZ 中,如何使粗线与框的侧面完全连接?

在我的图中,我有一条粗线与盒子的侧面相连。但是,这条线并没有完全与盒子相连。而且,这条线遮住了盒子的一部分(我不想要这样)。

因此,我希望线的下边缘与框完全连接,并防止线的上边缘覆盖框的一部分。PGF 手册中可能提到了如何处理这个问题,但我找不到它(而且由于我不知道这方面的术语,所以我也无法搜索它)。

在此处输入图片描述

这是上图的最小示例。

\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}

答案1

\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} 

在此处输入图片描述

第二种解决方案

\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} 

第三种解决方案

\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} 

第四版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}

在此处输入图片描述

答案2

可以通过以下方式实现稍微紧凑的版本

\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}

手册上说这只适用于专家;所以我犯了罪。因此,请保密。(或者它确实不适合这种用途:P)

在此处输入图片描述

答案3

据我所知,这是不可能的。不久前我也遇到过同样的问题,最后我把线稍微移到了框内,并确保框画在线上,这样它就遮住了框内的线的部分。

您可以使用背景层来实现这一点:

\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}

相关内容