如何在 LaTeX 中使用 Tikz 绘制平行距离尺寸线

如何在 LaTeX 中使用 Tikz 绘制平行距离尺寸线

现在的情况

我正在尝试绘制一条与 SN 平行的线(N 是 SM 和 AB 的交点)。同时还要在其中心添加“y”符号(就像 SB 和 BN 附近的线一样)。

更新:我想出了以下解决方案。不过,仅通过选择坐标来创建这样的线条绝对是疯狂的。更新

更新 2:感谢大家的帮助!我终于制作出了我想要的图片,代码如下!特别感谢 MS-SPO =)

在此处输入图片描述

\documentclass[leqno,10pt]{extarticle}
\usepackage{tikz}
\usetikzlibrary{angles}
\usetikzlibrary{intersections}

\begin{document}

\begin{tikzpicture}
    \draw[-] (-2, 0) -- (9.3, 0); \draw (-2, 3) -- (9.3, 3);
  
    \coordinate[label=above:\small $S$] (S) at (-1.7, 3); 
    \coordinate[label=below:\small $M$] (M) at (   5, 0); 
    \coordinate[label=above:\small $B$] (B) at (   2, 3);
    \coordinate[label=below:\small $A$] (A) at (   2, 0); 

    \coordinate[label=below:\small $Q$] (Q) at (   9.3, 0); 


    \draw[name path=ortho] (A) -- (B);
    \draw[name path=diag] (S) -- (M);
    \draw[dash pattern={on 1pt off 2pt on 1pt off 2pt}, name path=diagonal] (S) -- (Q);
    \fill [name intersections={of=ortho and diag}]
        (intersection-1) coordinate (N) circle (1pt)
        node[xshift=5pt,yshift=-10pt] {\small $N$};

    \fill [name intersections={of=ortho and diagonal}]
        (intersection-1) coordinate (L) circle (1pt)
        node[xshift=-5pt,yshift=-5pt] {\small $L$};
    
    \pic[draw, angle radius=2mm] {right angle=N--A--M};
    
    \fill (A) circle (1pt);
    \fill (B) circle (1pt);
    \fill (M) circle (1pt);
    \fill (S) circle (1pt);
    \fill (Q) circle (1pt);


    \draw[|-|] ([shift=(230:2.5mm)] S) -- 
        node[fill=white,inner sep=0.5mm,outer sep=0.5mm] {\small $y$} 
        ([shift=(230:2.5mm)] N);

    \draw[|-|] ([yshift=2.5mm, xshift=2.2mm)] S) --
        node[fill=white,inner sep=0.5mm,outer sep=0.5mm] {\small $c$}
        ([yshift=2.5mm, xshift=-2.2mm)] B);

    \draw[|-|] ([xshift=2.5mm, yshift=-1mm)] B) --
        node[fill=white,inner sep=0.5mm,outer sep=0.5mm] {\small $x$}
        ([xshift=2.5mm, yshift=1mm)] N);

    \draw[|-|] ([xshift=-2.5mm, yshift=-1mm)] B) --
        node[fill=white,inner sep=0.5mm,outer sep=0.5mm] {\small $\varepsilon$}
        ([xshift=-2.5mm, yshift=1.3mm)] L);
\end{tikzpicture}

答案1

平行线有很多含义,而且有多种创建方法。这里看起来你实际上想要一条“暗线”,这里有一张可以在两点(以 分隔--)之间实现此效果的图片,其中dim line distance是该平行线的偏移量。dim line shadow键用于将|箭头尖端的背景预涂成白色,以剪切出暗线的区域。

代码

\documentclass[leqno, 10pt]{extarticle}
\usepackage{tikz}
\colorlet{Green}{green!50!black}
\usetikzlibrary{arrows.meta, angles, calc, quotes}
\tikzset{
  math nodes/.style={execute at begin node=$, execute at end node=$},
  small math nodes/.style={node font=\small, math nodes},
  % dim line adapted from https://tex.stackexchange.com/a/688676
  Measure/.tip = {Bar[sep = +0pt +-.5]},
  dim line distance/.initial=.2cm,
  dim line flip/.style={
    dim line distance/.expanded={-(\pgfkeysvalueof{/tikz/dim line distance})}},
  dim line style/.style={very thin, -Measure},
  dim line text/.style={midway, auto=false, inner sep=+.15em},
  dim line shadow/.style={every edge/.append style={preaction={
    line width/.expanded=3*\the\pgflinewidth,color=white,tips,arrows={[round]}}}},
  pics/dim line/.style args={#1--#2}{code={
    \path[path only]($(#1)!\pgfkeysvalueof{/tikz/dim line distance}!90:(#2)$)coordinate(@1)
      --node[dim line text,style/.expand once=\tikzpictextoptions,alias=@]{\tikzpictext}
      ($(#2)!\pgfkeysvalueof{/tikz/dim line distance}!-90:(#1)$)coordinate(@2);
    \path[dim line style, line to, pic actions] (@) edge (@1) edge (@2);}}}
\begin{document}
\tikz[
  dashy/.style={dash pattern=on 1pt off 2pt},
  dot/.style={label={% #1 will be used to place a label/pin at a label
    [shape=circle, inner sep=+0pt, fill, minimum size=+2.5pt, #1]center:}},
  small math nodes, % small math nodes everywhere
  every pin/.append style={inner sep=+.15em},
  pin distance=2mm, angle radius=2mm]
\draw coordinate[dot="S"] (S) at (-1.7, 3)
      coordinate[dot="B"] (B) at ( 2,   3)
      [label position=below] % now the labels come below the dots
      coordinate[dot="A"] (A) at ( 2,   0)
      coordinate[dot="M"] (M) at ( 5,   0)
      coordinate[dot="Q"] (Q) at ( 9.3, 0)
      [quotes mean pin]      % now pins will be created instead of labels
      coordinate[dot="250:L" {pin distance=1mm}] % 250 is the direction of the pin
                              (L) at (intersection of S--Q and A--B)
      coordinate[dot="290:N"] (N) at (intersection of S--M and A--B)
  % draw some lines
  (-2, 0) -- +(right:11.5) (-2, 3) -- +(right:11.5) (A) -- (B)
  % the edges will be painted on top of the dots if we don't specify behind path
  { [behind path]
    (S) edge[      Green] (M)
        edge[dashy, blue] (Q) }
  % right angle pic
  pic[draw] {right angle=M--A--B}
  % and dim line pics
  pic["y"                           ] {dim line=N--S}
  pic["c",   dim line distance=1.5em] {dim line=S--B}
  % dim line shadow paints a white background behind the Bar tips
  pic["x",           Green, dim line shadow] {dim line=B--N}
  pic["\varepsilon", blue,  dim line shadow] {dim line=L--B}
;
\end{document}

输出

在此处输入图片描述

答案2

这是实现此目的的一种方法。

通过分析您的代码,我发现了一些不同之处。

calc这里不需要,而intersectionsangles

为了使用交叉点,我将这些路径命名为orthodiag。这个相当长的语句中的相关部分:

    \fill [name intersections={of=ortho and diag}]% which paths to intersect
        (intersection-1) coordinate (N) circle (1pt)% 1st as (N) etc.
        node[xshift=6pt,yshift=4pt] {N};% putting the label there
  • 哪些路径将会相交?[name intersections={of=ortho and diag}]
  • 他们的坐标是什么,即第一个?(intersection-1)
  • 很好:让我们存储它(intersection-1) coordinate (N)
  • 其余的都和你已经拥有的一样

从读者的角度来看,我认为您引入的那些额外线条不会增加视觉价值,也不会使事情更容易理解,尤其是当长度确实不同时。所以,让我们简单一点,只把变量名称放在那里。为此,我重新绘制了几条不可见的线条,只是为了将标签(节点)放在正确的位置:

    % ~~ SIMPLIFIED: kind of redrawing, just to put the labels ~~
    \draw[draw=none] (S) -- node[above] {c} (B);
    \draw[draw=none] (B) -- node[right] {x} (N);
    \draw[draw=none] (S) -- node[below] {y} (N);

让我们使用角度库来指示直角,因为它是为此而制作的:

    % ~~~ marking the right angle ~~~~~~~~~~
    \pic[draw, angle radius=2mm] {right angle=N--A--M};

最后,当先填充点时,彩色线条会切入点中。为了避免这种情况,一个简单的方法是将它们放在最后。

结果

\documentclass[10pt,border=3mm,tikz]{standalone}
\usetikzlibrary{intersections}%     <<< different !
\usetikzlibrary{angles}%            <<< new

\begin{document}
 \begin{tikzpicture}
    % ~~~ parallels ~~~~~~~~~~~~~~~~~~~~~~
    \draw[-] (-2, 0) -- (9.3, 0); \draw (-2, 3) -- (9.3, 3);
  
    % ~~~ most points ~~~~~~~~~~~~~~~~~~~
    \coordinate[label=above:$S$] (S) at (-1.7, 3); 
    \coordinate[label=below:$M$] (M) at (   3, 0); 
    \coordinate[label=above:$B$] (B) at (   2, 3); 
    \coordinate[label=below:$A$] (A) at (   2, 0); 

    % ~~~ orthogonal ~~~~~~~~~~~~~~~~
    \draw[red, name path=ortho] (A) -- (B);% <<<

    % ~~~ diagonal ~~~~~~~~~~~~~~~~
    \draw[blue, name path=diag] (S) -- (M);% <<<
    
    % ~~~ instersection ~~~~~~~~~~~~~~
    \fill [name intersections={of=ortho and diag}]% which paths to intersect
        (intersection-1) coordinate (N) circle (1pt)% 1st as (N) etc.
        node[xshift=6pt,yshift=4pt] {N};% putting the label there
    
    % ~~ SIMPLIFIED: kind of redrawing, just to put the labels ~~
    \draw[draw=none] (S) -- node[above] {c} (B);
    \draw[draw=none] (B) -- node[right] {x} (N);
    \draw[draw=none] (S) -- node[below] {y} (N);
    
    % ~~~ marking the right angle ~~~~~~~~~~
    \pic[draw, angle radius=2mm] {right angle=N--A--M};
    
    % ~~~ placing remaining circles/dots ~~~~~~~~
    \fill (A) circle (1pt);
    \fill (B) circle (1pt);
    \fill (M) circle (1pt);
    \fill (S) circle (1pt);
 \end{tikzpicture}
\end{document}

附言:如果你坚持要添加这些相似之处——并使阅读变得更加困难——这里有一种方法可以做到:

    % ~~~ one parallel ~~~~~~~~~~~
    \draw[|-|] ([shift=(230:5mm)] S) -- node[fill=white] {y} ([shift=(230:5mm)] N);
  • 使用SN
  • 将其坐标转换为极坐标
  • \draw[|-|] ...对于结束刻度

与平行

对于所有的人都这样做,例如:

全部

    
    % ~~~ all parallels ~~~~~~~~~~~
    \draw[|-|] ([shift=(230:5mm)] S) -- node[fill=white] {y} ([shift=(230:5mm)] N);
    \draw[|-|] ([yshift=7mm)] S)     -- node[fill=white] {c} ([yshift=7mm)] B);
    \draw[|-|] ([xshift=12mm)] B)    -- node[fill=white] {x} ([xshift=12mm)] N);

答案3

开始...

\documentclass[border=5mm]{standalone}
\usepackage{tkz-euclide}


\begin{document}

\begin{tikzpicture}
    \tkzDefPoints{2/0/A,2/3/B,3/0/M,-1.7/3/S}
    \tkzDrawPoints(A,B,M,S)
    \tkzDrawPoints(A,B,M,S)
    \tkzLabelPoints(A,M)
    \tkzLabelPoints[above](B,S)
    \tkzDrawSegments(A,B M,S)
    \tkzInterLL(A,B)(M,S)\tkzGetPoint{N}
    \tkzDrawPoint[red](N)
    \tkzDrawSegment[
        dim={$x$,5mm,right=2mm},
        dim style/.append style={blue}](B,N)
    \tkzDrawSegment[
        dim={$y$,5mm,left=2mm},
        dim style/.append style={blue}](N,S)
\end{tikzpicture}

\end{document}

在此处输入图片描述

相关内容