合并两个 Tikzset

合并两个 Tikzset

我有以下 MWE:

\documentclass[]{article}
\usepackage{comment} 
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{color}
\usepackage{xcolor} 

% tikz-related packages
\usepackage{tikz}
\usepackage[edges]{forest}
\usepackage[outline]{contour}
\usetikzlibrary{hobby}
\usetikzlibrary{calc,intersections,through,backgrounds,matrix,patterns}
\usetikzlibrary{decorations.pathreplacing}
\usetikzlibrary{decorations.markings}
\usetikzlibrary{fit,shapes.misc,arrows.meta}
\usepackage[normalem]{ulem}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usepgfplotslibrary{fillbetween}
\pgfdeclarelayer{bg}
\pgfsetlayers{bg,main}
\usepackage{pdfpages}

% tikzset for fillbetween
\tikzset{
hatch distance/.store in=\hatchdistance,
hatch distance=10pt,
hatch thickness/.store in=\hatchthickness,
hatch thickness=0.3pt
}

% tikzset for tangents - this is the part I need to combine with the above tikzset
\begin{comment}
\tikzset{mark tangent intersections with axes/.code={
\path let \p1=(tangent point-#1),
\p2=($(tangent unit vector-#1)-(tangent point-#1)$)
in ({\x1-\y1*\x2/\y2},0) coordinate (x-intersection-#1)
(0,{\y1-\x1*\y2/\x2}) coordinate (y-intersection-#1);},
mark tangent intersections with axes/.default=1,
tangent/.style={
decoration={
markings,% switch on markings
mark= at position #1 with
{
\coordinate (tangent point-\pgfkeysvalueof{/pgf/decoration/mark info/sequence number}) at (0pt,0pt);
\coordinate (tangent unit vector-\pgfkeysvalueof{/pgf/decoration/mark info/sequence number}) at (1,0pt);
\coordinate (tangent orthogonal unit vector-\pgfkeysvalueof{/pgf/decoration/mark info/sequence number})
at (0pt,1);
}
},
postaction=decorate
},
use tangent/.style={
shift=(tangent point-#1),
x=(tangent unit vector-#1),
y=(tangent orthogonal unit vector-#1)
},
use tangent/.default=1,
}
\end{comment}

% for shading between lines
\makeatletter
\pgfdeclarepatternformonly[\hatchdistance,\hatchthickness]{northeast}
{\pgfqpoint{0pt}{0pt}}
{\pgfqpoint{\hatchdistance}{\hatchdistance}}
{\pgfpoint{\hatchdistance-1pt}{\hatchdistance-1pt}}%
{
    \pgfsetcolor{\tikz@pattern@color}
    \pgfsetlinewidth{\hatchthickness}
    \pgfpathmoveto{\pgfqpoint{0pt}{0pt}}
    \pgfpathlineto{\pgfqpoint{\hatchdistance}{\hatchdistance}}
    \pgfusepath{stroke}
}

\pgfdeclarepatternformonly[\hatchdistance,\hatchthickness]{northwest}
{\pgfqpoint{0pt}{0pt}}
{\pgfqpoint{\hatchdistance}{\hatchdistance}}
{\pgfpoint{\hatchdistance-1pt}{\hatchdistance-1pt}}%
{
    \pgfsetcolor{\tikz@pattern@color}
    \pgfsetlinewidth{\hatchthickness}
    \pgfpathmoveto{\pgfqpoint{\hatchdistance}{0pt}}
    \pgfpathlineto{\pgfqpoint{0pt}{\hatchdistance}}
    \pgfusepath{stroke}
}
\makeatother



\begin{document}


        \begin{tikzpicture}
        \draw[name path = axis] (0,5) --(0,0)--(5,0)--(5,5)--(0,5);
        \draw [name path = B1, thick] (0,5)--(1,5)..controls (3,4) and (3.5,2)..(5,1)--(5,0);
        \draw [name path = B2, thick] (0,5)--(0,4)..controls (1,3.5) and (2,1.5)..(4,0)--(5,0);
        \draw [name path = B3, thick] (0,2.4)--(0,1)..controls (2,1) and (3,2.4)..(5,2.5);
        \draw [name path = B4, thick] (0,2.4)..controls (2,2.4) and (3,3.8)..(5,3.8)--(5,2.5);

        \path[name intersections={of = B2 and B4, by = I1}];
        \path[name intersections={of = B1 and B3, by = I2}];        

        \begin{pgfonlayer}{bg}
        \fill [pattern= northeast, hatch distance = 10pt,
        intersection segments={of=B1 and B2,sequence={L2--R2}}];
        \fill [pattern= northwest, hatch distance = 10pt,
        intersection segments={of=B3 and B4,sequence={L2--R2}}];
        \end{pgfonlayer}

        % the following code will not work
        %\draw[tangent=0.35] (1,-3)..controls (1,0) and (3,-3)..(3,0);
        %\draw[mark tangent intersections with axes, red, dashed] (x-intersection-1) -- (y-intersection-1);
        \end{tikzpicture}

\end{document}

请注意:

  • 其中一个tikzset被用来修改阴影线之间的距离。
  • tikzset环境中还包含另一个comment,我想使用它来为我提供tangent功能。
  • tikzpicture文档中,最后有一个部分被注释掉了,因为如果没有环境中的 tikzset 它就无法工作comment

我正在努力将这两个 tikzset 合并起来。我该怎么做?

答案1

我不太明白为什么你不能将它们组合起来使用,并放在一个中\tikzset(毕竟 tikzset 的参数是一个以逗号分隔的定义列表)。

此外,您的示例远非最小,您可能需要考虑使用您所包含的一些库,例如backgrounds

\documentclass{article}

% tikz-related packages
\usepackage{tikz}
\usetikzlibrary{calc,intersections,backgrounds,patterns}
\usetikzlibrary{decorations.pathreplacing}
\usetikzlibrary{decorations.markings}
\usetikzlibrary{arrows.meta}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}

% tikzset for fillbetween
\tikzset{
hatch distance/.store in=\hatchdistance,
hatch distance=10pt,
hatch thickness/.store in=\hatchthickness,
hatch thickness=0.3pt,
mark tangent intersections with axes/.code={
\path let \p1=(tangent point-#1),
\p2=($(tangent unit vector-#1)-(tangent point-#1)$)
in ({\x1-\y1*\x2/\y2},0) coordinate (x-intersection-#1)
(0,{\y1-\x1*\y2/\x2}) coordinate (y-intersection-#1);},
mark tangent intersections with axes/.default=1,
tangent/.style={
decoration={
markings,% switch on markings
mark= at position #1 with
{
\coordinate (tangent point-\pgfkeysvalueof{/pgf/decoration/mark info/sequence number}) at (0pt,0pt);
\coordinate (tangent unit vector-\pgfkeysvalueof{/pgf/decoration/mark info/sequence number}) at (1,0pt);
\coordinate (tangent orthogonal unit vector-\pgfkeysvalueof{/pgf/decoration/mark info/sequence number})
at (0pt,1);
}
},
postaction=decorate
},
use tangent/.style={
shift=(tangent point-#1),
x=(tangent unit vector-#1),
y=(tangent orthogonal unit vector-#1)
},
use tangent/.default=1,
}

% for shading between lines
\makeatletter
\pgfdeclarepatternformonly[\hatchdistance,\hatchthickness]{northeast}
{\pgfqpoint{0pt}{0pt}}
{\pgfqpoint{\hatchdistance}{\hatchdistance}}
{\pgfpoint{\hatchdistance-1pt}{\hatchdistance-1pt}}%
{
    \pgfsetcolor{\tikz@pattern@color}
    \pgfsetlinewidth{\hatchthickness}
    \pgfpathmoveto{\pgfqpoint{0pt}{0pt}}
    \pgfpathlineto{\pgfqpoint{\hatchdistance}{\hatchdistance}}
    \pgfusepath{stroke}
}

\pgfdeclarepatternformonly[\hatchdistance,\hatchthickness]{northwest}
{\pgfqpoint{0pt}{0pt}}
{\pgfqpoint{\hatchdistance}{\hatchdistance}}
{\pgfpoint{\hatchdistance-1pt}{\hatchdistance-1pt}}%
{
    \pgfsetcolor{\tikz@pattern@color}
    \pgfsetlinewidth{\hatchthickness}
    \pgfpathmoveto{\pgfqpoint{\hatchdistance}{0pt}}
    \pgfpathlineto{\pgfqpoint{0pt}{\hatchdistance}}
    \pgfusepath{stroke}
}
\makeatother



\begin{document}


        \begin{tikzpicture}
        \draw[name path = axis] (0,5) --(0,0)--(5,0)--(5,5)--(0,5);
        \draw [name path = B1, thick] (0,5)--(1,5)..controls (3,4) and (3.5,2)..(5,1)--(5,0);
        \draw [name path = B2, thick] (0,5)--(0,4)..controls (1,3.5) and (2,1.5)..(4,0)--(5,0);
        \draw [name path = B3, thick] (0,2.4)--(0,1)..controls (2,1) and (3,2.4)..(5,2.5);
        \draw [name path = B4, thick] (0,2.4)..controls (2,2.4) and (3,3.8)..(5,3.8)--(5,2.5);

        \path[name intersections={of = B2 and B4, by = I1}];
        \path[name intersections={of = B1 and B3, by = I2}];        

        \begin{scope}[on background layer]
        \fill [pattern= northeast, hatch distance = 10pt,
        intersection segments={of=B1 and B2,sequence={L2--R2}}];
        \fill [pattern= northwest, hatch distance = 10pt,
        intersection segments={of=B3 and B4,sequence={L2--R2}}];
        \end{scope}

        \draw[tangent=0.35] (1,-3)..controls (1,0) and (3,-3)..(3,0);
        \draw[mark tangent intersections with axes, red, dashed] (x-intersection-1) -- (y-intersection-1);
        \end{tikzpicture}

\end{document}

相关内容