Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question
4  
You might skip directly to mdframed package to have similar constructs. –  percusse Dec 25 '12 at 15:37
add comment

2 Answers 2

up vote 9 down vote accepted

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}
share|improve this answer
add comment

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

share|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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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