节点之间的箭头行为奇怪

节点之间的箭头行为奇怪

我想编写一些用于绘制 MPM 图的宏。这是我可以提供的 MWE:

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{arrows.meta}

\newcommand\noeud[6]{%
  % #1 : Task name, #2 : task duration, #3 : sooner start time,
  % #4 : later start time, #5 : total margin, #6 : free margin
  \begin{tikzpicture}
    \draw[black,thick] ++(-0.8,0) -- (0.8,0);
    \draw[black,thick] ++(0,-0.6) -- (0,0.6);
    \draw[black,thick] ++(-0.8,-0.6) rectangle ++(1.6,1.8);
    \draw[black,thick] ++(-0.8,0.6) rectangle ++(1.6,0);
    \draw[black] ++(0,0.9) node{#1\ (#2)};
    \draw[blue] ++(-0.4,0.3) node{#3};
    \draw[green!75!black] ++(0.4,0.3) node{#4};
    \draw[green!75!black] ++(-0.4,-0.3) node{#5};
    \draw[violet] ++(0.4,-0.3) node{#6};
  \end{tikzpicture}
}
\newcommand\noeudcritique[6]{%
  % #1 : Task name, #2 : task duration, #3 : sooner start time,
  % #4 : later start time, #5 : total margin, #6 : free margin
  \begin{tikzpicture}
    \draw[black,thick] ++(-0.8,0) -- (0.8,0);
    \draw[black,thick] ++(0,-0.6) -- (0,0.6);
    \draw[red,thick] ++(-0.8,-0.6) rectangle ++(1.6,1.8);
    \draw[black,thick] ++(-0.8,0.6) rectangle ++(1.6,0);
    \draw[black] ++(0,0.9) node{#1\ (#2)};
    \draw[blue] ++(-0.4,0.3) node{#3};
    \draw[green!75!black] ++(0.4,0.3) node{#4};
    \draw[green!75!black] ++(-0.4,-0.3) node{#5};
    \draw[violet] ++(0.4,-0.3) node{#6};
  \end{tikzpicture}
}

\newcommand{\fleche}[5]{%
  % #1 : starting node, #2 : ending node,
  % #3 : sooner start time forward, #4 : sooner start time backward,
  % #5 : later start time
  \begin{tikzpicture}
    \draw[thick,black,-Latex] (#1) node[near start,above,blue]{#4}
    node[near start,below,green!75!black]{#5}
    -- (#2) %node[near end,above,black]{#3};
    ;
  \end{tikzpicture}
}

\newcommand{\flechecritique}[5]{%
  % #1 : starting node, #2 : ending node,
  % #3 : sooner start time forward, #4 : sooner start time backward,
  % #5 : later start time
  \begin{tikzpicture}
    \draw[thick,red,-Latex] (#1) node[near start,above,blue]{#4}
    node[near start,below,green!75!black]{#5}
    -- (#2) node[near end,above,black]{#3};
  \end{tikzpicture}
}

\begin{document}

\begin{center}
  \begin{tikzpicture}[every node/.style={inner sep = 0}]
    \node (A) at (0,0) {\noeudcritique{A}{2}{0}{0}{0}{0}};
    \node (B) at (3,2) {\noeud{B}{5}{2}{6}{4}{2}};
    \node (C) at (3,-2) {\noeudcritique{C}{7}{2}{2}{0}{0}};
    % \draw[thick,black,-Latex] (A) node[near start,above,blue]{#4}
    % node[near start,below,green!75!black]{#5}  -- (B) node[near
    % end,above,black]{#3};
    \fleche{A}{B}{2}{4}{5};
    \flechecritique{A}{C}{2}{2}{2};
  \end{tikzpicture}
\end{center}

\end{document}

如果你编译它,你会注意到箭头没有正确放置。

在此处输入图片描述

我哪里做错了 ?

我想要这样的结果:

在此处输入图片描述

或这个 :

在此处输入图片描述

这个想法是,在节点的前向更新期间绘制蓝色标签(它们是任务的最小开始日期),在节点的后向过程中绘制绿色(在我的例子中是紫色)标签(它们是最大结束日期)。

那些图片是通过手工排版实现的,但是输入起来很长,我想稍微自动化一些。

谢谢你的帮助。

\再见

答案1

提供的 MWE 有嵌套的tikzpictures,一般来说效果不佳。但是,节点是表,因此您也可以tabular为此使用环境。

另一个问题是代码中箭头节点的位置。节点应在箭头后指定,即(a) --(b) [nodes here]。这纠正了箭头标签的位置。

\noeud可以通过从命令中调用该命令\noeudcritique并使用线条颜色的额外参数来进一步简化代码(否则两个命令是相同的)。

表格的代码变得有点混乱,表格的线条颜色、线条粗细和单元格填充的精确配置需要结合\arraystretch、、、和一些使用彩色\tabcolsep的特殊表格列规范。除此之外,它相对简单\arrayrulewidth:)\arrayrulecolor\vrule

在下面的 MWE 中,指定了表格边框上箭头的起点和终点的位置,否则箭头将从节点的中心(表格中间)开始和结束。这可以通过诸如 等位置或度数(从 0 到 360,向左转(逆时针),0/360 在中间右侧(即东))来完成north westsouth此外,我稍微调整了above和 的below位置,使数字不与箭头重叠。

梅威瑟:

\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{tikz}
\usetikzlibrary{arrows.meta}

\newcommand{\noeud}[7][black]{%
\renewcommand{\arraystretch}{1.5}%
\setlength{\tabcolsep}{1em}%
\setlength\arrayrulewidth{1pt}%
\arrayrulecolor{#1}%
\begin{tabular}{|c!{\color{black}\vrule}c|}
\hline
\arrayrulecolor{black}%
\multicolumn{2}{!{\color{#1}\vrule width 1pt}c!{\color{#1}\vrule width 1pt}}{#2 (#3)}\\
\hline
\textcolor{blue}{#4} & \textcolor{green}{#5}\\
\hline
\textcolor{green}{#6} & \textcolor{violet}{#7}\\
\arrayrulecolor{#1}%
\hline
\end{tabular}%
}

\newcommand\noeudcritique[6]{%
\noeud[red]{#1}{#2}{#3}{#4}{#5}{#6}%
}

\newcommand{\fleche}[5]{%
  % #1 : starting node, #2 : ending node,
  % #3 : sooner start time forward, #4 : sooner start time backward,
  % #5 : later start time
  \draw[thick,black,-Latex] (#1) -- (#2) node[near start,above=3pt,blue]{#4}
    node[near start,below=3pt,green!75!black]{#5} node[near end,above=4pt,black]{#3}
    ;
}

\newcommand{\flechecritique}[5]{%
  % #1 : starting node, #2 : ending node,
  % #3 : sooner start time forward, #4 : sooner start time backward,
  % #5 : later start time
  \draw[thick,red,-Latex] (#1) -- (#2) node[near start,above=3pt,blue]{#4}
    node[near start,below=3pt,green!75!black]{#5} node[near end,above=4pt,black]{#3}
    ;
}

\begin{document}

\begin{center}
  \begin{tikzpicture}[every node/.style={inner sep = 0}]
    \node (A) at (0,0) {\noeudcritique{A}{2}{0}{0}{0}{0}};
    \node (B) at (3,2) {\noeud{B}{5}{2}{6}{4}{2}};
    \node (C) at (3,-2) {\noeudcritique{C}{7}{2}{2}{0}{0}};
    \fleche{A.north east}{B.210}{2}{4}{5}
    \flechecritique{A.330}{C.160}{2}{2}{2}
  \end{tikzpicture}
\end{center}

\end{document}

结果:

在此处输入图片描述

答案2

不清楚你喜欢画什么。我猜是类似下图的东西:

在此处输入图片描述

编辑: 现在重现第一张图片...

上图是用

\documentclass[tikz, margin=3.14159mm]{standalone}  % :-)
\usetikzlibrary{arrows.meta,
                fit,
                matrix,
                positioning}
\newcommand\matrixgrid[1]{
    \draw   (#1-1-1.south west) -- (#1-1-2.south east)
            (#1-2-1.south west) -- (#1-2-2.south east)
            (#1-1-1.south east) -- (#1-3-1.south east);
                        }

\begin{document}
  \begin{tikzpicture}[
M/.style = {matrix of nodes,
            nodes in empty cells,
            nodes = {thin,minimum width=2em, minimum height=3ex, anchor=center},
            column sep=-\pgflinewidth,
            row sep=-\pgflinewidth,
            %
            row 2 column 1/.style={text=blue},
            row 2 column 2/.style={text=teal},
            row 3 column 1/.style={text=teal},
            row 2 column 1/.style={text=purple},
            %
            inner sep=0pt, draw=#1, thick},
       arr/.style = {draw, semithick, - Stealth},
       lbl/.style = {auto, font=\footnotesize, text=#1, inner sep=2pt, sloped}
                    ]
\matrix (m1) [M=red]
{
    &       \\
0   &    0  \\
0   &    0  \\
};
\node[fit=(m1-1-1) (m1-1-2), label=center:{$A(1)$}] {};
\matrixgrid{m1};
%
\matrix (m2) [M=black, above right=of m1]
{
    &       \\
2   &    6  \\
4   &    2  \\
};   
\node[fit=(m2-1-1) (m2-1-2), label=center:{$A(1)$}] {};
\matrixgrid{m2};
%
%
\matrix (m2) [M=black, above right=of m1]
{
    &       \\
2   &    6  \\
4   &    2  \\
};   
\node[fit=(m2-1-1) (m2-1-2), label=center:{$A(1)$}] {};
\matrixgrid{m2};
%
\matrix (m3) [M=black,below right=of m1]
{
    &       \\
2   &    2  \\
0   &    0  \\
};
\node[fit=(m3-1-1) (m3-1-2), label=center:{$A(1)$}] {};
\matrixgrid{m3};
%
\draw[arr]  (m1) -- node [lbl=blue,above] {4}
                    node [lbl=green, near start, below] {5}
                    node [lbl=black, near end, above]  {2} (m2-3-1.west);
\draw[arr]  (m1) -- node [lbl=green,below] {2}
                    node [lbl=blue, near start, above] {5}
                    node [lbl=black, near end, above]  {2} (m3-1-1.west);
  \end{tikzpicture}

\end{document}

通过定义在矩阵中绘制线条的新命令可以进一步缩短时间。

上述解决方案是否是您想要的?

相关内容