TikZ 范围和宏中的 xshift 问题

TikZ 范围和宏中的 xshift 问题

如果作用域嵌入在宏中,则使用作用域时会遇到问题。有一个答案,但依赖于本地边界框:相对于另一个 tikz 范围定位 tikz 范围

如果范围包含在宏中,则不起作用——这是宏内分组的问题,我无法解决。我肯定我遗漏了一些令人烦恼的基本知识。

\documentclass{article}

\usepackage{graphicx}
\usepackage{xcolor}
\usepackage{tikz}
\usepackage[papersize={5.5in,8.5in},margin=0.6in,bottom=0.7in]{geometry}
\usepackage{fontspec}
%% \usepackage{array}
%% \usepackage{multicol}
\usepackage{xparse}

\newlength{\xmove}

\usetikzlibrary{positioning, calc}

\NewDocumentCommand{\makespine}{O{}mm}{%% With scope built in
    \foreach \ingtmp [count=\ingnumtmp from 1] in {#2}{\xdef\maxitems{\ingnumtmp}}
    \begin{scope}[xshift=\xmove]%
        \node at (0,0) {};
        \foreach \N [count=\M from 1] in {#2}
            \node[anchor=west,inner xsep=0pt] at (\M*.125,\M*.5) {$\leftarrow$\N};
        \draw[<-] (0,0) -- (\maxitems*.125,\maxitems*.5)coordinate(head);
        \node[anchor=south,above =2pt of head]{#3};
    \end{scope}%
}

\NewDocumentCommand{\nsmakespine}{O{}mm}{%% Uses external scope 
    \foreach \ingtmp [count=\ingnumtmp from 1] in {#2}{\xdef\maxitems{\ingnumtmp}}
        \node at (0,0) {};
        \foreach \N [count=\M from 1] in {#2}
            \node[anchor=west,inner xsep=0pt] at (\M*.125,\M*.5) {$\leftarrow$\N};
        \draw[<-] (0,0) -- (\maxitems*.125,\maxitems*.5)coordinate(head);
        \node[anchor=south,above =2pt of head]{#3};
}

%% \Setmainfont{}
%% \setsansfont{}
%% \newfontfamily{}{}
%% \newfontinstance{}{}

\begin{document}

This works:

\begin{tikzpicture}[]
    \draw[->] (0,0) -- (2in,0);
    \nsmakespine{1,2,3,4,5,6}{Spine 1}
    \begin{scope}[xshift=1in]
    \nsmakespine{A,B,C,D,E,F, G,H,I}{Spine 2}
    \end{scope}
    \begin{scope}[xshift=2in]
    \nsmakespine{A,B,C,D,E,F, G,H,I,J}{Spine 3}
    \end{scope}
\end{tikzpicture}

This does not:

\begin{tikzpicture}[]
    \draw[->] (0,0) -- (2in,0);
    \makespine[xmove=0in]{1,2,3,4,5,6}{Spine 1}
    \makespine[xmove=1in]{A,B,C,D,E,F, G,H,I}{Spine 2}
    \makespine[xmove=2in]{A,B,C,D,E,F, G,H,I,J}{Spine 3}
\end{tikzpicture}

\end{document}

在此处输入图片描述

答案1

我不知道到底哪里出了问题,但我认为,如果您正在加载 pgf,则可以使用它的密钥,据我所知,这些密钥比其他密钥管理系统更强大。此外,您可能不会引入全局宏来计算项目数量。

\documentclass{article}

\usepackage{tikz}% tikz loads graphicx and xcolor
\usepackage[papersize={5.5in,8.5in},margin=0.6in,bottom=0.7in]{geometry}
\usepackage{fontspec}
%% \usepackage{array}
%% \usepackage{multicol}
\usepackage{xparse}

\makeatletter
\pgfmathdeclarefunction{Dim}{1}{%
\begingroup%
\pgfutil@tempcnta0%
\@for\pgfutil@tempa:=#1\do{\advance\pgfutil@tempcnta1}%
\edef\pgfmathresult{\the\pgfutil@tempcnta}%
\pgfmathsmuggle\pgfmathresult\endgroup%
}
\makeatother

\tikzset{spine/.cd,xmove/.initial=0pt}

\usetikzlibrary{positioning, calc}

\NewDocumentCommand{\makespine}{O{}mm}{%% With scope built in
    \pgfmathsetmacro{\maxitems}{Dim("{#2}")}
    \tikzset{spine/.cd,#1}
    \begin{scope}[xshift=\pgfkeysvalueof{/tikz/spine/xmove}]%
        \node at (0,0) {};
        \foreach \N [count=\M from 1] in {#2}
            \node[anchor=west,inner xsep=0pt] at (\M*.125,\M*.5) {$\leftarrow$\N};
        \draw[<-] (0,0) -- (\maxitems*.125,\maxitems*.5)coordinate(head);
        \node[anchor=south,above =2pt of head]{#3};
    \end{scope}%
}

\begin{document}

This works:

\begin{tikzpicture}[]
    \draw[->] (0,0) -- (2in,0);
    \makespine[xmove=0in]{1,2,3,4,5,6}{Spine 1}
    \makespine[xmove=1in]{A,B,C,D,E,F, G,H,I}{Spine 2}
    \makespine[xmove=2in]{A,B,C,D,E,F, G,H,I,J}{Spine 3}
\end{tikzpicture}

\end{document}

在此处输入图片描述

答案2

您的可选参数应该是\xmove=1in(而不是xmove=1in),因为\xmove是长度(而不是键)。此外,您必须在\makespine宏中评估此可选参数。

这是一个可以运行的版本:

\documentclass{article}

\usepackage{graphicx}
\usepackage{xcolor}
\usepackage{tikz}
\usepackage[papersize={5.5in,8.5in},margin=0.6in,bottom=0.7in]{geometry}
% \usepackage{fontspec}
%% \usepackage{array}
%% \usepackage{multicol}
\usepackage{xparse}

\newlength{\xmove}

\usetikzlibrary{positioning, calc}

\NewDocumentCommand{\makespine}{O{}mm}{%% With scope built in
  #1%
  \foreach \ingtmp [count=\ingnumtmp from 1] in {#2}{\xdef\maxitems{\ingnumtmp}}
  \begin{scope}[xshift=\xmove]%
    \node at (0,0) {};
    \foreach \N [count=\M from 1] in {#2}
    \node[anchor=west,inner xsep=0pt] at (\M*.125,\M*.5) {$\leftarrow$\N};
    \draw[<-] (0,0) -- (\maxitems*.125,\maxitems*.5)coordinate(head);
    \node[anchor=south,above =2pt of head]{#3};
  \end{scope}%
}

\NewDocumentCommand{\nsmakespine}{O{}mm}{%% Uses external scope 
  \foreach \ingtmp [count=\ingnumtmp from 1] in {#2}{\xdef\maxitems{\ingnumtmp}}
  \node at (0,0) {};
  \foreach \N [count=\M from 1] in {#2}
  \node[anchor=west,inner xsep=0pt] at (\M*.125,\M*.5) {$\leftarrow$\N};
  \draw[<-] (0,0) -- (\maxitems*.125,\maxitems*.5)coordinate(head);
  \node[anchor=south,above =2pt of head]{#3};
}

%% \Setmainfont{}
%% \setsansfont{}
%% \newfontfamily{}{}
%% \newfontinstance{}{}

\begin{document}

This works:

\begin{tikzpicture}[]
  \draw[->] (0,0) -- (2in,0);
  \nsmakespine{1,2,3,4,5,6}{Spine 1}
  \begin{scope}[xshift=1in]
    \nsmakespine{A,B,C,D,E,F, G,H,I}{Spine 2}
  \end{scope}
  \begin{scope}[xshift=2in]
    \nsmakespine{A,B,C,D,E,F, G,H,I,J}{Spine 3}
  \end{scope}
\end{tikzpicture}

This does not:

\begin{tikzpicture}[]
  \draw[->] (0,0) -- (2in,0);
  \makespine[\xmove=0in]{1,2,3,4,5,6}{Spine 1}
  \makespine[\xmove=1in]{A,B,C,D,E,F, G,H,I}{Spine 2}
  \makespine[\xmove=2in]{A,B,C,D,E,F, G,H,I,J}{Spine 3}
\end{tikzpicture}

\end{document}

相关内容