迭代 pgfkey 列表 - 无法访问宏

迭代 pgfkey 列表 - 无法访问宏

我必须绘制一些带注释的数字线,例如:

带箭头的注释数轴

为此,我pgfplots在界面上添加了几个额外的键。

\begin{tikzpicture}
\begin{axis}[
    number line,
    xmin=-2,xmax=4,
    xlabel={$f'(x)$\\$f(x)$},
    xtick={-1,2},
    interval labels={-1.5/$+$/$\nearrow$,0.5/$-$/$\searrow$,3/$+$/$\nearrow$},
]
\end{axis}
\end{tikzpicture}

干净整洁。问题是,有时我想要这样的数字线:

带四分之一圆注释的数字线

四分之一圆来自manfnt包。但是现在我将键设置为等于列表并迭代列表的实现无法编译。

以下是完整的 MWE:

\documentclass[png]{standalone}
\usepackage{tikz}
\colorlet{primary}{blue!40!black}       % HTML{000066}
\tikzstyle{number line value}=[primary,above]
\tikzstyle{number line description}=[primary,below]
\tikzstyle{number line point description}=[number line description,yshift=-2.5em,text width=2cm,text centered,anchor=south]
\usepackage{pgfplots}
\pgfplotsset{compat=1.8}
\pgfplotsset{every linear axis/.append style={axis x line=middle, axis y line=middle}}
\pgfplotsset{
    number line/.style={%
     axis x line=bottom,
        ymin=0,ymax=0.1,
        axis equal image,
        hide y axis,
        every axis x label/.append style={%
            anchor=west,
            align=left,
        },
    },
    interval labels/.style={
        after end axis/.prefix code={
            \edef\mylist{#1}
            \foreach \x/\val/\desc in \mylist {
                \ifx\val\desc
                    \edef\temp{\noexpand\draw ({axis cs:\x,0}|-{axis description cs:1,0}) node[number line value] {\val};}
                \else
                    \edef\temp{\noexpand\draw ({axis cs:\x,0}|-{axis description cs:1,0}) node[number line value] {\val} node[number line description] {\desc};}
                \fi
                \temp
            }
        }
    }
}
\usepackage{manfnt}
\newcommand{\nequartercircle}{\manquartercircle}
\newcommand{\nwquartercircle}{\manrotatedquartercircle}
\newcommand{\sequartercircle}{\rotatebox[origin=c]{180}{\nwquartercircle}}
\newcommand{\swquartercircle}{\rotatebox[origin=c]{180}{\nequartercircle}}

\begin{document}
\begin{tikzpicture}
    \begin{axis}[number line,
        xmin=-2,xmax=4,
        xlabel={$f(x)$\\shape},
        xtick={-1,0.5,2},
        xticklabels={$-1$,$\frac12$,$2$},
        % interval labels={-1.50//NW,-0.25//NE,1.25//SW,3.00//SE} % works, but not the symbols I want
        % interval labels={-1.50//\nwquartercircle,-0.25//\nequartercircle,1.25//\swquartercircle,3.00//\sequartercircle} % doesn't work
        after end axis/.append code={
            \coordinate (BR) at (axis description cs:1,0) {};
            \draw ({axis cs:-1.50,0}|-BR) node [number line description] {\nwquartercircle};
            \draw ({axis cs:-0.25,0}|-BR) node [number line description] {\nequartercircle};
            \draw ({axis cs: 1.25,0}|-BR) node [number line description] {\swquartercircle};
            \draw ({axis cs: 3.00,0}|-BR) node [number line description] {\sequartercircle};    
        } % works, but not the desired interface
    ]
    \end{axis}
\end{tikzpicture}
\end{document}

我猜测这与列表中的宏的扩展方式有关(\nearrow不是宏,而是用定义的字符控制序列\mathchardef),但除此之外我无法判断。

答案1

您正在尝试过度扩展:问题在于\rotatebox,它很脆弱。

移除\edef\mylist{#1}并更改\foreach \x/\val/\desc in \mylist

\foreach \x/\val/\desc in {#1}

然后使用

\unexpanded\expandafter{\val}

\unexpanded\expandafter{\desc}

而不是简单的\val\desc当你这样做\edef\temp{...}

完整代码:

\documentclass[png]{standalone}
\usepackage{tikz}
\colorlet{primary}{blue!40!black}       % HTML{000066}
\tikzstyle{number line value}=[primary,above]
\tikzstyle{number line description}=[primary,below]
\tikzstyle{number line point description}=[number line description,yshift=-2.5em,text width=2cm,text centered,anchor=south]
\usepackage{pgfplots}
\pgfplotsset{compat=1.8}
\pgfplotsset{every linear axis/.append style={axis x line=middle, axis y line=middle}}
\pgfplotsset{
  number line/.style={%
    axis x line=bottom,
    ymin=0,ymax=0.1,
    axis equal image,
    hide y axis,
    every axis x label/.append style={%
      anchor=west,
      align=left,
    },
  },
  interval labels/.style={
    after end axis/.prefix code={
      \foreach \x/\val/\desc in {#1} {
        \ifx\val\desc
          \edef\temp{%
            \noexpand\draw ({axis cs:\x,0}|-{axis description cs:1,0}) 
            node[number line value] {\unexpanded\expandafter{\val}};%
          }
        \else
          \edef\temp{%
            \noexpand\draw ({axis cs:\x,0}|-{axis description cs:1,0}) 
            node[number line value] {\val} node[number line description] 
            {\unexpanded\expandafter{\desc}};%
          }
        \fi
        \temp
      }
    }
  }
}
\usepackage{manfnt}
\newcommand{\nequartercircle}{\manquartercircle}
\newcommand{\nwquartercircle}{\manrotatedquartercircle}
\newcommand{\sequartercircle}{\rotatebox[origin=c]{180}{\nwquartercircle}}
\newcommand{\swquartercircle}{\rotatebox[origin=c]{180}{\nequartercircle}}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
  number line,
  xmin=-2,xmax=4,
  xlabel={$f(x)$\\shape},
  xtick={-1,0.5,2},
  xticklabels={$-1$,$\frac12$,$2$},
  interval labels={
    -1.50//\nwquartercircle,
    -0.25//\nequartercircle,
    1.25//\swquartercircle,
    3.00//\sequartercircle
  }
]
\end{axis}
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容