通常,在 TikZ 中绘制带有箭头的路径时,我们希望箭头精确地结束于路径的最后一点,而 TikZ 默认就是这么做的。但是,对于某些特定的箭头,可能希望更精确地控制箭头的位置:例如,您可能希望箭头的底部位于过去的最后一个点,而不是箭头尖端。
下面是在实线上绘制区间的一个例子:
\documentclass{article}
\usepackage{tikz,amssymb}
\usetikzlibrary{arrows}
\begin{document}
\begin{tikzpicture}
\draw (-3,0) -- (3,0) node[anchor=south] {$\mathbb{R}$};
\foreach \x in {-1,0,1} \draw (\x,2pt) -- (\x, -2pt) node[anchor=north] {$\x$};
\draw[thick,blue,[-[] (-1,0) -- (1,0);
\end{tikzpicture}
\end{document}
是否可以确保右方括号[
位于对应的刻度上x=1
?当然,可以手动调整,但这似乎不是一个令人满意的解决方案。
答案1
我会根据旧箭头声明一个新的箭头,但设置\pgfarrowsleftextend
为0pt
。
定义箭头尖的语法在 PGF 手册的第 74 节“箭头尖”中进行了解释。
\documentclass{article}
\usepackage{tikz,amssymb}
\usetikzlibrary{arrows}
\makeatletter
\pgfarrowsdeclare{[*}{]*}
{
\pgfarrowsleftextend{0pt}
\pgfarrowsrightextend{0pt}
}
{
\pgfutil@tempdima=2pt%
\advance\pgfutil@tempdima by1.5\pgflinewidth%
\pgfutil@tempdimb=\pgfutil@tempdima%
\advance\pgfutil@tempdimb by\pgflinewidth%
\pgfsetdash{}{+0pt}
\pgfsetmiterjoin
\pgfsetbuttcap
\pgfpathmoveto{\pgfqpoint{-.5\pgfutil@tempdimb}{-1\pgfutil@tempdima}}
\pgfpathlineto{\pgfqpoint{0pt}{-1\pgfutil@tempdima}}
\pgfpathlineto{\pgfqpoint{0pt}{\pgfutil@tempdima}}
\pgfpathlineto{\pgfqpoint{-.5\pgfutil@tempdimb}{\pgfutil@tempdima}}
\pgfusepathqstroke
}
\pgfarrowsdeclarereversed{]*}{[*}{[*}{]*}
\makeatother
\begin{document}
\begin{tikzpicture}
\draw (-3,0) -- (3,0) node[anchor=south] {$\mathbb{R}$};
\foreach \x in {-1,0,1} \draw (\x,2pt) -- (\x, -2pt) node[anchor=north] {$\x$};
\draw[thick,blue,[*-[*] (-1,0) -- (1,0);
\end{tikzpicture}
\end{document}
为了在绘图中使用(使用 PGFPlots 或命令\draw plot
),您可能需要绘图标记而不是箭头提示:
\documentclass{standalone}
\usepackage{pgfplots}
\makeatletter
\pgfdeclareplotmark{]}
{%
\pgfpathmoveto{\pgfqpoint{-0.65\pgfplotmarksize}{\pgfplotmarksize}}
\pgfpathlineto{\pgfqpoint{0pt}{\pgfplotmarksize}}
\pgfpathlineto{\pgfqpoint{0pt}{-\pgfplotmarksize}}
\pgfpathlineto{\pgfqpoint{-0.65\pgfplotmarksize}{-\pgfplotmarksize}}
\pgfusepathqstroke
}
\pgfdeclareplotmark{[}
{%
\pgfpathmoveto{\pgfqpoint{0.65\pgfplotmarksize}{\pgfplotmarksize}}
\pgfpathlineto{\pgfqpoint{0pt}{\pgfplotmarksize}}
\pgfpathlineto{\pgfqpoint{0pt}{-\pgfplotmarksize}}
\pgfpathlineto{\pgfqpoint{0.65\pgfplotmarksize}{-\pgfplotmarksize}}
\pgfusepathqstroke
}
\makeatother
\begin{document}
\begin{tikzpicture}
\begin{axis}[
yshift=4cm,
y = 1cm,
xmin = -2, xmax = 2,
ymin = -1, ymax = 1,
hide y axis,
axis x line* = middle,
disabledatascaling
]
\addplot [thick, blue, mark={[}, mark size=1mm] coordinates {(-1,0) (1,0)};
\end{axis}
\end{tikzpicture}
\end{document}
答案2
您可以使用shorten
。:
\documentclass{article}
\usepackage{tikz,amssymb}
\usetikzlibrary{arrows}
\begin{document}
\begin{tikzpicture}
\draw (-3,0) -- (3,0) node[anchor=south] {$\mathbb{R}$};
\foreach \x in {-1,0,1} \draw (\x,2pt) -- (\x, -2pt) node[anchor=north] {$\x$};
\draw[thick,blue,[-[,shorten >=-2pt] (-1,0) -- (1,0);
\end{tikzpicture}
\end{document}