如何定义以节点开始的环境?

如何定义以节点开始的环境?

请参阅以下定义:

     \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}
     }

现在我想将其转换为环境:

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

或者,

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

不正确,有什么建议吗?

答案1

您可以开始节点在零件中并使用和\begin在零件中完成它:\end\bgroup\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}

答案2

您无法在零件中启动节点\begin并在零件中完成它\end。但是该environ软件包可以解决这个问题:

\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}

环境的内容被吸收并成为的替代文本\BODY

在此处输入图片描述

相关内容