Using macros with tikzpictures?

I tried to compress tikzpictureusing the following newcommand:

\newcommand{\tchild}[3]{ child { node{#2} #3 edge from parent node[above]{#1} } }
%intended usage: \tchild{edge label}{vertex label}{child nodes}

If I applied it to the following example, I will get a working document. However, the example below pdflatexgives Package pgf Error: No shape named is known.(note the double space between "named" and "is"). If you manually deploy the second tchild, I get a working document. Any ideas what is wrong here?

\begin{tikzpicture}
    \node{0} [grow'=right]
        \tchild{0}{1}{}
        \tchild{1}{0}{};
\end{tikzpicture}
+3
source share
1 answer

Edit: for some really cool (and working) PerlTeX examples with TikZ see this TUGboat article !

, , TikZ- , LaTeX. , pgfmanual tikz tikzpicture (., , . 223 pgfmanual.pdf, (IV)).

, , PerlTeX, . , .

: (. OLD) , TikZ PerlTeX, . , node, , , -. (:), (,). , . , OLD.

\documentclass{article}
\usepackage{perltex}

\usepackage{tikz}

\perlnewcommand{\tree}[3]{ 
  my ($root,$root_opts,$children) = @_;
  my @children = split(/\:/, $children);

  my $return = '';

  $return .= sprintf( "\\node{%s} \[%s\]\n", $root,$root_opts);

  foreach my $child (@children) {
    my ($edge, $vertex, $child_nodes) = split(/,/, $child);
    $child_nodes ||= '';
    $return .= sprintf("child { node{%s} %s edge from parent node[above]{%s} }\n",$vertex,$child_nodes,$edge);
  }
  $return .= "\;\n"; 
  return $return;
}

\begin{document}
\begin{tikzpicture}
%    \node{0} [grow'=right]
%      child { node{1}  edge from parent node[above]{0} }
%     child { node{0}  edge from parent node[above]{1} };
  \tree{0}{grow'=right}{0,1:1,0}
\end{tikzpicture}
\end{document}

--- BEGIN OLD ---

- , (, - , TikZ ). , , PerlTeX TikZ. perl TikZ . , . , perltex --latex=pdflatex text.tex:

\documentclass{article}
\usepackage{perltex}

\usepackage{tikz}

\perlnewcommand{\tchild}[3]{ 

  my ($edge, $vertex, $child) = @_;

  $child ||= '';

  my $return = 'child { node{' . $vertex . '} ' . $child . ' edge from parent node[above]{' . $edge . '} }';

  return $return;
}

\begin{document}
\begin{tikzpicture}
    \node{0} [grow'=right]
        \tchild{0}{1}{}
        \tchild{1}{0}{}
    ;
\end{tikzpicture}
\end{document}

--- ---

, , , , # 3, , , , (..\tchild [] {0} {1}).

0

Source: https://habr.com/ru/post/1773489/


All Articles