[Tex/LaTex] How to put environment text inside node

environmentsnodessectioningtikz-pgf

Consider a simple environment in the documentclass as

  \newenvironment{abstract}{
    \node (tbl) {

                };
  }

The content of

\begin{abstract}
Content
\end{abstract}

will goes after the node. How to put the content inside the node? The node has been used by tikzpicture to draw picture backgrounds.

This can also apply to Section: how we can put the section header and text inside a node? a simple section header is as

\newcommand\section{\@startsection {section}{1}{\z@}%
                                   {-3.5ex \@plus -1ex \@minus -.2ex}%
                                   {2.3ex \@plus.2ex}%
                                   {\normalfont\Large\bfseries}}

This only defines the section header text. How we can play with the section header and text within node?

UPDATE: an example in reference to discussion with @MartinScharrer

  \newenvironment{abstract}{
    \begin{center}
    \begin{tikzpicture}
    \node (x) {
    This is inside node
    };
    \begin{pgfonlayer}{background}
    \draw[rounded corners, top color=red, bottom color=black, draw=white]
    ($(x.north west)+(0.14,0)$) rectangle ($(x.north east)-(0.13,0.9)$);
    \end{pgfonlayer}
    \end{tikzpicture}
    \end{center}
  }

Best Answer

You can split the code of a \node by replacing the { and } with \bgroup and \egroup. This is possible because TikZ reads the content of the node as box and not as macro argument. Note that you need to set special options to allow for paragraph or line breaks inside the node or add a minipage (or varwidth) environment.

\newenvironment{abstract}[1][]{% most likely \renewenvironment!
  \begin{tikzpicture}[<picture options>]
  \node [<default options>,#1] (tbl) \bgroup
  %\begin{minipage}{<some width>}
}{%
  %\end{minipage}
  \egroup;
  \end{tikzpicture}%
}

Usage:

\begin{abstract}
  My abstract ...
\end{abstract}

An alternative is to use the environ package to collect the whole environment content as \BODY and then use \node [<options>] (<name>) {\BODY};.