调整节点标签位置

调整节点标签位置

在下面的 MWE 中,我如何调整节点标签的位置?请注意,节点由

dot/.style={circle,inner sep=1pt,fill,label={#1},name=#1}

但我不知道如何保持相同的设置才能调整标签的位置。例如,类似dot=M,leftdot=M,right。代码的原始来源来自 https://tex.stackexchange.com/a/21759/10898

在此处输入图片描述

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}

\tikzset{
    right angle quadrant/.code={
        \pgfmathsetmacro\quadranta{{1,1,-1,-1}[#1-1]}     % Arrays for selecting quadrant
        \pgfmathsetmacro\quadrantb{{1,-1,-1,1}[#1-1]}},
    right angle quadrant=1, % Make sure it is set, even if not called explicitly
    right angle length/.code={\def\rightanglelength{#1}},   % Length of symbol
    right angle length=2ex, % Make sure it is set...
    right angle symbol/.style n args={3}{
        insert path={
            let \p0 = ($(#1)!(#3)!(#2)$) in     % Intersection
                let \p1 = ($(\p0)!\quadranta*\rightanglelength!(#3)$), % Point on base line
                \p2 = ($(\p0)!\quadrantb*\rightanglelength!(#2)$) in % Point on perpendicular line
                let \p3 = ($(\p1)+(\p2)-(\p0)$) in  % Corner point of symbol
            (\p1) -- (\p3) -- (\p2)
        }
    }
}

\begin{document}
\begin{tikzpicture}[dot/.style={circle,inner sep=1pt,fill,label={#1},name=#1},
  extended line/.style={shorten >=-#1,shorten <=-#1},
  extended line/.default=1cm]

\node [dot=L] at (1,0) {};
\node [dot=M] at (4,0) {};
\node [dot=N] at (4,5) {};

\draw [extended line=0.5cm] (L) -- (M) node [below,midway] {3};
\draw [extended line=0.5cm] (L) -- (N) node [above left,midway] {6};
\draw [extended line=0.5cm] ($(L)!(N)!(M)$) -- (N) node [right,midway] {$x$};
\draw [red,right angle symbol={M}{N}{L}];

\end{tikzpicture}
\end{document}

注意,我要找的是将其N向右移动、L向左下方移动和M向右下方移动。

答案1

标签的语法是

/tikz/label=[<options>]<angle>:<text>

where<angle>可帮助您在其位置周围放置标签。对于您来说,您只需要在定义中包含第二个参数即可dot/.style。使用类似

dot/.style 2 args={circle,inner sep=1pt,fill,label={#2:#1},name=#1},

您可以使用\node [dot={L}{below}] at (1,0) {};。与往常一样,TiKZ您可以使用诸如、、、... 之类的表达式belowabove代替below left数值。

在 中,绘制顺序很重要TiKZ。在您的代码中,dots绘制在 之前extended lines,因此出现在线条后面。您可以在库帮助dots下将线条放在点后面。它声明一个,放置在其上的所有对象都将出现在默认主层上绘制的对象后面。backgroundsbackground layer

经过这些修改后,你的代码可能会

\PassOptionsToPackage{dvipsnames}{xcolor}
\documentclass[tikz,border=5mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc,backgrounds}

\tikzset{
    right angle quadrant/.code={
        \pgfmathsetmacro\quadranta{{1,1,-1,-1}[#1-1]}     % Arrays for selecting quadrant
        \pgfmathsetmacro\quadrantb{{1,-1,-1,1}[#1-1]}},
    right angle quadrant=1, % Make sure it is set, even if not called explicitly
    right angle length/.code={\def\rightanglelength{#1}},   % Length of symbol
    right angle length=2ex, % Make sure it is set...
    right angle symbol/.style n args={3}{
        insert path={
            let \p0 = ($(#1)!(#3)!(#2)$) in     % Intersection
                let \p1 = ($(\p0)!\quadranta*\rightanglelength!(#3)$), % Point on base line
                \p2 = ($(\p0)!\quadrantb*\rightanglelength!(#2)$) in % Point on perpendicular line
                let \p3 = ($(\p1)+(\p2)-(\p0)$) in  % Corner point of symbol
            (\p1) -- (\p3) -- (\p2)
        }
    }
}

\begin{document}
\begin{tikzpicture}[dot/.style 2 args={circle,inner sep=1pt,fill,label={#2:#1},name=#1},
  extended line/.style={shorten >=-#1,shorten <=-#1,draw=Cerulean},
  extended line/.default=1cm]

\node [dot={L}{below right}] at (1,0) {};
\node [dot={M}{below left}] at (4,0) {};
\node [dot={N}{right}] at (4,5) {};

\begin{scope}[on background layer]
\draw [extended line=0.5cm] (L) -- (M) node [below,midway] {3};
\draw [extended line=0.5cm] (L) -- (N) node [above left,midway] {6};
\draw [extended line=0.5cm] ($(L)!(N)!(M)$) -- (N) node [right,midway] {$x$};
\end{scope}

\draw [red,right angle symbol={M}{N}{L}];

\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容