如何定位大括号标签?

如何定位大括号标签?

我想要这种布局,但我无法控制 s1、s2 和 s3 的位置。我该如何实现这一点。我的 MWE 如下。

在此处输入图片描述

\documentclass[11pt]{article}
\usepackage{tikz}
\usetikzlibrary{decorations.pathreplacing,angles,quotes}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\begin{document}
\begin{figure}[ht]
  \centering
  \begin{tikzpicture}
    \coordinate (Origin)   at (0,0);
    \coordinate (XAxisMin) at (-2.0,0);
    \coordinate (XAxisMax) at (2.0,0);
    \coordinate (YAxisMin) at (0,-2.2);
    \coordinate (YAxisMax) at (0,2.2);
    \draw  (XAxisMin) -- (XAxisMax);
    \draw  (YAxisMin) -- (YAxisMax);
    \coordinate (P1) at (-2,-2);
    \coordinate (P2) at (-1,-1.74);
    \coordinate (P3) at (0.82,0);
    \coordinate (P4) at (2,2);
    \draw [thick,green] (P1) -- (P2) -- (P3) -- (P4);
    \draw (P1) node[circle,fill,minimum size=1mm,inner sep=2pt,label=above:$p_1$](){};
    \draw (P2) node[circle,fill,minimum size=1mm,inner sep=2pt,label=above:$p_2$](){};
    \draw (P3) node[circle,fill,minimum size=1mm,inner sep=2pt,label=above left:$p_3$](){};
    \draw (P4) node[circle,fill,minimum size=1mm,inner sep=2pt,label=above left:$p_4$](){};
    \draw[red,decoration={brace,mirror,raise=10pt},decorate](P1) -- node[below=10pt] {$s_1$}(P2);
    \draw[red,decoration={brace,mirror,raise=10pt},decorate](P2) -- node[right=16pt] {$s_2$}(P3);
    \draw[red,decoration={brace,mirror,raise=10pt},decorate](P3) -- node[right=16pt] {$s_3$}(P4);
    \end{tikzpicture}
\end{figure}
\end{document}

答案1

如果你同意括号标签是以括号为导向的(有些人期望如此),那么解决方案就相对简单了。使用\draw[brace ...] (Pi) -- node[sloped,below=12pt] {Si} (Pj);

完整的代码(我尽量将其写得更短)如下:

\documentclass[11pt]{article}
\usepackage{tikz}
\usetikzlibrary{decorations.pathreplacing,angles,quotes}

\begin{document}
\begin{figure}[ht]
  \centering
  \begin{tikzpicture}[
dot/.style = {circle, fill=black, 
              minimum size=1mm, inner sep=2pt,
              node contents = {},
              label=120:#1}, %< --- changed
brc/.style = {red,
              decoration={brace,mirror,raise=10pt},
              decorate}
                    ]
    \coordinate (Origin)   at (0,0);
    \coordinate (XAxisMin) at (-2.0,0);
    \coordinate (XAxisMax) at (2.0,0);
    \coordinate (YAxisMin) at (0,-2.2);
    \coordinate (YAxisMax) at (0,2.2);
\draw  (XAxisMin) -- (XAxisMax);
\draw  (YAxisMin) -- (YAxisMax);
    \coordinate (P1) at (-2,-2);
    \coordinate (P2) at (-1,-1.74);
    \coordinate (P3) at (0.82,0);
    \coordinate (P4) at (2,2);
\draw   (P1) node[dot=$p_1$] --
        (P2) node[dot=$p_2$] --
        (P3) node[dot=$p_3$] --
        (P4) node[dot=$p_4$];
\draw[brc](P1) -- node[sloped,below=12pt] {$s_1$} (P2);
\draw[brc](P2) -- node[sloped,below=12pt] {$s_2$}(P3);
\draw[brc](P3) -- node[sloped,below=12pt] {$s_3$}(P4);
    \end{tikzpicture}
\end{figure}
\end{document}

上述代码生成以下图表:

在此处输入图片描述

编辑: 节点标签的位置仅略有改善......

答案2

有点乏味,但您可以修改pos和的距离。节点相对于点之间的路径定位,而不是沿着括号移动的路径定位。因此,当您将节点直接放置在P1和之间的路径中点下方时P2,由于线的角度,它最终会位于括号中间的左侧。

\documentclass[11pt]{article}
\usepackage{tikz}
\usetikzlibrary{decorations.pathreplacing,angles,quotes}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\begin{document}
\begin{figure}[ht]
  \centering
  \begin{tikzpicture}
    \coordinate (Origin)   at (0,0);
    \coordinate (XAxisMin) at (-2.0,0);
    \coordinate (XAxisMax) at (2.0,0);
    \coordinate (YAxisMin) at (0,-2.2);
    \coordinate (YAxisMax) at (0,2.2);
    \draw  (XAxisMin) -- (XAxisMax);
    \draw  (YAxisMin) -- (YAxisMax);
    \coordinate (P1) at (-2,-2);
    \coordinate (P2) at (-1,-1.74);
    \coordinate (P3) at (0.82,0);
    \coordinate (P4) at (2,2);
    \draw [thick,green] (P1) -- (P2) -- (P3) -- (P4);
    \draw (P1) node[circle,fill,minimum size=1mm,inner sep=2pt,label=above:$p_1$](){};
    \draw (P2) node[circle,fill,minimum size=1mm,inner sep=2pt,label=above:$p_2$](){};
    \draw (P3) node[circle,fill,minimum size=1mm,inner sep=2pt,label=above left:$p_3$](){};
    \draw (P4) node[circle,fill,minimum size=1mm,inner sep=2pt,label=above left:$p_4$](){};
    \draw[red,decoration={brace,mirror,raise=10pt},decorate](P1) -- node[below=12pt,pos=0.7] {$s_1$} (P2);
    \draw[red,decoration={brace,mirror,raise=10pt},decorate](P2) -- node[right=20pt,pos=0.25] {$s_2$} (P3);
    \draw[red,decoration={brace,mirror,raise=10pt},decorate](P3) -- node[right=15pt,pos=0.35] {$s_3$} (P4);
    \end{tikzpicture}
\end{figure}
\end{document}

在此处输入图片描述

答案3

您可以在和S1之间的括号中间设置一个节点。然后,使用库,找到点和之间的中点。最后,将括号标签放置在距离 到 路径一定距离的节点中。对其他括号重复此操作。P1P2calcM1P1P2M1S1

\documentclass[11pt]{article}
\usepackage{tikz}
\usetikzlibrary{decorations.pathreplacing,angles,quotes,calc}

\begin{document}
\begin{figure}[ht]
  \centering
  \begin{tikzpicture}[
dot/.style = {circle, fill=black, 
              minimum size=1mm, inner sep=2pt,
              node contents = {},
              label=above:#1},
brc/.style = {red,
              decoration={brace,mirror,raise=10pt},
              decorate}
                    ]
    \coordinate (Origin)   at (0,0);
    \coordinate (XAxisMin) at (-2.0,0);
    \coordinate (XAxisMax) at (2.0,0);
    \coordinate (YAxisMin) at (0,-2.2);
    \coordinate (YAxisMax) at (0,2.2);
\draw  (XAxisMin) -- (XAxisMax);
\draw  (YAxisMin) -- (YAxisMax);
    \coordinate (P1) at (-2,-2);
    \coordinate (P2) at (-1,-1.74);
    \coordinate (P3) at (0.82,0);
    \coordinate (P4) at (2,2);
    \coordinate (M1) at ($(P1)!0.5!(P2)$);
    \coordinate (M2) at ($(P2)!0.5!(P3)$);
    \coordinate (M3) at ($(P3)!0.5!(P4)$);
\draw [thick,green] (P1) -- (P2) -- (P3) -- (P4);
    \draw (P1) node[circle,fill,minimum size=1mm,inner sep=2pt,label=above:$p_1$](){};
    \draw (P2) node[circle,fill,minimum size=1mm,inner sep=2pt,label=above:$p_2$](){};
    \draw (P3) node[circle,fill,minimum size=1mm,inner sep=2pt,label=above left:$p_3$](){};
    \draw (P4) node[circle,fill,minimum size=1mm,inner sep=2pt,label=above left:$p_4$](){};\draw[brc](P1) -- node(S1)[sloped,below=12pt] {} (P2);
\draw[brc](P2) -- node(S2)[sloped,below=12pt] {}(P3);
\draw[brc](P3) -- node(S3)[sloped,below=12pt] {}(P4);
\path[red] (M1) to ($(M1)!1.2!(S1)$) node{$s_1$};
\path[red] (M2) to ($(M2)!1.2!(S2)$) node{$s_2$};
\path[red] (M3) to ($(M3)!1.2!(S3)$) node{$s_3$};
    \end{tikzpicture}
\end{figure}
\end{document}

在此处输入图片描述

相关内容