12

Please see the following definition:

     \tikzstyle{boxStyle} = [draw=blue!80, fill=blue!9, very thick,
        rectangle, rounded corners=3mm, inner sep=10pt, inner ysep=15pt]
     \tikzstyle{boxTitleStyle} =[fill=blue!80, rectangle, rounded corners=2mm,
        text=white, inner sep=10pt, inner ysep=7pt, left=10pt]

     \newcommand{\textBox}[2] {
       \begin{tikzpicture}
       \node [boxStyle]  (box) {\begin{minipage}{0.5\textwidth}#2\end{minipage}};
       \node[boxTitleStyle] at (box.north east) {#1};
       \end{tikzpicture}
     }

Now I want to covert it as an environment:

 \newenvironment{textBox}[1]
  {\begin{tikzpicture}\node [boxStyle]  (box) {\begin{minipage}{0.5\textwidth}}}
  {\end{minipage};\node[boxTitleStyle] at (box.north east) {#1};\end{tikzpicture}}

or,

 \newenvironment{textBox}[1]
  {\begin{tikzpicture}\node [boxStyle]  (box) \begin{minipage}{0.5\textwidth}}
  {\end{minipage};\node[boxTitleStyle] at (box.north east) {#1};\end{tikzpicture}}

are not true, any suggestions?

  • 4
    You might skip directly to mdframed package to have similar constructs. – percusse Dec 25 '12 at 15:37
9

You can start a node in the \begin part and finish it in the \end part using \bgroup and \egroup:

\documentclass{article}
\usepackage{tikz}
\tikzset{
  boxStyle/.style={draw=blue!80, fill=blue!9, very thick,
    rectangle, rounded corners=3mm, inner sep=10pt, inner ysep=15pt},
  boxTitleStyle/.style={fill=blue!80, rectangle, rounded corners=2mm,
    text=white, inner sep=10pt, inner ysep=7pt, left=10pt},
}

\newenvironment{textBox}[1]{%
  \def\title{#1}%
  \begin{tikzpicture}
    \node [boxStyle] (box)
    \bgroup\minipage{0.5\textwidth}%
}{
    \endminipage%
    \egroup;
    \node[boxTitleStyle] at (box.north east) {\title};
  \end{tikzpicture}%
}

\begin{document}
\begin{textBox}{Hello}
This is some text that goes in the minipage
\end{textBox}
\end{document}
| improve this answer | |
12

You can't start a node in the \begin part and finish it in the \end part. But the environ package comes to the rescue:

\documentclass{article}
\usepackage{tikz,environ}
\tikzstyle{boxStyle} = [draw=blue!80, fill=blue!9, very thick,
  rectangle, rounded corners=3mm, inner sep=10pt, inner ysep=15pt]
\tikzstyle{boxTitleStyle} =[fill=blue!80, rectangle, rounded corners=2mm,
  text=white, inner sep=10pt, inner ysep=7pt, left=10pt]

\NewEnviron{textBox}[1]
 {\begin{tikzpicture}
    \node [boxStyle]  (box)
     {\begin{minipage}{0.5\textwidth}\BODY\end{minipage}};
    \node[boxTitleStyle] at (box.north east) {#1};
  \end{tikzpicture}}

\begin{document}

\begin{textBox}{Hello}
This is some text that goes in the minipage
\end{textBox}

\end{document}

The environment's contents is absorbed and becomes the replacement text of \BODY.

enter image description here

| improve this answer | |
  • Thank you very much, before I register here I learn a lot from your great answers :) – S. Razi Dec 24 '12 at 18:53

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.