화살표의 노드가 제대로 정렬되지 않았습니다.

화살표의 노드가 제대로 정렬되지 않았습니다.

나는 다음과 같은 자동 장치용 LaTeX를 작성했습니다.

여기에 이미지 설명을 입력하세요

보시다시피, q1에서 q3까지의 화살표에 있는 'b'는 q3에서 q2까지의 화살표에 있는 'a, b, c'보다 아래쪽에 위치합니다.

또한, q2에서 q3까지의 가장자리에 있는 'b'는 q3에서 q1까지의 화살표에 있는 'a, c'보다 낮게 위치합니다.

두 노드 듀오를 모두 같은 기준선에 잘 정렬하고 싶습니다. 그러나 4개가 모두 같은 라인에 있을 필요는 없습니다.

내 코드가 이 작업을 자체적으로 수행해야 하는 것 같지만 왠지 작동하지 않습니다. 왜?

여기 내 코드가 있습니다. 두 개의 노드 듀오를 주석으로 표시했습니다.

\documentclass[border=5mm]{standalone}

\usepackage{tikz}
\usetikzlibrary{automata,arrows,calc,positioning}

\begin{document}
\begin{tikzpicture}[->,node distance=25mm]
\node[state,initial,accepting] (q0) {$q_0$};
\node[state,right=of q0] (q1) {$q_1$};
\node[state,right=of q1] (q2) {$q_2$};
\node[state,below=of $(q1)!0.5!(q2)$] (q3) {$q_3$};
\node[state,accepting,right=of q2] (q4) {$q_4$};

\draw (q0) edge[loop above] node[above] {a, b} (q0);
\draw (q0) -- node[above] {b, c} ++ (q1);
\draw (q1) edge[loop above] node[above] {a, c} (q1);
\draw (q1) edge[bend left] node[above] {a, b, c} (q2);
\draw (q2) -- node[below] {a, c} ++ (q1);
\draw (q2) edge[bend left] node[right] {b} (q3);             % ! duo B
\draw (q3) -- node[above left] {a, b, c} ++ (q2);            % ! duo A
\draw (q3) edge[bend left] node[left] {a, c} (q1);           % ! duo B
\draw (q1) -- node[above right] {b} ++ (q3);                 % ! duo A
\draw (q2) edge[loop above] node[above] {a, b, c} (q2);
\draw (q2) -- node[above] {c} ++ (q4);
\draw (q1) edge[out=60,in=120] node[above] {c} (q4);
\end{tikzpicture}
\end{document}

답변1

해당 노드에 대해 추가하거나 \strut정의합니다 . 나는 또한 적절 하게 및 로 text depth변경했습니다 .above leftleftabove rightright

\documentclass[border=5mm]{standalone}

\usepackage{tikz}
\usetikzlibrary{automata,arrows,calc,positioning}

\begin{document}
\begin{tikzpicture}[->,node distance=25mm]
\node[state,initial,accepting] (q0) {$q_0$};
\node[state,right=of q0] (q1) {$q_1$};
\node[state,right=of q1] (q2) {$q_2$};
\node[state,below=of $(q1)!0.5!(q2)$] (q3) {$q_3$};
\node[state,accepting,right=of q2] (q4) {$q_4$};

\draw (q0) edge[loop above] node[above] {a, b} (q0);
\draw (q0) -- node[above] {b, c} ++ (q1);
\draw (q1) edge[loop above] node[above] {a, c} (q1);
\draw (q1) edge[bend left] node[above] {a, b, c} (q2);
\draw (q2) -- node[below] {a, c} ++ (q1);
\draw (q2) edge[bend left] node[right] {\strut b} (q3);             % ! duo B
\draw (q3) -- node[left] {\strut a, b, c} ++ (q2);            % ! duo A
\draw (q3) edge[bend left] node[left] {\strut a, c} (q1);           % ! duo B
\draw (q1) -- node[right] {\strut b} ++ (q3);                 % ! duo A
\draw (q2) edge[loop above] node[above] {a, b, c} (q2);
\draw (q2) -- node[above] {c} ++ (q4);
\draw (q1) edge[out=60,in=120] node[above] {c} (q4);
\end{tikzpicture}
\end{document}

여기에 이미지 설명을 입력하세요

답변2

먼저 s 옵션 text height=1ex, text depth=0pt에 옵션을 추가하여 tikzpicture모든 레이블이 동일한(세로) 크기를 갖도록 합니다.

이렇게 하면 문제가

보시다시피, q1에서 q3까지의 화살표에 있는 'b'는 q3에서 q2까지의 화살표에 있는 'a, b, c'보다 아래쪽에 위치합니다.

또한, q2에서 q3까지의 가장자리에 있는 'b'는 q3에서 q1까지의 화살표에 있는 'a, c'보다 낮게 위치합니다.

마법처럼 고쳐졌습니다.

대안적이고 이국적인 해결책은 중요한 노드의 이름을 지정하고 중요한 노드의 위치에 따라 의 위치를 a,b,c​​정의하는 것입니다 . b그건:

\draw (q1) -- node[shift={(label-abc.west)}, xshift=-3.5mm] {b} ++ (q3);

전체 코드:

\documentclass[border=5mm]{standalone}

\usepackage{tikz}
\usetikzlibrary{automata,arrows,calc,positioning}

\begin{document}
\begin{tikzpicture}[->,node distance=25mm, text height=1ex, text depth=0pt]
\node[state,initial,accepting] (q0) {$q_0$};
\node[state,right=of q0] (q1) {$q_1$};
\node[state,right=of q1] (q2) {$q_2$};
\node[state,below=of $(q1)!0.5!(q2)$] (q3) {$q_3$};
\node[state,accepting,right=of q2] (q4) {$q_4$};

\draw (q0) edge[loop above] node[above] {a, b} (q0);
\draw (q0) -- node[above] {b, c} ++ (q1);
\draw (q1) edge[loop above] node[above] {a, c} (q1);
\draw (q1) edge[bend left] node[above] {a, b, c} (q2);
\draw (q2) -- node[below] {a, c} ++ (q1);
\draw (q2) edge[bend left] node[right] {b} (q3);             % ! duo B
\draw (q3) -- node[above left](label-abc) {a, b, c} ++ (q2); % ! duo A
\draw (q3) edge[bend left] node[left] {a, c} (q1);           % ! duo B
\draw (q1) -- node[shift={(label-abc.west)}, xshift=-3.5mm] {b} ++ (q3); % ! duo A
\draw (q2) edge[loop above] node[above] {a, b, c} (q2);
\draw (q2) -- node[above] {c} ++ (q4);
\draw (q1) edge[out=60,in=120] node[above] {c} (q4);
\end{tikzpicture}
\end{document}

여기에 이미지 설명을 입력하세요

참고: 두 가지를 모두 설정하면 다음 text heighttext depth같은 문자의 경우 정말 도움이 됩니다.,,g등등.

예를 들어:

\documentclass[border=5mm]{standalone}

\usepackage{tikz}
\usetikzlibrary{automata,arrows,calc,positioning,fit}

\begin{document}
\begin{tikzpicture}[->,node distance=25mm, text height=1ex, text depth=0pt]
\node[state,initial,accepting] (q0) {$q_0$};
\node[state,right=of q0] (q1) {$q_1$};
\node[state,right=of q1] (q2) {$q_2$};
\node[state,below=of $(q1)!0.5!(q2)$] (q3) {$q_3$};
\node[state,accepting,right=of q2] (q4) {$q_4$};

\draw (q0) edge[loop above] node[above] {a, b} (q0);
\draw (q0) -- node[above] {b, c} ++ (q1);
\draw (q1) edge[loop above] node[above] {a, c} (q1);
\draw (q1) edge[bend left] node[above] {a, b, c} (q2);
\draw (q2) -- node[below] {a, c} ++ (q1);
\draw (q2) edge[bend left] node[right] (label-g) {g} (q3);             % ! duo B
\draw (q3) -- node[draw,above left](label-abc) {a, b, c} ++ (q2); % ! duo A
\draw (q3) edge[bend left] node[left] (label-ac) {a, c} (q1);           % ! duo B
\draw (q1) -- node[draw,above right] {q} ++ (q3);                 % ! duo A
\draw (q2) edge[loop above] node[above] {a, b, c} (q2);
\draw (q2) -- node[above] {c} ++ (q4);
\draw (q1) edge[out=60,in=120] node[above] {c} (q4);

\node[draw,fit=(label-g)(label-ac)]{};
\draw (label-ac.base)--(label-g.base);
\end{tikzpicture}
\end{document}

결과:

여기에 이미지 설명을 입력하세요

답변3

다이어그램 코드를 더 짧게 만드는 방법을 연습하기 위해 대부분 주제에서 벗어났습니다. :-). 또한 tikz라이브러리 '인용문'을 사용하기 위한 몇 가지 추가 설명이 제공되고 제안되었습니다 .

다른 답변에서 언급했듯이 가장자리 레이블에 쉼표 공백을 정의해야 합니다. 이는 두 가지 방법으로 수행할 수 있습니다.

  • 제안대로클라우디오 피안드리노: 결정 text height=1ex하고 설정합니다 text depth=0pt. 이는 문자 b와 쉼표가 각각 노드의 위쪽 및 아래쪽 테두리에 닿기 때문입니다.
  • @user11232 제안대로: 각 경사 가장자리 레이블의 노드 콘텐츠에 스트럿을 사용하여 해당 노드를 높이와 깊이와 동일하게 만듭니다.

아래 mwe에서 나는 다음을 따릅니다.클라우디오 피안드리노그러나 노드 크기는 다르게 정의되며 제 생각에는 더 정확합니다. 가장자리 라벨에는 다음이 사용됩니다 edge quotes.

\documentclass[tikz, border=3mm]{standalone}
\usetikzlibrary{arrows.meta, automata,
                calc,
                positioning,
                quotes}

\begin{document}
    \begin{tikzpicture}[-Straight Barb,
node distance = 25mm,
         auto = left,
every edge quotes/.style = {inner sep=1pt,     % that labels are closer to edges
                            text height=1.5ex, % equal height, 
                            text depth=2pt,    % space for commas
                                               % however this depth is not sufficient for letters as p,q, ...
                                               % for them is better 0.25ex or slightly more
                            font=\small}       % smaller letters, gives a nicer result
                        ]
\node (q0)  [state,initial,accepting]         {$q_0$};
\node (q1)  [state,right=of q0]               {$q_1$};
\node (q2)  [state,right=of q1]               {$q_2$};
\node (q3)  [state,below=of $(q1)!0.5!(q2)$]  {$q_3$};
\node (q4)  [state,accepting,right=of q2]     {$q_4$};
%
\draw   (q0) edge[loop above, "{a, b}"]     ()
        (q0) edge["{b, c}"]                 (q1)
        (q1) edge[loop above, "{a, c}"]     ()
        (q1) edge[bend left, "{a, b, c}"]   (q2)
        (q2) edge["{a, c}"]                 (q1)
        (q2) edge[loop above, "{a, b, c}"]  ()
        (q2) edge[bend left, "b"]           (q3)    % ! duo B
        (q2) edge["c"]                      (q4)
        (q3) edge["{a, b, c}"]              (q2)    % ! duo A
        (q3) edge[bend left, "{a, c}"]      (q1)    % ! duo B
        (q1) edge["b"]                      (q3)    % ! duo A
        (q1) edge[out=60,in=120, "c"]       (q4);
\end{tikzpicture}
\end{document}

여기에 이미지 설명을 입력하세요

관련 정보