pgfkey 和宏参数以不同的方式通过相同的测试

pgfkey 和宏参数以不同的方式通过相同的测试

在下一个 MWE 中,对宏参数提供的测试给出了预期的答案(它会查找参数是否以 开头(),但相同的测试失败或忽略了它必须测试的 pgfkey。

这是一个最小的例子,它是为了完成我对这个问题的回答:如何定义 tikz 样式选项以在特定点之间绘制尺寸线

在此处输入图片描述

    \documentclass{article}
\usepackage{xparse,tikz}
\usetikzlibrary{calc}
\usepackage{ifluatex}

\makeatletter

\ifluatex
\RequirePackage{pdftexcmds}
\let\pdfstrcmp\pdf@strcmp
\let\pdffilemoddate\pdf@filemoddate
\fi

\tikzset{%
    Cote/.style={to path={\pgfextra{
        \pgfinterruptpath
               \draw[>=latex,|<->|] let
    \p1=($(\tikztostart)!2mm!90:(\tikztotarget)$),
    \p2=($(\tikztotarget)!2mm!-90:(\tikztostart)$)
    in(\p1) -- (\p2) node[pos=.5,sloped,above]{%
    \expandafter\Cote\expandafter{\@aspect} -- 
    \expandafter\CoteBis{$\pi$}\expandafter<\@aspect>};
        \endpgfinterruptpath
        }(\tikztostart) -- (\tikztotarget) \tikztonodes}}
}

\pgfkeys{tikz/Cote/.cd,
    aspect/.store in=\@aspect,
    aspect=o,
} 
\makeatletter

\NewDocumentCommand{\Cote}{m}{%
    \ifnum\pdfstrcmp{\unexpanded\expandafter{\@car#1\@nil}}{(}=\z@
        true
    \else
        false
    \fi
    }

\NewDocumentCommand{\CoteBis}{md<>
    }{%
    #1
    \ifnum\pdfstrcmp{\unexpanded\expandafter{\@car#2\@nil}}{(}=\z@
        true
    \else
        false
    \fi
    }

\makeatother

\begin{document}

\verb+\Cote+ : \Cote{(F)} -- \Cote{F} : The test gives the expected answers

\verb+\Codetis+ : \CoteBis{$\pi$}<(F)> -- \CoteBis{$\pi$}<F> : The test gives the expected answers

\bigskip

In the \verb+\Cote+ macro, the test gives the expected answers

In the \verb+\CoteBis+ macro, the test gives unexpected answers

\bigskip

\begin{tikzpicture}
\path[Cote/aspect=F] (0,0) to[Cote] (5,0) ;
\path[Cote/aspect=(F)] (0,1) to[Cote] (5,1) ;
\end{tikzpicture}
\end{document}

答案1

两个问题:

  • \CoteBis永远不会以 TikZ 风格调用Cote。我认为第二个\Cote应该是\CoteBis

  • \expandafter只能跳过标记,而不是一组标记。因此,

    \expandafter\Cote{$\pi$}\expandafter<\@aspect>
    

    需要更多次调用\expandafter(可能\CoteBis代替\Cote):

    \expandafter\CoteBis\expandafter{%
      \expandafter$\expandafter\pi\expandafter$\expandafter}%
      \expandafter<\@aspect>
    

结果:

固定结果

相关内容