我有两个刻度标记的标签。其中一个标签,,-8
将被空白包围,以便它看起来像是在图表上排版的。我使用以下命令执行此操作。
extra x ticks={-8},
extra x tick style={ticklabel style={font=\scriptsize, inner xsep=10pt, fill=white}},
extra x tick labels={\makebox[0pt][r]{$-$}8},
但是空白太多了。节点中心和边缘之间的默认距离是多少?我希望是这个距离的三分之二或一半。如何减小包含-8
空白的框的尺寸,同时保持标签和其各自刻度线之间的距离-8
一致8
?
\documentclass{amsart}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{tikz}
\usetikzlibrary{calc,intersections}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11}
\begin{document}
\begin{tikzpicture}
\begin{axis}[width=3.5in, height=3.5in, axis lines=middle, axis on top, clip=false,
axis lines=middle,
xmin=-10,xmax=10,
ymin=-2.5,ymax=10,
restrict y to domain=-2.5:10,
xtick={8}, ytick={8},
ticklabel style={font=\scriptsize},
xticklabels={8},
extra x ticks={-8},
extra x tick style={ticklabel style={font=\scriptsize, inner xsep=10pt, fill=white}},
extra x tick labels={\makebox[0pt][r]{$-$}8},
axis line style={latex-latex},
xlabel=$x$,ylabel=$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[samples=2, blue, domain=-10:0] {x + 8} node [anchor=south east, pos=0.35, font=\footnotesize] {$y= \vert x \vert + y = 8$};
\addplot[samples=2, blue, domain=0:10] {-x + 8};
\addplot[dashed, latex-latex, domain=-2.5:10] (6,x) node [pos=0.9, anchor=north, font=\footnotesize, sloped] {$x=6$};
\coordinate (A) at (6,0);
\coordinate (B) at (6,2);
\coordinate (C) at (0,8);
\coordinate (D) at (-8,0);
\end{axis}
\fill[gray!50, fill opacity=0.25] (A) -- (B) -- (C) -- (D) -- cycle;
\end{tikzpicture}
\end{document}
答案1
假设您在这里指的是垂直距离,那么的默认值inner sep
,即从节点内容到节点边框的距离,在和0.333em
两个方向上。x
y
使用 设置垂直填充可能并不奇怪inner ysep
,与inner xsep
设置水平填充类似。
如果您减少该值,节点标签将向上移动一点,但您可以将其向下移动以考虑到这一点yshift
。例如,添加inner ysep=0.15em,yshift=-0.183em
到您的“ticklabel 样式”。0.333-0.15=0.183,因此值为。
答案2
另一种可能性是使用\colorbox
额外的刻度标签:
\documentclass{amsart}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11}
\usetikzlibrary{calc,intersections}
\newcommand\whitebg[2][2pt]{{%
\setlength\fboxsep{#1}%
\raisebox{0pt}[\dimexpr\height-#1\relax][\dimexpr\depth-#1\relax]{\colorbox{white}{#2}}%
}}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
width=3.5in, height=3.5in,
axis on top,
clip=false,
axis lines=middle,
xmin=-10,xmax=10,
ymin=-2.5,ymax=10,
restrict y to domain=-2.5:10,
xtick={8}, ytick={8},
ticklabel style={font=\scriptsize},
xticklabels={8},
extra x ticks={-8},
extra x tick style={ticklabel style={font=\scriptsize}},
extra x tick labels={\whitebg{\makebox[2em]{\makebox[0pt][r]{$-$}8}}},
axis line style={latex-latex},
xlabel=$x$,ylabel=$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[samples=2, blue, domain=-10:0] {x + 8}
node [anchor=south east, pos=0.35, font=\footnotesize] {$y= \vert x \vert + y = 8$};
\addplot[samples=2, blue, domain=0:10] {-x + 8};
\addplot[dashed, latex-latex, domain=-2.5:10] (6,x)
node [pos=0.9, anchor=north, font=\footnotesize, sloped] {$x=6$};
\coordinate (A) at (6,0);
\coordinate (B) at (6,2);
\coordinate (C) at (0,8);
\coordinate (D) at (-8,0);
\end{axis}
\fill[gray!50, fill opacity=0.25] (A) -- (B) -- (C) -- (D) -- cycle;
\end{tikzpicture}
\end{document}