2

In this example

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

at node c I try to use \newcommand \element that should construct node c in the same way as nodes a and b are constructed.

Is such an approach possible? Assume I need to construct multiple nodes in a repetitive and nontrivial way.

5

Perhaps you should define a tikz style for nodes:

for one argument

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

for 3 arguments

\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}
  • 1
    How to change element=c if I have more than 1 argument to element? – Viesturs Oct 3 '18 at 7:06
  • I will reedit my answer considering your new requirement soon – Hafid Boukhoulda Oct 3 '18 at 7:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.