如何突出显示水平线上的区域?

如何突出显示水平线上的区域?

我想绘制如图所示的“虚线”高光:

在此处输入图片描述

到目前为止,我有以下代码:

\documentclass[10pt,a4paper]{article}
\usepackage{tikz}
\begin{document}
\begin{figure}[ht]
\centering
\begin{tikzpicture}
    \node[circle,draw, inner sep=0pt, minimum size=.1cm, thick, fill=black, label={A}] (A) at  (-2,0) {};
    \node[circle,draw, inner sep=0pt, minimum size=.1cm, thick, fill=black, label={B}] (B) at  (0,0) {};
    \draw [] (A)--(B);
\end{tikzpicture}
\caption{}
\end{figure}
\end{document}

答案1

您可以使用 tikzlibrarypattern来绘制线条并设置选项:在线上方绘制pattern=north east linesrectangle

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{patterns}
\usetikzlibrary{calc}

\begin{document}

\begin{tikzpicture}
    \node[circle,draw, inner sep=0pt, minimum size=.1cm, thick, fill=black, label={A}] (A) at  (-2,0) {};
    \node[circle,draw, inner sep=0pt, minimum size=.1cm, thick, fill=black, label={B}] (B) at  (0,0) {};
    \draw [] (A)--(B);
    \draw [pattern=north east lines, draw=none] ($(A)+(0,0.1)$) rectangle (B);
\end{tikzpicture}

\end{document}

在此处输入图片描述

编辑:由于此解决方案仅涵盖水平和垂直线,因此我想到了一个更好的更通用的解决方案。我在以下网址找到了此解决方案的基础示例

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{
    decorations.pathreplacing,
    decorations.pathmorphing
} 

\begin{document}

\begin{tikzpicture}[
    linedecoration/.style={
        postaction={draw,decorate,decoration={border,angle=45,
                    amplitude=2mm,segment length=1.5mm}}},
    ]
    \node[circle,draw, inner sep=0pt, minimum size=.1cm, thick, fill=black, label={A}] (A) at  (-2,0) {};
    \node[circle,draw, inner sep=0pt, minimum size=.1cm, thick, fill=black, label={B}] (B) at  (1,1) {};
    \draw [linedecoration] (A)--(B);
\end{tikzpicture}

\end{document}

在此处输入图片描述

相关内容