
그림을 생성 tikzset
하고 다른 변수로 로드하려고 할 때 몇 가지 문제에 직면하고 있습니다.
tikzset에는 다음 스타일이 포함되어 있습니다.
- tikz 그림의 일부(예제에서는 노드) 및
- 추가될 수 있는 부분(예제에서는 경로)
그 사이에 그림의 크기가 다르게 조정되어야 합니다. 이는 현재 코드에 문제가 있는 것 같습니다.
\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 외부에서 다른 스타일을 정의하지만 내부에서는 tikzpicture
작동하지 않습니다.
마지막 부분의 문제는 내가 정의한 스타일의 키 어딘가에 있다고 확신하지만 올바르게 호출하는 방법을 알 수 없습니다. 호출되는 tikzpicture의 스타일을 정의하고 tikzpicture 환경을 통해 확장 가능한 콘텐츠도 포함하는 tikzset을 만드는 방법에 대한 아이디어가 있는 사람이 있습니까?
매우 감사합니다
답변1
두 가지 문제가 있습니다.
- 이라고 말하면 은
code={...}
같은...
코드가 아니라 코드여야 합니다key=blabla
. 너~할 수 있다하지만 eg 를 사용하여 키를 정의하세요\tikzset
. - 가 있는 경우 두 인수는 via
n args=2
로 전달되어야 하며pic
\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}