pgfkeys 问题

pgfkeys 问题

也许是一个愚蠢的错误,但结果却不同,为什么?我希望节点在一条水平线上。如果我评论,\pgfqkeys{/LX}{#1}那么结果就很好(节点在水平线上)

\documentclass[11pt]{scrartcl}
\usepackage{tikz}
\pgfkeys{/LX/.cd,
        frac/.store in              = \Xfrac, %not used
        frac                        = 0,
        /LX/.search also            = {/tikz}, 
}  

\def\labelX[#1](#2,#3){%
\pgfqkeys{/LX}{#1}% ok if I comment this line
\foreach \p in {#2,...,#3}{%
    \path[shift = {(\p,0)}] (0pt,5 pt)--(0pt,-5 pt) 
                            node[/LX/.cd,#1]{\p};}
    \ifnum\Xfrac=1 \node at (0,1){Test frac =1};
    \else \node at (0,1){Test frac =0};
    \fi
}

\def\labelXb[#1](#2,#3){%
\pgfqkeys{/LX}{#1}
\foreach \p in {#2,...,#3}{%
    \path[shift={(\p,0)}] (0pt,5 pt)--(0pt,-5 pt)
                          node[/LX/.cd,#1,rotate=-30]{\p};}
    \ifnum\Xfrac=1 \node at (0,1){Test frac =1};
    \else \node at (0,1){Test frac =0};
    \fi
}

\begin{document}
\begin{tikzpicture}
    \labelX[frac=1,red,rotate=-30](0,5)
\end{tikzpicture}

\begin{tikzpicture}
    \labelXb[frac=0,blue,draw](0,5)
\end{tikzpicture}

\end{document}

在此处输入图片描述

答案1

在第一种情况下,让我们跟踪转换:假设该论点#1仅仅rotate=-30为了简单起见成立。

\def\labelX[#1](#2,#3){%
\pgfqkeys{/LX}{#1}% Here it is equivalent to \tikzset{rotate=-30}
\foreach \p in {#2,...,#3}{%
     %
     % Here still rotate=-30 is enforced hence the path below obeys the rotation.
     %
     \path[shift = {(\p,0)}] (0pt,5 pt)--(0pt,-5 pt) 
                            node[/LX/.cd,#1]{\p};}
     %
     %We can also see the rotation from something at the origin that won't rotate
     %
     %
     \node[circle,fill,inner sep=2pt] {}
     %
     \ifnum\Xfrac=1 \node[transform shape,draw] at (0,1){Test frac =1};
     \else \node at (0,1){Test frac =0};
     \fi
}

在此处输入图片描述

最简单的方法是重置转换(或至少仅重置非平移转换)

\documentclass[tikz]{standalone}
\pgfkeys{/LX/.cd,
        frac/.store in              = \Xfrac, %not used
        frac                        = 0,
        /LX/.search also            = {/tikz}, 
}  

\def\labelX[#1](#2,#3){%
\pgfqkeys{/LX}{#1}
\foreach \p in {#2,...,#3}{%
\pgftransformresetnontranslations%                <------------
    \path[shift = {(\p,0)}] (0pt,5 pt)--(0pt,-5 pt) 
                            node[/LX/.cd,#1]{\p};}
    \ifnum\Xfrac=1 \node at (0,1){Test frac =1};
    \else \node at (0,1){Test frac =0};
    \fi
}

\begin{document}
\begin{tikzpicture}
    \labelX[frac=1,red,rotate=-30](0,5)
\end{tikzpicture}
\end{document}

相关内容