4

Is there a way to pre- and append code (text and commands) to all nodes with the rectangle option?

I found this post which explains how to execute a command before every node, but this does not allow me to insert visible text, at least not when I tried it.

Edit:

\tikzset{
  ell/.style={
    % The shape:
    ellipse,
    %minimum width=6mm,
    minimum height=6mm,
    % The rest
    inner sep=0,
    draw=black,
    fill=white,
    font=\ttfamily},
  skip loop/.style={to path={-- ++(0,#1) -| (\tikztotarget)}}
}

{
  \tikzset{ell/.append style={text height=1.5ex,text depth=.25ex}}
}

\begin{tikzpicture}[
    >=latex,thick,
    /pgf/every decoration/.style={/tikz/sharp corners},
    line join=round,line cap=round,
    fuzzy/.style={decorate,
        decoration={random steps,segment length=0.5mm,amplitude=0.15pt}},
  ]

    \ttfamily
    \begin{scope}[start chain=1 going below,
            node distance=1mm,
            every node/.append style={on chain, scale=0.7,
                minimum width=5cm, align=center},
            every ell node/.append style={
                execute at begin node=(,
                execute at end node=)},
        scale=0.7,
        ]
        \node [ell] (stmt01) {2.  \verb|z = 20|};
    \end{scope}
\end{tikzpicture}

1 Answer 1

5

Welcome! Yes, it is.

\documentclass[tikz,border=3mm]{standalone}
\begin{document}
\begin{tikzpicture}[every rectangle node/.append style={
    execute at begin node=bla\space,
    execute at end node=\space pft}]
    \path node[rectangle] {hello} (0,-1) node[rectangle] {world};
\end{tikzpicture}
\end{document}

enter image description here

Since rectangle is the default,

 \path node {hello} (0,-1) node {world};

yields the same result.

UPDATE: As for your updated question, ell is not a shape but a style. So instead of every ell node/.append style= you need something like ell/.append style=.

\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{chains,shapes.geometric}
\begin{document}
\tikzset{
  ell/.style={
    % The shape:
    ellipse,
    %minimum width=6mm,
    minimum height=6mm,
    % The rest
    inner sep=0,
    draw=black,
    fill=white,
    font=\ttfamily},
  skip loop/.style={to path={-- ++(0,#1) -| (\tikztotarget)}}
}

  \tikzset{ell/.append style={text height=1.5ex,text depth=.25ex}}

\begin{tikzpicture}[
    >=latex,thick,
    /pgf/every decoration/.style={/tikz/sharp corners},
    line join=round,line cap=round,
    fuzzy/.style={decorate,
        decoration={random steps,segment length=0.5mm,amplitude=0.15pt}},
  ]


    \begin{scope}[start chain=1 going below,
            node distance=1mm,
            every node/.append style={on chain, scale=0.7,
                minimum width=5cm, align=center},
            ell/.append style={
                execute at begin node=(,
                execute at end node=)},
        scale=0.7,node font=\ttfamily
        ]
        \node [ell] (stmt01) {2.  \verb|z = 20|};
    \end{scope}
\end{tikzpicture}
\end{document}

Or you could append the stuff to each ellipse node with

\begin{scope}[start chain=1 going below,
        node distance=1mm,
        every node/.append style={on chain, scale=0.7,
            minimum width=5cm, align=center},
        every ellipse node/.append style={
            execute at begin node=(,
            execute at end node=)},
    scale=0.7,node font=\ttfamily
    ]
    \node [ell] (stmt01) {2.  \verb|z = 20|};
\end{scope}

enter image description here

7
  • In that case I'm doing something wrong, could you please take a look at the updated answer?
    – Consti P
    Commented Mar 14, 2020 at 14:43
  • 1
    @ConstiP ell is not a shape, but a style in your post. So instead of every ell node/.append style= you need something like ell/.append style=.
    – user194703
    Commented Mar 14, 2020 at 14:45
  • @schrödingers-cat Thank you. Good (?) news, now I see the ) at the end, ( is still missing...
    – Consti P
    Commented Mar 14, 2020 at 14:54
  • @ConstiP I added the full code to the answer, and get both ( and ).
    – user194703
    Commented Mar 14, 2020 at 14:59
  • @schrödingers-cat Yes I copied the relevant parts. I just checked and it seems the bounding box gets added, it's just that the text itself isn't visible. There is another scope coming afterwards but this doesn't seem to be the source of this. I'll need to look around, thanks for the great answer.
    – Consti P
    Commented Mar 14, 2020 at 15:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .