在 tikzset 中设置样式

在 tikzset 中设置样式

当我尝试创建图片tikzset并使用不同的变量加载它时,我遇到了几个问题。

tikzset 包含以下样式:

  1. tikz 图片的部分(示例中为节点)和
  2. 可能添加的部分(示例中为路径)

同时,图片应该以不同的比例缩放,这似乎是我当前代码的一个问题

\documentclass{article}
\usepackage{tikz,pgfplots} 
\pgfplotsset{compat=1.16}  % 
\usetikzlibrary{positioning}

\begin{document}

\tikzset{
    pics/withscope/.style n args={2}{
        code = { %
            scale=#1,
            mypathstyle/.style={line width=#2mm, ->}, 
            \begin{scope}
                [scale=#1,  
                %mypathstyle/.style would only work here if path inside scope
                every node/.append style={transform shape},
                nodestyle/.style={circle,draw=black,fill=white,thick, font=\bfseries}]
                %nodes 
                \node(1) [nodestyle] {$1$};
                \node(2) [right=of 1] [nodestyle] {$2$};
            \end{scope}
        } %code
    }, % style
    pics/withscope/.default={1}{2}
} %tikzset  


\tikzset{
    pics/thisisscopeless/.style n args={1}{
        code = { %
            %Those wont do anything
            mypathstyle/.style={line width=#1mm, ->}, 
            every node/.append style={transform shape},
            nodestyle/.style wont work

            \node(1) [circle,draw=black,fill=white,thick, font=\bfseries] {$1$};
            \node(2) [right=of 1] [circle,draw=black,fill=white,thick, font=\bfseries] {$2$};
        } %code
    }, % style
    pics/thisisscopeless/.default={1}
} %tikzset


Loading without parameter works\\[2cm]
\begin{tikzpicture}
    \pic {withscope};
\end{tikzpicture} \\

putting everything in tikzpicture works\\

\begin{tikzpicture} [scale=1.5, 
    every node/.append style={transform shape},
    nodestyle/.style={circle,draw=black,fill=white,thick, font=\bfseries},
    mypathstyle/.style={line width=0.2mm, ->}] 
    \node(1) [nodestyle] {$1$};
    \node(2) [right=of 1] [nodestyle] {$2$};
    %path   
    \draw [mypathstyle] (1) -- (2) node[pos=0.5,above] {2};
\end{tikzpicture} \\

scale doesnt work with scope \\

\begin{tikzpicture}[scale=2,mypathstyle/.style={line width=0.5mm, ->}]
    \pic {withscope};
    %   %path   
    \draw [mypathstyle] (1) -- (2) node[pos=0.5,above]  {3};
\end{tikzpicture}   


without scope it works   \\

\begin{tikzpicture}[scale=2,
    every node/.append style={transform shape},
    mypathstyle/.style={line width=0.5mm, ->}]
    \pic {thisisscopeless};
    \draw [mypathstyle] (1) -- (2) node[pos=0.5,above]  {4};
\end{tikzpicture}   

with scope the scale in tkzpicture wont work. You can hand scale through tikzset, but this isnt great\\

\begin{tikzpicture}[scale=3,mypathstyle/.style={line width=0.5mm, ->}]
    \pic {withscope={3,8}};
    %   %path   
    \draw [mypathstyle] (1) -- (2) node[pos=0.5,above]  {5};
\end{tikzpicture}


without mypathstyle in tikzpicture gives an error as "mypathstyle" is unknown\\

\begin{tikzpicture}%[mypathstyle/.style={line width=0.5mm, ->}]
    \pic {withscope={4,8}};
    %   %path   
    \draw [mypathstyle] (1) -- (2) node[pos=0.5,above]  {6};
\end{tikzpicture}   


\end{document}

我的问题是:如果我使用scope样式,它会起作用,但scale不起作用。如果我不使用scope样式,它将不起作用,但scale会起作用。在这两种情况下,为 tikzset 之外但在 tikzset 之内的其他内容定义样式tikzpicture都不会起作用。

我相当确定最后一部分的问题在于我定义的样式的关键部分,但我不知道如何正确调用它。有谁知道如何创建一个 tikzset,它定义了调用它的 tikzpicture 的样式,并且还具有可通过 tikzpicture 环境扩展的内容?

非常感谢

答案1

存在两个问题:

  1. 如果你说code={...}...必须是一些代码,而不是像key=blabla。你但是,请使用例如来定义键\tikzset
  2. 如果有的话n args=2,两个参数必须传递给picvia \pic {withscope={3}{8}};,而不是\pic {withscope={3,8}};

考虑到这一点,MWE 变为

\documentclass{article}
\usepackage{tikz,pgfplots} 
\pgfplotsset{compat=1.16}  % 
\usetikzlibrary{positioning}

\begin{document}

\tikzset{
    pics/withscope/.style n args={2}{
        code = { %scale=#1,
            %mypathstyle/.style={line width=#2mm, ->}, 

            \begin{scope}
                [scale=#1,  
                %mypathstyle/.style would only work here if path inside scope
                every node/.append style={transform shape},
                nodestyle/.style={circle,draw=black,fill=white,thick, font=\bfseries}]
                %nodes 
                \node(1) [nodestyle] {$1$};
                \node(2) [right=of 1] [nodestyle] {$2$};
            \end{scope}
        } %code
    }, % style
    pics/withscope/.default={1}{2}
} %tikzset  


\tikzset{
    pics/thisisscopeless/.style n args={1}{
        code = { %

            \tikzset{mypathstyle/.style={line width=#1mm, ->}, 
            every node/.append style={transform shape},}
            %nodestyle/.style wont work

            \node(1) [circle,draw=black,fill=white,thick, font=\bfseries] {$1$};
            \node(2) [right=of 1] [circle,draw=black,fill=white,thick, font=\bfseries] {$2$};
        } %code
    }, % style
    pics/thisisscopeless/.default={1}
} %tikzset


Loading without parameter works\\[2cm]
\begin{tikzpicture}
    \pic {withscope};
\end{tikzpicture} 

putting everything in tikzpicture works

\begin{tikzpicture} [scale=1.5, 
    every node/.append style={transform shape},
    nodestyle/.style={circle,draw=black,fill=white,thick, font=\bfseries},
    mypathstyle/.style={line width=0.2mm, ->}] 
    \node(1) [nodestyle] {$1$};
    \node(2) [right=of 1] [nodestyle] {$2$};
    %path   
    \draw [mypathstyle] (1) -- (2) node[pos=0.5,above] {2};
\end{tikzpicture} 

scale doesn't work with scope \textbf{because the options are local}

\begin{tikzpicture}[scale=2,mypathstyle/.style={line width=0.5mm, ->}]
    \pic {withscope};
    %   %path   
    \draw [mypathstyle] (1) -- (2) node[pos=0.5,above]  {3};
\end{tikzpicture}   


without scope it works  

\begin{tikzpicture}[scale=2,
    every node/.append style={transform shape},
    mypathstyle/.style={line width=0.5mm, ->}]
    \pic {thisisscopeless};
    \draw [mypathstyle] (1) -- (2) node[pos=0.5,above]  {4};
\end{tikzpicture}   

with scope the scale in tkzpicture won't work. You can hand scale through
tikzset, but this isnt great.

\begin{tikzpicture}[scale=3,mypathstyle/.style={line width=0.5mm, ->}]
    \pic {withscope={3}{8}}; %corrected syntax for two arguments 
    %   %path   
    \draw [mypathstyle] (1) -- (2) node[pos=0.5,above]  {5};
\end{tikzpicture}


without mypathstyle in tikzpicture gives an error as "mypathstyle" is unknown\\

\begin{tikzpicture}%[mypathstyle/.style={line width=0.5mm, ->}]
    \pic {withscope={4}{8}};%corrected syntax for two arguments 
    %   %path   
    %\draw [mypathstyle] (1) -- (2) node[pos=0.5,above]  {6};
    % doesn't work because mypathstyle is defined in scope
\end{tikzpicture}   
\end{document}

在此处输入图片描述

相关内容