Tikz 和包围边框

Tikz 和包围边框

我如何才能在这个 MWE 中的蓝线周围添加包围边框?

\documentclass[tikz,border=5mm]{standalone}
\begin{document}
\begin{tikzpicture}
\draw[blue, line width=10pt](0,0)--(90:1.5)--(60:2.5)--(30:3.0); 
\end{tikzpicture}
\end{document}

在此处输入图片描述

PS. 我发现在之前的回答中,使用 MetaPost 也可以实现类似的效果https://tex.stackexchange.com/a/330016,但我想知道 Tikz 是否也能做到这一点。

答案1

shorten这是具有负值并声明为添加的样式的Salim Bou 解决方案preaction

样式border有三个参数:下线宽度、边框宽度(应用于shorten)和颜色。

\documentclass[tikz,border=5mm]{standalone}
\begin{document}
\begin{tikzpicture}[border/.style n args={3}{
    preaction={draw=#3, line width=#1, shorten <=-#2, shorten >=-#2}}]
\draw[blue, line width=10pt, border={14pt}{2pt}{green}](0,0)--(90:1.5)--(60:2.5)--(30:3.0); 

\draw[blue, line width=10pt, border={14pt}{2pt}{green}](3,0)rectangle++(2,3); 

\draw[blue, line width=10pt, border={14pt}{2pt}{green}] (7,1) circle(1cm);

\end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

基于的解决方案这个答案。此答案带有Outline样式,该样式有一个参数,即从中心到线到轮廓的距离。宽度是此长度的两倍。

\documentclass[tikz,border=5pt]{standalone}
\usetikzlibrary{decorations}

% based on https://tex.stackexchange.com/a/103088/121799
\def\pgfdecoratedcontourdistance{0pt}

\pgfkeys{/pgf/decoration/contour distance/.code={%
    \pgfmathparse{#1}%
    \let\pgfdecoratedcontourdistance=\pgfmathresult},%
    /pgf/decoration/contour name/.store in=\ContourName,
    /pgf/decoration/contour name=mycontour
}

\pgfdeclaredecoration{contour lineto}{start}
{
    \state{start}[next state=draw, width=0pt]{
    \pgfcoordinate{\ContourName-0}{\pgfpoint{0pt}{\pgfdecoratedcontourdistance}}
        \pgfpathlineto{\pgfpoint{0pt}{\pgfdecoratedcontourdistance}}%
    }
    \state{draw}[next state=draw, width=\pgfdecoratedinputsegmentlength]{       
        \pgfmathparse{-\pgfdecoratedcontourdistance*cot(-\pgfdecoratedangletonextinputsegment/2+90)}%
        \let\shorten=\pgfmathresult%
        \pgfpathlineto{\pgfpoint{\pgfdecoratedinputsegmentlength+\shorten}{\pgfdecoratedcontourdistance}}%  
    %\stepcounter{Outline}
    \pgfcoordinate{\ContourName-1}{\pgfpoint{\pgfdecoratedinputsegmentlength+\shorten}{\pgfdecoratedcontourdistance}}
    }
    \state{final}{
        \pgfpathlineto{\pgfpoint{\pgfdecoratedinputsegmentlength}{\pgfdecoratedcontourdistance}}%
        \pgfpathlineto{\pgfpoint{\pgfdecoratedinputsegmentlength}{0pt}}
    }   
}
\tikzset{Outline/.style={ postaction={
        decoration={contour lineto, contour distance=-#1,contour name=mycontourA},draw=blue,
         decorate},
        postaction={
        decoration={contour lineto, contour distance=#1,contour name=mycontourB},draw=blue,
         decorate},}}
\begin{document}

\begin{tikzpicture}
\draw[red,line width=10pt](0,0)--(90:1.5)--(60:2.5)--(30:3.0); 
\path[blue,very thick,Outline=5pt](0,0)--(90:1.5)--(60:2.5)--(30:3.0); 

\begin{scope}[xshift=4cm]
\path[blue,line width=2pt,Outline=5pt](0,0)--(90:1.5)--(60:2.5)--(30:3.0); 
\end{scope}
\begin{scope}[xshift=8cm,scale=2]
\path[blue,line width=4pt,Outline=10pt](0,0)--(90:1.5)--(60:2.5)--(30:3.0); 
\end{scope}
\end{tikzpicture}
\end{document}

在此处输入图片描述

笔记:这仅适用于非封闭的多边形。

相关内容