嵌套环境耗尽了我的 tex 堆栈

嵌套环境耗尽了我的 tex 堆栈

我想创建一个环境,用于在我的文档中绘制相同样式的派生树。有时我希望将此环境放置在某个浮点中,但有时我想要一个不同的环境(例如使用横向旋转的页面或其他),因此我将我最喜欢的浮点拆分为外部环境,将我的内部浮点拆分为 tikz 树。不幸的是,这在 tex 中创建了一个递归,导致我的堆栈爆炸。这是一个代码示例:

\documentclass[12pt]{article}    
\usepackage{environ}
\usepackage{tikz}

\NewEnviron{AST}{
\begin{tikzpicture}
        [
                sibling distance = 6em,
                level distance = 6em,
                align = center,
                grow' = right,
                every node/.style = {shape=rectangle, rounded corners,
                        draw, align=center,
                        top color=white, bottom color=blue!20},
                leaf/.style = {font=\ttfamily}
        ]
        \BODY\
\end{tikzpicture}
        }

% #1 is short name for figure
% #2 is long name for figure
\NewEnviron{ASTfig}[2]{
\begin{figure}[hbp]
  \begin{center}
    \begin{AST}
        \BODY\
    \end{AST}
  \end{center}
\caption[AST - #1]{Abstract Syntax Tree - #2}
\end{figure}
}

\begin{document}
\begin{ASTfig}{shambalulu}{bambashushu}
\node {root}
        child { node {node}
                child { node [leaf] {leafy} }
                child { node [leaf] {mcleaf} }
                }
        };
\end{ASTfig}
\end{document}

我猜是因为 tex 试图无限地用 \BODY\ 替换 \BODY\。我翻阅了文档并尝试使用 \environbodyname 命令,但它在两种情况下都更改了 \BODY,并没有解决问题。

我错过了什么?

答案1

内部\BODY需要\begin{AST}\BODY\end{ast}重命名环境AST被称为,例如:

\documentclass[12pt]{article}
\usepackage{environ}
\usepackage{tikz}

\NewEnviron{AST}{%
  \begin{tikzpicture}
    [
      sibling distance = 6em,
      level distance = 6em,
      align = center,
      grow' = right,
      every node/.style = {
        shape=rectangle, rounded corners,
        draw, align=center,
        top color=white, bottom color=blue!20},
      leaf/.style = {font=\ttfamily}
    ]
    \BODY
  \end{tikzpicture}%
}

% #1 is short name for figure
% #2 is long name for figure
\NewEnviron{ASTfig}[2]{%
  \begin{figure}[hbp]
    \centering
    \let\INNERBODY\BODY
    \begin{AST}
      \INNERBODY
    \end{AST}
    \caption[AST - #1]{Abstract Syntax Tree - #2}
  \end{figure}%
}

\begin{document}
\begin{ASTfig}{shambalulu}{bambashushu}
\node {root}
  child {
    node {node}
    child { node [leaf] {leafy} }
    child { node [leaf] {mcleaf} }
  };
\end{ASTfig}
\end{document}

结果

补充说明:

  • \centering最好使用环境center,因为环境center会增加额外的垂直间距。但figure已经增加了垂直空间。

  • 在 命令后使用反斜杠是没有意义的\BODY\。环境无论如何都会结束,在这里强制使用空格是没有意义的。

  • 行尾通常会添加一个空格。示例在定义中注释了一些行尾,以防止泄漏空格。

答案2

不完全确定您要做什么,但似乎\AST应该是一个宏,而不是一个环境:

在此处输入图片描述

参考:

代码:

\documentclass[12pt]{article}    
\usepackage{environ}
\usepackage{tikz}

\newcommand{\AST}[1]{%
\begin{tikzpicture}
        [
                sibling distance = 6em,
                level distance = 6em,
                align = center,
                grow' = right,
                every node/.style = {shape=rectangle, rounded corners,
                        draw, align=center,
                        top color=white, bottom color=blue!20},
                leaf/.style = {font=\ttfamily}
        ]
        #1
\end{tikzpicture}%
}

% #1 is short name for figure
% #2 is long name for figure
\NewEnviron{ASTfig}[2]{%
\begin{figure}[hbp]
  \centering
  \AST{\BODY}
\caption[AST - #1]{Abstract Syntax Tree - #2}
\end{figure}%
}

\begin{document}
\begin{ASTfig}{shambalulu}{bambashushu}
\node {root}
        child { node {node}
                child { node [leaf] {leafy} }
                child { node [leaf] {mcleaf} }
                }
        ;
\end{ASTfig}
\end{document}

答案3

您不需要\NewEnviron内部环境。

我建议使用xparse,这样您也可以避免\NewEnviron使用外部的。

\documentclass[12pt]{article}    
\usepackage{xparse}
\usepackage{tikz}

\NewDocumentEnvironment{AST}{}
 {%
  \begin{tikzpicture}[
    sibling distance = 6em,
    level distance = 6em,
    align = center,
    grow' = right,
    every node/.style = {
      shape=rectangle,
      rounded corners,
      draw,
      align=center,
      top color=white,
      bottom color=blue!20
    },
      leaf/.style = {font=\ttfamily}
  ]
 }
 {\end{tikzpicture}}

% #1 is short name for figure
% #2 is long name for figure
\NewDocumentEnvironment{ASTfig}{O{#2}m}
 {%
  \begin{figure}[hbp]
  \centering
  \begin{AST}
 }
 {%
  \end{AST}
  \caption[AST - #1]{Abstract Syntax Tree - #2}
  \end{figure}
 }

\begin{document}
\begin{ASTfig}[shambalulu]{bambashushu}
  \node {root}
    child {
      node {node}
      child { node [leaf] {leafy} }
      child { node [leaf] {mcleaf} }
    };
\end{ASTfig}
\end{document}

语法有点不同,但我认为更方便。你可以调用

\begin{ASTfig}{xyz}

如果短标题等于长标题;你调用

\begin{ASTfig}[short]{long}

如果它们不同。

在此处输入图片描述

相关内容