让绘图忽略轮廓而不绘制标签

让绘图忽略轮廓而不绘制标签

我使用轮廓包来标记一条边。我希望其他边不会被标签周围的轮廓打断。这可以通过在标记的边之后绘制这些边来实现。但这会覆盖标签,如果我使用不同的颜色,这会使其更难阅读。以下示例给了我这个

在此处输入图片描述

我确实希望黑线被中断,灰线连续。但是,灰线画在 x 上,这是我不希望的。我怎样才能在灰线上画 x?

\documentclass{article}
\usepackage{tikz}
\usepackage[outline]{contour}
\contourlength{1.8pt}

\begin{document}
\begin{tikzpicture}
\draw[-] (0,0) -- (2,0) node[pos=0.50] {\contour{white}{$x$}};
\draw[-, black!30] (1,1) -- (1,-1);
\end{tikzpicture}
\end{document}

答案1

代码(手册)

\documentclass[tikz]{standalone}
\begin{document}
\tikz
  \path % place the node first
    (0,0) -- (2,0) node[pos=.5, inner sep=+.15em] (x) {$x$}
    [behind path]
    % then draw the lines
    % • the black line use the node as a coordinate
    %   so that it actually stops at its border
    %   (could also be a separate path, doesn't need to be an edge)
    (0,0) edge[to path=--(x)--(\tikztotarget)] (2,0)
    % • the gray line doesn't use the node name
    %   but since “behind path” is used
    %   it will be behind the node
    %   (which is by default “in front of path”)
    (1,1) edge[black!30] (1,-1);
\end{document}

代码(使用tikz-extext.nodesnode on line

\documentclass[tikz]{standalone}
\usetikzlibrary{ext.nodes}
\begin{document}
\tikz
  \draw (0,0) to [node on line] node[pos=.5, inner sep=+.15em] (x) {$x$} (2,0)
    [behind path] (1,1) edge[black!30] (1, -1);
\end{document}

输出

在此处输入图片描述

答案2

您可以将节点留空并稍后添加内容:

\documentclass{article}
\usepackage{tikz}
\usepackage[outline]{contour}
\contourlength{1.8pt}

\begin{document}
\begin{tikzpicture}
\draw[-] (0,0) -- (2,0) node[pos=0.50] (x){};
\draw[-, black!30] (1,1) -- (1,-1);
\node at (x){\contour{white}{$x$}};
\end{tikzpicture}
\end{document}

在此处输入图片描述

如果你真的想让灰线穿过 x(在我看来这看起来不太好):

\documentclass{article}
\usepackage{tikz}
\usepackage[outline]{contour}
\contourlength{1.8pt}

\begin{document}
\begin{tikzpicture}
\draw[-] (0,0) -- (2,0) node[fill=white,inner sep=0pt,pos=0.50] (x){\phantom{$x$}};
\draw[-, black!30] (1,1) -- (1,-1);
\node at (x){$x$};
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案3

我找到了一个解决方案,它更像是一种变通方法。我可以先画一个白色标签,然后再在该标签上画图。

在此处输入图片描述

\documentclass{article}
\usepackage{tikz}
\usepackage[outline]{contour}
\contourlength{1.8pt}

\begin{document}
\begin{tikzpicture}
\draw[-] (0,0) -- (2,0) node[pos=0.50] {\contour{white}{\textcolor{white}{$x$}}};
\draw[-, black!30] (1,1) -- (1,-1);
\path (0,0) -- (2,0) node[pos=0.50] {$x$};
\end{tikzpicture}
\end{document}

如果您有想法,请随意发布更优雅的方式。

相关内容