在 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。你不過,可以使用 eg 來定義鍵\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}

在此輸入影像描述

相關內容