
Я сталкиваюсь с несколькими проблемами при попытке создать изображение tikzset
и загрузить его с различными переменными.
В комплект tikzset входят стили для:
- части рисунка тикз (в примере узлы) и
- части, которые могут быть добавлены (в примере путь)
Между тем, изображения должны масштабироваться по-разному, что, по-видимому, является проблемой моего текущего кода.
\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
не будет работать.
Я почти уверен, что проблема последней части кроется где-то в ключе стилей, которые я определяю, но я не могу понять, как правильно его вызвать. Есть ли у кого-нибудь идея, как создать tikzset, который определяет стили для tikzpicture, в котором он вызывается, и имеет также контент, который масштабируется через среду tikzpicture?
Большое спасибо
решение1
Есть две проблемы:
- если вы говорите
code={...}
,...
должен быть какой-то код, а не что-то вродеkey=blabla
. Выможетхотя определите ключи, используя eg\tikzset
. - Если у вас есть
n args=2
, два аргумента должны быть переданы вpic
via\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}