Configuración de estilos en tikzset

Configuración de estilos en tikzset

Me enfrento a varios problemas al intentar crear una imagen tikzsety cargarla con diferentes variables.

El tikzset incluye estilos para:

  1. partes de la imagen tikz (en el ejemplo, los nodos) y
  2. partes que podrían agregarse (en el ejemplo, la ruta)

Mientras tanto, las imágenes deberían tener una escala diferente, lo que parece ser un problema con mi código actual.

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

Mis problemas son: si uso scopeel estilo funciona, pero scaleno. Si no uso scopelos estilos, no funcionará, pero scalefunciona. En ambos escenarios, definir un estilo para algo más fuera del tikzset pero dentro del tikzset tikzpictureno funcionará.

Estoy bastante seguro de que el problema de la última parte radica en algún lugar de la clave de los estilos que defino, pero no sé cómo llamarlo correctamente. ¿Alguien tiene una idea sobre cómo crear un tikzset que defina estilos para el tikzpicture al que se llama y que también tenga contenido que sea escalable a través del entorno tikzpicture?

Muchas gracias

Respuesta1

Hay dos problemas:

  1. Si dices code={...}, ...tiene que haber algún código, no algo así key=blabla. TúpoderSin embargo , defina claves usando \tikzset, por ejemplo, .
  2. Si es así n args=2, los dos argumentos deben pasarse a la picvía \pic {withscope={3}{8}};y no\pic {withscope={3,8}};

Teniendo esto en cuenta, el MWE se convierte en

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

ingrese la descripción de la imagen aquí

información relacionada