tikz 节点内的 \newcommand

tikz 节点内的 \newcommand

在此示例中

\documentclass[tikz,border=5pt]{standalone}
\usetikzlibrary{shapes.misc, positioning, calc}
\newcommand{\element}[1]{[draw, rectangle] {#1}}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
  \node [align=center](a) 
          [draw, rectangle]{a};
  \node [align=center, anchor = north](b) at ($(a.south) + (0, -0.3)$) 
          [draw, rectangle]{b};
  \node [align=center, anchor = west](c) at ($(a.east) + (0.3, 0)$)        
          \element{c};

\end{tikzpicture}
\end{document}

在节点处c我尝试使用\newcommand \element应该c以与节点相同的方式构造节点ab构造。

这种方法可行吗?假设我需要以重复且非平凡的方式构造多个节点。

答案1

也许你应该为节点定义一个 tikz 样式:

对于一个论点

\documentclass[tikz,border=5pt]{standalone}

\usetikzlibrary{shapes.misc, positioning, calc}
\usepackage{tikz}
\tikzset{
element/.style={draw,rectangle,node contents=#1}}
\begin{document}
\begin{tikzpicture}
  \node [align=center](a) 
          [draw, rectangle]{a};
  \node [align=center, anchor = north](b) at ($(a.south) + (0, -0.3)$) 
          [draw, rectangle]{b};
  \node [align=center, anchor = west,element=c,at={($(a.east) + (0.3, 0)$)}] ;

\end{tikzpicture}

\end{document}

针对 3 个参数

\documentclass[tikz,border=5pt]{standalone}

\usetikzlibrary{shapes.misc, positioning, calc}
\usepackage{tikz}
\tikzset{
element/.style n args={3}{draw=#2,shape=#3,node contents=#1}}
\begin{document}
\begin{tikzpicture}
  \node [align=center](a) 
          [draw, rectangle]{a};
  \node [align=center, anchor = north](b) at ($(a.south) + (0, -0.3)$) 
          [draw, rectangle]{b};
  \node [align=center, anchor = west,element={c}{blue}{rectangle},name=c,at={($(a.east) + (0.3, 0)$)}] ;

\end{tikzpicture}
\end{document}

相关内容