TikZ定位库错误

TikZ定位库错误

我正在尝试做一些我认为简单的事情——采用逗号分隔的列表并将列表中的每个元素放在它们自己的节点中\foreach

\documentclass{article}

\usepackage{tikz}
\usepackage{xparse}

\usetikzlibrary{positioning,calc}

\NewDocumentEnvironment{footest}{m}{%
\begin{tikzpicture}
    \node[inner sep=0pt] (A0) at (0,0) {};
    \foreach \n [count=\ingnum from 1] in {#1}
        {%
        \pgfmathsetmacro{\ntmp}{\ingnum-1}
        %% This at least does not give an error, but is not correct:
        % \node[below=10pt of A\ntmp,draw,anchor=north east] (A\ingnum)
        \node[below=10pt of A\ntmp.south west,draw,anchor=north east] (A\ingnum)
            {\parbox{1in}{\strut\n\strut}};
        }%
}{\end{tikzpicture}}

\begin{document}

\begin{footest}{1,2,3,4 bla bla bla bla bla blba,5}\end{footest}

\end{document}

错误是Package PGF Math Error: Unknown operators' 或so' (in '0.south west').我尝试过用A\ntmp.south west各种括号和大括号组合括起来,但都无济于事。如果我删除,.south west那么代码排版但垂直大框(4在示例中)将覆盖下面的下一个节点。TikZ 文档中的示例(例如版本 3.1.5b,第 241 页)似乎表明这应该是可能的。可能忽略了一些基本的东西……

答案1

如果你取消注释节点行并添加,\typeout{\ntmp}则日志列表

0.0
1.0
2.0
3.0
4.0

尝试\pgfmathsetmacro{\ntmp}{int(\ingnum-1)}或者使用

\pgfmathtruncatemacro{\ntmp}{\ingnum-1}

答案2

@daleif解释问题很好。下面是编写修复代码的另一种方法,使用构造\foreach ... [..., evaluate=... as ... using ...] in {...}/tikz/text width节点选项而不是\parbox{1in}(据我所知,/tikz/text width即使在使用非 LaTeX 格式的文档中,例如纯 TeX 或 ConTeXt,也应该可以工作):

\documentclass{article}
\usepackage{xparse}
\usepackage{tikz}
\usetikzlibrary{positioning}

\NewDocumentEnvironment{footest}{m}
  {%
    \begin{tikzpicture}
      \node[inner sep=0pt] (A0) at (0,0) {};
      \foreach \n [count=\ingnum from 1,
                   evaluate=\ingnum as \ntmp using int(\ingnum-1)] in {#1}
        {
          \node[below=10pt of A\ntmp.south west, draw, anchor=north east,
                text width=1in] (A\ingnum) {\strut\n\strut};
        }
  }
  {\end{tikzpicture}}

\begin{document}

\begin{footest}{1, 2, 3, 4 bla bla bla bla bla bla, 5}
\end{footest}

\end{document}

截屏

相关内容