TikZ:tikzset 设置宏并传递键值

TikZ:tikzset 设置宏并传递键值

我制作了一个名为 gen(erator) 的 TikZ 图片。我想建立一个 TikZ 键,将 rotate=90 传递给此图片,但同时设置一个名为 的宏\complabelanchor来更改锚点,这样如果我传递pic text给图片,文本就会锚定在东边并出现在左侧,如下图所示。

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
\usetikzlibrary{calc}
\usetikzlibrary{quotes,babel}

\def\myunitlength{5mm}

\tikzset{
  LeftComp/.style={....} %% I would like LeftComp to pass 'rotate=-90' to the pic, and define \complabelanchor as "east"
}

\gdef\complabelanchor{north}

\tikzset{
gen/.pic = { %
            \node[draw, inner sep=0, circle=0.7*\myunitlength,minimum size=0.7*\myunitlength] (-dum) at (0,-\myunitlength) {};
            \draw(0,0) edge (-dum);
            \coordinate (-b) at (0.0, -1.6*\myunitlength);
            \node [inner sep=0pt,align=left,font=\footnotesize,anchor=\complabelanchor,\tikzpictextoptions] at (-b) {\tikzpictext};
            \gdef\complabelanchor{north}

    }
}
\begin{document}


\begin{tikzpicture}
        \pic [pic text=default behavior] at (0,0) {gen};

        %%%% I'm trying to capture this behavior with a single key called LeftComp
        \gdef\complabelanchor{east} %%
        \pic [rotate=-90,
            %%%%
            pic text=left side component] at (0,0) {gen};
    
\end{tikzpicture}

\end{document}

在此处输入图片描述

答案1

您可以向您的图片添加一个参数(具有north默认值)来获取方向:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning,calc,quotes,babel}
\def\myunitlength{5mm}

\tikzset{
  gen/.pic={
    \node[draw, inner sep=0, circle=0.7*\myunitlength,minimum size=0.7*\myunitlength] (-dum) at (0,-\myunitlength) {};
    \draw(0,0) edge (-dum);
    \coordinate (-b) at (0.0, -1.6*\myunitlength);
    \node [inner sep=0pt,align=left,font=\footnotesize,anchor=#1,\tikzpictextoptions] at (-b) {\tikzpictext};
  },
  pics/gen/.default=north,
}

\begin{document}
\begin{tikzpicture}
  \pic [pic text=default behavior] at (0,0) {gen };
  \pic [rotate=-90, pic text=left side component] at (-3,0) {gen=east};  
\end{tikzpicture}
\end{document}

这是更接近您要求的第二个解决方案:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning,calc,quotes,babel}
\def\myunitlength{5mm}

\tikzset{
  gen orientation/.store in=\gencomplabel,
  gen orientation=north, % default value
  gen/.pic={
    \node[draw, inner sep=0, circle=0.7*\myunitlength,minimum size=0.7*\myunitlength] (-dum) at (0,-\myunitlength) {};
    \draw(0,0) edge (-dum);
    \coordinate (-b) at (0.0, -1.6*\myunitlength);
    \node [inner sep=0pt,align=left,font=\footnotesize,anchor=\gencomplabel,\tikzpictextoptions] at (-b) {\tikzpictext};
  },
  gen rotate/.is choice,
  gen rotate/bottom/.style={rotate=0,gen orientation=north},
  gen rotate/left/.style={rotate=-90,gen orientation=east},
  gen rotate/right/.style={rotate=90,gen orientation=west},
  gen rotate/top/.style={rotate=180,gen orientation=south},
}
\begin{document}
\begin{tikzpicture}
  \pic [gen rotate=bottom, pic text=default (bottom) behavior] at (0,0) {gen};
  \pic [gen rotate=left, pic text=left side component] at (-1,0) {gen};  
  \pic [gen rotate=right, pic text=right side component] at (-4,1) {gen};  
  \pic [gen rotate=top, pic text=top side component] at (0,1) {gen};  
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容