形成 xticklabel 时出现 `\pgfmathresult pt = 1 pt` 问题(非常大的值)

形成 xticklabel 时出现 `\pgfmathresult pt = 1 pt` 问题(非常大的值)

我在比较时遇到了困难\pgfmathresult pt = 1 pt。一个完整的示例,其中一种情况有效,第二种情况无效(均在 x 轴上):

\documentclass{article}

\usepackage{tikz}
\usepackage{pgfplots}
\usetikzlibrary{pgfplots.groupplots}
\pgfplotsset{compat=1.14}

\begin{filecontents}{clock1.dat}
time signal
0 0
100 0
100 1
200 1
200 0
249 0
\end{filecontents}

\begin{filecontents}{clock2.dat}
time signal
99751 0
99800 0
99800 1
99900 1
99900 0
100000 0
\end{filecontents}

\begin{document}

  \pgfplotsset{ every non boxed x axis/.style={} }
  \begin{tikzpicture}
    \begin{groupplot}[group style={group name=my fancy plots,
                                   group size=2 by 1,
                                   yticklabels at=edge left,
                                   horizontal sep=10pt
                                  },
                      xticklabel style = {font = \scriptsize},
                      width = \textwidth,
                      height = 0.5\textwidth,
                      ymin = -0.25, ymax = 1.25,
                      restrict x to domain = 0:1e5,
                      ytick = {-1, -2, ..., -5},
                     ]
    \nextgroupplot[xmin = 0, xmax = 250,
                   restrict x to domain = 0:250,
                   xtick = {0, 100, 200},
                   xticklabel={\pgfmathparse{round(\tick/100)}{\ifdim\pgfmathresult pt=1 pt $\tau$%
                                                               \else \ifdim\pgfmathresult pt=0 pt $0$%
                                                               \else $\pgfmathprintnumber[int detect]{\pgfmathresult}\tau$%
                                                               \fi\fi%
                                                              }%
                              },
                   axis y line* = left,
                   scale only axis = true,
                   axis x discontinuity = none,
                   width = 0.5\textwidth,
                   extra y tick style = {blue, tick align = outside, tick label style = {rotate = 90, anchor = south}, every tick/.append style={white}, },
                   extra y ticks = {0.5, 2.0},
                   extra y tick labels = {clock, another},
                  ]
      \addplot [blue, very thin, domain = 0:250] table [x = time, y = signal] {clock1.dat};
    \nextgroupplot[xmin = 0.9975e5, xmax = 1e5,
                   restrict x to domain = 0.9975e5:1e5,
                   xtick={.998e5, .999e5, 1e5},
                   scale only axis = true,
                   scaled x ticks = manual:{}{\pgfmathmultiply{#1}{1e-2}},
                   xticklabel={\pgfmathparse{int(round(\tick-1e3))}
                                {\ifdim\pgfmathresult pt = 1 pt $\Delta-\tau$%
                                 \else \ifdim\pgfmathresult pt = 0 pt $\Delta$%
                                 \else $\Delta\pgfmathprintnumber[int detect]{\pgfmathresult}\tau$%
                                 \fi\fi%
                                }%
                              },
                   axis y line* = right,
                   ytick = \empty,
                   width = 0.5\textwidth
                  ]
      \addplot [blue, very thin, domain = 0.9975e5:1e5] table [x = time, y = signal] {clock2.dat};
    \end{groupplot}
  \end{tikzpicture}
\end{document}

这里(pdfTeX,版本 3.14159265-2.6-1.40.18(TeX Live 2017)(预加载格式=pdflatex))结果如下所示: 在此处输入图片描述

在右侧,我更希望看到 x-ticks $\Delta-2\tau$、$\Delta-\tau$ 和 $\Delta$。也就是说,我的目的是去掉“1”。在左侧,这个技巧效果很好。如果我处理较小的数据(在 [-1600,1600] 范围内,不需要缩放),它也有效。但我需要处理巨大的数字,所以我以这种方式准备了示例。

您能提供建议吗?提前谢谢您!问候。

答案1

我认为,如果您考虑到第二幅图中的数字为负数,您的比较将能够很好地进行。尽管如此,由于您正在检查整数,我想说,\ifnum在这里,稍微不那么脆弱的比较是有利的。

\documentclass{article}

\usepackage{tikz}
\usepackage{pgfplots}
\usetikzlibrary{pgfplots.groupplots}
\pgfplotsset{compat=1.14}

\begin{filecontents}{clock1.dat}
time signal
0 0
100 0
100 1
200 1
200 0
249 0
\end{filecontents}

\begin{filecontents}{clock2.dat}
time signal
99751 0
99800 0
99800 1
99900 1
99900 0
100000 0
\end{filecontents}

\begin{document}

  \pgfplotsset{ every non boxed x axis/.style={} }
  \begin{tikzpicture}
    \begin{groupplot}[group style={group name=my fancy plots,
                                   group size=2 by 1,
                                   yticklabels at=edge left,
                                   horizontal sep=10pt
                                  },
                      xticklabel style = {font = \scriptsize},
                      width = \textwidth,
                      height = 0.5\textwidth,
                      ymin = -0.25, ymax = 1.25,
                      restrict x to domain = 0:1e5,
                      ytick = {-1, -2, ..., -5},
                     ]
    \nextgroupplot[xmin = 0, xmax = 250,
                   restrict x to domain = 0:250,
                   xtick = {0, 100, 200},                  
xticklabel={\pgfmathparse{int(round(\tick/100))}{%
\ifnum\pgfmathresult=1 
$\tau$%
\else                                                             
  \ifnum\pgfmathresult=0 
    $0$%
  \else 
    $\pgfmathprintnumber[int detect]{\pgfmathresult}\tau$%
  \fi
\fi%
}%
},
                   axis y line* = left,
                   scale only axis = true,
                   axis x discontinuity = none,
                   width = 0.5\textwidth,
                   extra y tick style = {blue, tick align = outside, tick label style = {rotate = 90, anchor = south}, every tick/.append style={white}, },
                   extra y ticks = {0.5, 2.0},
                   extra y tick labels = {clock, another},
                  ]
      \addplot [blue, very thin, domain = 0:250] table [x = time, y = signal] {clock1.dat};
    \nextgroupplot[xmin = 0.9975e5, xmax = 1e5,
                   restrict x to domain = 0.9975e5:1e5,
                   xtick={.998e5, .999e5, 1e5},
                   scale only axis = true,
                   scaled x ticks = manual:{}{\pgfmathmultiply{#1}{1e-2}},
                   xticklabel={%
\pgfmathparse{int(round(\tick-1e3))}
\ifnum\pgfmathresult=-1
  $\Delta-\tau$%
\else 
  \ifnum\pgfmathresult=0 
    $\Delta$%
  \else 
    $\Delta\pgfmathprintnumber[int detect]{\pgfmathresult}\tau$%
  \fi
\fi%
}, axis y line* = right,
                   ytick = \empty,
                   width = 0.5\textwidth
                  ]
      \addplot [blue, very thin, domain = 0.9975e5:1e5] table [x = time, y = signal] {clock2.dat};
    \end{groupplot}
  \end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容