TikZ:带有边缘节点的箭头不正确

TikZ:带有边缘节点的箭头不正确

为什么左箭头是错误的,我该如何修复它?

\documentclass[border=5pt]{standalone}

\usepackage{tikz}

\begin{document}
    \begin{tikzpicture}[auto]
    \draw[<->] (0,0) edge node {$a$} (1,0);
    \end{tikzpicture}
\end{document}  

在此处输入图片描述

如果没有边缘节点,它可以正常工作:

...
\draw[<->] (0,0) -- (1,0);
...

在此处输入图片描述

答案1

以下是解决此问题的几种方法。问题是edge创建了一条独立路径。正如最初指出的那样保罗·加博利,可以使用键tip=propertips=on proper draw,它们在 pgfmanual v3.1.4b 第 188 页中有描述。在我看来,更干净的解决方案是将箭头传递给edge。可以说最干净的方法是用 替换edgeto这样可以避免绘制两条单独的路径。还有其他选项,例如使用\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}  

或两种建议的结合。

相关内容