覆盖样式

覆盖样式

我有两条线的图,它们是跨 - 轴的相互反射x。y 轴截距为(0,-2)(0,2)。我使用ticklabel style来排版2extra y tick style来排版-2y- 轴上。为什么它们与 - 轴的距离不等y

\documentclass{amsart}
\usepackage{mathtools,array}

\usepackage{tikz}
\usetikzlibrary{calc,intersections}

\usepackage{pgfplots}
\pgfplotsset{compat=1.11}


\begin{document}

\begin{tikzpicture}
\begin{axis}[width=3.5in, axis equal image, clip=false,
    axis lines=middle,
    xmin=-14,xmax=6,
    ymin=-5,ymax=5,
    ticklabel style={anchor=south east, xshift=2pt, yshift=-2pt, font=\tiny},
    xtick={\empty},ytick={2},
    extra y ticks={-2},
    extra y tick style={tick label style={anchor=north east, xshift=2pt, yshift=2pt, font=\tiny}},
    extra y tick labels={\makebox[0pt][r]{$-$}2},
    axis line style={latex-latex},
    xlabel=\textit{x}, ylabel=\textit{y},
    axis line style={shorten >=-12.5pt, shorten <=-12.5pt},
    xlabel style={at={(ticklabel* cs:1)}, xshift=12.5pt, anchor=north west},
    ylabel style={at={(ticklabel* cs:1)}, yshift=12.5pt, anchor=south west}
]

\addplot[latex-latex, domain=-14:6] {(1/2) * x + 2};
\addplot[latex-latex, dashed, domain=-14:6] {-(1/2) * x - 2};

\coordinate (A) at (-12,-4);
\coordinate (B) at (0,2);
\coordinate (C) at (-12,4);
\coordinate (D) at (0,-2);

\end{axis}

%A "pin" is drawn between the label for the line with slope 1/2 and the the line.
\draw[draw=gray, shorten <=1mm, shorten >=1mm] (A) -- ($(A) +({atan(-2)}:0.5)$);
\draw node[anchor=north west, inner sep=0, font=\scriptsize] at ($(A) +({atan(-2)}:0.5)$){$-2x + y = 2$};


%A "pin" is drawn between the label for the line with slope -1/2 and the the line.
\draw[draw=gray, shorten <=1mm, shorten >=1mm] (C) -- ($(C) +({atan(2)}:0.5)$);
\draw node[anchor=south west, inner sep=0, font=\scriptsize] at ($(C) +({atan(2)}:0.5)$){$2x + y = -2$};


\end{tikzpicture}

\end{document}

答案1

我刚刚意识到,xshifts 是累积的。如果向同一个节点添加多个xshift,则总移位不是最后一个移位的值,而是两个移位的总和。举个例子来说明这一点:

\documentclass[border=5mm]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\draw [help lines] (0,-1) grid (2,1);

% no xshift
\node [yshift=1cm,draw] {1};
% one xshift
\node [yshift=0cm,xshift=1cm,draw] {2};
% two xshift
\node [yshift=-1cm,xshift=1cm,xshift=1cm,draw] {3};
\end{tikzpicture}
\end{document}

在此处输入图片描述

注意3节点移动了 2cm,因为它有xshift=1cm两次。

对于您的 ,也会发生同样的事情extra tick,因为ticklabel style是 的缩写every tick label/.append style。键是append,这意味着 的参数ticklabel style添加到样式的末尾every tick label,而不会更改样式的现有部分。

因此,额外刻度的标签会得到xshift=2pt两次。一次来自ticklabel style,另一次来自。因此,如果您从 中extra y tick style={ticklabel style={..}}删除,位置将保持不变。完整示例,删除了大部分不必要的部分,并使用垂直红线突出显示刻度标签的对齐方式。我还添加了,以便更容易看到刻度标签的位置。xshiftextra y tick styledraw

在此处输入图片描述

\documentclass[border=5mm]{standalone}    
\usepackage{pgfplots}
\pgfplotsset{compat=1.11}    
\begin{document}

\begin{tikzpicture}
\begin{axis}[height=5cm,name=a,
    axis lines=middle,
    xmin=-1,xmax=1,ymin=-3,ymax=3,
    ticklabel style={
      draw, % added just for example
      anchor=south east,
      xshift=2pt,
      yshift=-2pt, % you have -2pt here
      font=\tiny
    },
    xtick={\empty},ytick={2},
    extra y ticks={-2},
    extra y tick style={
         tick label style={
             anchor=north east,
             xshift=0pt, % zero xshift here
             yshift=4pt, % 4pt here, 2pt to counter the -2pt from above, another 2pt to get the same shift
          }
     },
    extra y tick labels={\makebox[0pt][r]{$-$}2},
]
\end{axis}

\draw [very thin,red] ([xshift=-0.4em]a.north) -- ++(0,-4cm); 
\end{tikzpicture}

\end{document}

覆盖样式

如上所述,ticklabel style附加到现有样式。如果您不ticklabel style={使用every tick label/.style={,则every tick label style将被完全重新定义,因此您不必考虑累积变化,但您必须重复,例如font=\tiny。这是一个例子,输出与上面相同。

\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11}
\begin{document}

\begin{tikzpicture}
\begin{axis}[height=5cm,name=a,
    axis lines=middle,
    xmin=-1,xmax=1,ymin=-3,ymax=3,
    ticklabel style={
      draw, % just for example
      anchor=south east,
      xshift=2pt,
      yshift=-2pt, 
      font=\tiny
    },
    xtick={\empty},ytick={2},
    extra y ticks={-2},
    extra y tick style={
         every tick label/.style={
             draw, % just for example
             anchor=north east,
             xshift=2pt, % 2pt here as well
             yshift=2pt, 
             font=\tiny
          }
     },
    extra y tick labels={\makebox[0pt][r]{$-$}2},
]
\end{axis}

\draw [very thin,red] ([xshift=-0.4em]a.north) -- ++(0,-4cm); 
\end{tikzpicture}

\end{document}

相关内容