答案1
以下是解决此问题的几种方法。问题是edge
创建了一条独立路径。正如最初指出的那样保罗·加博利,可以使用键tip=proper
或tips=on proper draw
,它们在 pgfmanual v3.1.4b 第 188 页中有描述。在我看来,更干净的解决方案是将箭头传递给edge
。可以说最干净的方法是用 替换edge
,to
这样可以避免绘制两条单独的路径。还有其他选项,例如使用\path
而不是绘制,但在我看来,这违背了目的,因为路径仍然存在,并且可能(至少在原则上)改变边界框,比如说。
\documentclass[border=5pt]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[auto,font=\sffamily]
\begin{scope}[local bounding box=original]
\draw[<->] (0,0) edge node {$a$} (1,0);
\end{scope}
\node[above] at (original.north) {original situation};
%
\begin{scope}[xshift=4cm,local bounding box=proper,tips=proper]
\draw[<->] (0,0) edge node {$a$} (1,0);
\end{scope}
\node[above] at (proper.north) {\texttt{tips=proper}};
%
\begin{scope}[yshift=-2cm,local bounding box=on proper draw,tips=on proper draw]
\draw[<->] (0,0) edge node {$a$} (1,0);
\end{scope}
\node[above] at (on proper draw.north) {\texttt{tips=on proper draw}};
%
\begin{scope}[yshift=-2cm,xshift=4cm,local bounding box=clean]
\draw (0,0) edge[<->] node {$a$} (1,0);
\end{scope}
\node[above] at (clean.north) {cleaner(?) solution};
%
\begin{scope}[yshift=-4cm,local bounding box=to]
\draw[<->] (0,0) to node {$a$} (1,0);
\end{scope}
\node[above] at (to.north) {\texttt{to} instead of \texttt{edge}};
%
\begin{scope}[yshift=-4cm,xshift=4cm,local bounding box=path]
\path[<->] (0,0) edge node {$a$} (1,0);
\end{scope}
\node[above] at (path.north) {\texttt{\textbackslash path} instead of
\texttt{\textbackslash draw}};
\end{tikzpicture}
\end{document}
答案2
一种方法是定义边:
\documentclass[border=5pt]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[auto,
every edge/.style={draw, <->}
]
\draw (0,0) edge node {$a$} (1,0);
\end{tikzpicture}
\end{document}
这使:
或者使用quotes
库:
\documentclass[border=5pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{quotes}
\begin{document}
\begin{tikzpicture}[auto,
every edge/.style={draw, <->}
]
\draw (0,0) edge[<->, "$a$"] (1,0);
\end{tikzpicture}
\end{document}
或两种建议的结合。