Skip to content

Comment dessiner automatiquement des lignes parallèles ?

Après avoir enquêté sur différents référentiels et sites, nous avons finalement trouvé la solution que nous allons vous montrer maintenant.

Solution :

Quelque chose comme ceci ?

documentclass{scrartcl}
usepackage{tikz}

tikzset{
         signal/.style = coordinate,
         block/.style = {
                         draw,
                         rectangle,
                         minimum height = 2em,
                         minimum width = 4em
                        }
        }
usetikzlibrary{decorations.markings,positioning}
tikzset{triple line with arrows/.style args={#1,#2,#3}{decorate,decoration={markings,%
mark=at position 0 with {coordinate (ta-base-1) at (0,4pt);
coordinate (ta-base-2) at (0,0pt);
coordinate (ta-base-3) at (0,-4pt);},
mark=at position 1 with {draw[#1] (ta-base-1) -- (0,4pt);
draw[#2] (ta-base-2) -- (0,0pt);
draw[#3] (ta-base-3) -- (0,-4pt);}}}}

begin{document}

  begin{tikzpicture}

    node[signal] (input) {};
    node[
          block,
          right = of input
         ] (block 1) {};
    node[
          block,
          right = of block 1
         ] (block 2) {};
    node[
          signal,
          right = of block 2
         ] (output) {};
    draw
      [->] (input) -- (block 1);
    draw
      [triple line with arrows={->,->,->}] (block 1) -- (block 2);
    draw
      [triple line with arrows={->,->,->}] (block 2) -- (output);

  end{tikzpicture}

end{document}

Entrez la description de l'image ici

ADDENDUM: Juste pour le plaisir : une version légèrement plus souple.

documentclass{scrartcl}
usepackage{tikz}

tikzset{
         signal/.style = coordinate,
         block/.style = {
                         draw,
                         rectangle,
                         minimum height = 2em,
                         minimum width = 4em
                        }
        }
usetikzlibrary{decorations.markings,positioning}
pgfkeys{tikz/.cd,
         triple line distance/.store in =triplelinedist,
         triple line distance=4pt
}
tikzset{triple line with arrows/.style args={#1,#2,#3}{decorate,decoration={markings,%
mark=at position 0 with {coordinate (ta-base-1) at (0,triplelinedist);
coordinate (ta-base-2) at (0,0pt);
coordinate (ta-base-3) at (0,-triplelinedist);},
mark=at position 1 with {draw[#1] (ta-base-1) -- (0,triplelinedist);
draw[#2] (ta-base-2) -- (0,0pt);
draw[#3] (ta-base-3) -- (0,-triplelinedist);}}}}

begin{document}

  begin{tikzpicture}

    node[signal] (input) {};
    node[
          block,
          right = of input
         ] (block 1) {};
    node[
          block,
          right = of block 1
         ] (block 2) {};
    node[
          signal,
          right = of block 2
         ] (output) {};
    draw
      [->] (input) -- (block 1);
    draw
      [triple line with arrows={->,->,->}] (block 1) -- (block 2);
    draw
      [triple line with arrows={->,->,->},triple line distance=5pt] (block 2) -- (output);

  end{tikzpicture}

end{document}

Entrez la description de l'image ici

Vous pouvez faire usage de la foreach boucle

documentclass{scrartcl}
usepackage{tikz}

tikzset{
         signal/.style = coordinate,
         block/.style = {
                         draw,
                         rectangle,
                         minimum height = 2em,
                         minimum width = 4em
                        }
         }
usetikzlibrary{positioning}

begin{document}

  begin{tikzpicture}

  node[signal] (input) {};
  node[
        block,
        right = of input
       ] (block 1) {};
  node[
        block,
        right = of block 1
       ] (block 2) {};
  node[
        signal,
        right = of block 2
       ] (output) {};
  draw
    [->] (input) -- (block 1);
  foreach k in {-1,0,1}{
    draw [->] ([yshift=k*.2cm]block 1.east) -- ([yshift=k*.2cm]block 2.west);
    draw [->] ([yshift=k*.2cm]block 2.east) -- ([yshift=k*.2cm]output.west);
  }
  end{tikzpicture}

end{document}

résultat

Je décale les positions des flèches vers le haut et vers le bas d'une certaine quantité en fonction de la valeur de k (-1, 0, et enfin 1).

Je cite sans vergogne ma propre réponse ici (légèrement ajustée pour ce cas).

J'ai défini une nouvelle forme block qui a trois options qui peuvent être données au style comme suit. block={inputs=,outputs=,io spacing=}:

  • inputs Nombre entier arbitraire d'entrées, la valeur par défaut est 1.
  • outputs Nombre entier arbitraire de sorties, valeur par défaut : 1
  • io spacing Espacement entre les entrées et les sorties, valeur par défaut : 5mm.

Avec cette forme/style, nous pouvons dessiner :

Entrez la description de l'image ici

Avec seulement (utilise aussi le positioning bibliothèque, voir le MWE en bas) :

begin{tikzpicture}
    node[block={outputs=3}](node1){};
    node[block={inputs=3,outputs=3},right=of node1](node2){};

    draw[<-] (node1.input 1) -- ++(-1,0);

    foreach i in {1,...,3}{
        draw[->] (node1.output i) -- (node2.input i);
        draw[->] (node2.output i) --++(1,0);
    }
end{tikzpicture}

Cela pourrait être un peu exagéré pour votre problème, mais qui sait, vous pourriez avoir des demandes supplémentaires qui peuvent être satisfaites avec cela :).

La définition complète de la forme est :

makeatletter
newdimen[email protected]
newdimen[email protected]

newif[email protected]@flip

tikzset{
    block/.code={
        pgfkeys{
            /block/.cd,
            #1
        }
        pgfmathsetlengthmacro{[email protected]}{%
            max(10mm, int(max([email protected],[email protected]) * [email protected]))}
        tikzset{
            draw,
            align=center,
            minimum width = 15mm,
            minimum height = [email protected],
            shape=block
        }
    }
}
pgfkeys{
    /block/.is family,
    /block/.cd,
    inputs/.code={
        pgfmathparse{int(#1)}
        let[email protected]=pgfmathresult
    },
    inputs=1,
    outputs/.code={
        pgfmathparse{int(#1)}
        let[email protected]=pgfmathresult
    },
    outputs=1,
    io spacing/.code=setlength[email protected]{#1},
    io spacing=5mm,
    flip/.is choice,
    flip/true/.code={@[email protected]def@[email protected]{1}},
    flip/false/.code={@[email protected]def@[email protected]{0}},
    flip/.default=true,
    flip=false,
}

pgfdeclareshape{block}{
    inheritsavedanchors[from={rectangle}]
    savedanchorcenterpoint{%
        [email protected]=.5wdpgfnodeparttextbox%
        [email protected]=.5htpgfnodeparttextbox%
        advance[email protected] by-.5dppgfnodeparttextbox%
    }
    inheritsavedanchors[from=rectangle]
    inheritanchorborder[from=rectangle]
    inheritanchor[from=rectangle]{north}
    inheritanchor[from=rectangle]{north west}
    inheritanchor[from=rectangle]{north east}
    inheritanchor[from=rectangle]{center}
    inheritanchor[from=rectangle]{text}
    inheritanchor[from=rectangle]{west}
    inheritanchor[from=rectangle]{east}
    inheritanchor[from=rectangle]{mid}
    inheritanchor[from=rectangle]{mid west}
    inheritanchor[from=rectangle]{mid east}
    inheritanchor[from=rectangle]{base}
    inheritanchor[from=rectangle]{base west}
    inheritanchor[from=rectangle]{base east}
    inheritanchor[from=rectangle]{south}
    inheritanchor[from=rectangle]{south west}
    inheritanchor[from=rectangle]{south east}
    savedmacroblockinputs{%
        pgfmathparse{int([email protected])}%
        letblockinputs=pgfmathresult}%
    savedmacroblockoutputs{%
        pgfmathparse{int([email protected])}%
        letblockoutputs=pgfmathresult}%
    savedmacroblockmaxio{%
        pgfmathparse{int(max([email protected],[email protected]))}%
        letblockmaxio=pgfmathresult}%
    savedmacroblockflip{%
        pgfmathparse{@[email protected]}%
        letblockflip=pgfmathresult}%
    saveddimenhalfwidth{pgfmathsetlength[email protected]{%
            pgfkeysvalueof{/pgf/minimum width}/2}pgfmathresult}
    saveddimenhalfheight{pgfmathsetlength[email protected]{%
            pgfkeysvalueof{/pgf/minimum height}/2}pgfmathresult}
    saveddimeniospacing{pgfmathsetlength[email protected]{%
            [email protected]}pgfmathresult}
    inheritbackgroundpath[from={rectangle}]
    [email protected]@[email protected][email protected]@[email protected]{%
        pgfmathloop%
        ifnumpgfmathcounter>blockinputsrelax%
        else%
        [email protected]{[email protected]@[email protected] pgfmathcounter}{%
            expandafterxdefcsname [email protected]@[email protected] %
            pgfmathcounterendcsname{noexpand%
                [email protected]@[email protected]@[email protected]{pgfmathcounter}%
            }%
        }{}
        repeatpgfmathloop%
        pgfmathloop%
        ifnumpgfmathcounter>blockoutputsrelax%
        else%
        [email protected]{[email protected]@[email protected] pgfmathcounter}{%
            expandafterxdefcsname [email protected]@[email protected] %
            pgfmathcounterendcsname{noexpand%
                [email protected]@[email protected]@[email protected]{pgfmathcounter}%
            }%
        }{}
        repeatpgfmathloop%
    }%
}

def[email protected]@[email protected]@[email protected]#1{%
    [email protected]{centerpoint}%
    [email protected]=[email protected]%
    ifnumblockflip=0relax%
        [email protected]{southwest}%
    else%
        [email protected]{northeast}%
    fi%
    pgfmathsetlength[email protected]{[email protected] + (0.5*(blockinputs+1)-#1)*iospacing}
}
def[email protected]@[email protected]@[email protected]#1{%
    [email protected]{centerpoint}%
    [email protected]=[email protected]%
    ifnumblockflip=0relax%
        [email protected]{northeast}%
    else%
        [email protected]{southwest}%
    fi%
    pgfmathsetlength[email protected]{[email protected] + (0.5*(blockoutputs+1)-#1)*iospacing}
}
makeatother

MWE (copy-pastcapable) :

documentclass[tikz,margin=2mm]{standalone}

usetikzlibrary{positioning}

makeatletter
newdimen[email protected]
newdimen[email protected]

newif[email protected]@flip

tikzset{
    block/.code={
        pgfkeys{
            /block/.cd,
            #1
        }
        pgfmathsetlengthmacro{[email protected]}{%
            max(10mm, int(max([email protected],[email protected]) * [email protected]))}
        tikzset{
            draw,
            align=center,
            minimum width = 15mm,
            minimum height = [email protected],
            shape=block
        }
    }
}
pgfkeys{
    /block/.is family,
    /block/.cd,
    inputs/.code={
        pgfmathparse{int(#1)}
        let[email protected]=pgfmathresult
    },
    inputs=1,
    outputs/.code={
        pgfmathparse{int(#1)}
        let[email protected]=pgfmathresult
    },
    outputs=1,
    io spacing/.code=setlength[email protected]{#1},
    io spacing=5mm,
    flip/.is choice,
    flip/true/.code={@[email protected]def@[email protected]{1}},
    flip/false/.code={@[email protected]def@[email protected]{0}},
    flip/.default=true,
    flip=false,
}

pgfdeclareshape{block}{
    inheritsavedanchors[from={rectangle}]
    savedanchorcenterpoint{%
        [email protected]=.5wdpgfnodeparttextbox%
        [email protected]=.5htpgfnodeparttextbox%
        advance[email protected] by-.5dppgfnodeparttextbox%
    }
    inheritsavedanchors[from=rectangle]
    inheritanchorborder[from=rectangle]
    inheritanchor[from=rectangle]{north}
    inheritanchor[from=rectangle]{north west}
    inheritanchor[from=rectangle]{north east}
    inheritanchor[from=rectangle]{center}
    inheritanchor[from=rectangle]{text}
    inheritanchor[from=rectangle]{west}
    inheritanchor[from=rectangle]{east}
    inheritanchor[from=rectangle]{mid}
    inheritanchor[from=rectangle]{mid west}
    inheritanchor[from=rectangle]{mid east}
    inheritanchor[from=rectangle]{base}
    inheritanchor[from=rectangle]{base west}
    inheritanchor[from=rectangle]{base east}
    inheritanchor[from=rectangle]{south}
    inheritanchor[from=rectangle]{south west}
    inheritanchor[from=rectangle]{south east}
    savedmacroblockinputs{%
        pgfmathparse{int([email protected])}%
        letblockinputs=pgfmathresult}%
    savedmacroblockoutputs{%
        pgfmathparse{int([email protected])}%
        letblockoutputs=pgfmathresult}%
    savedmacroblockmaxio{%
        pgfmathparse{int(max([email protected],[email protected]))}%
        letblockmaxio=pgfmathresult}%
    savedmacroblockflip{%
        pgfmathparse{@[email protected]}%
        letblockflip=pgfmathresult}%
    saveddimenhalfwidth{pgfmathsetlength[email protected]{%
            pgfkeysvalueof{/pgf/minimum width}/2}pgfmathresult}
    saveddimenhalfheight{pgfmathsetlength[email protected]{%
            pgfkeysvalueof{/pgf/minimum height}/2}pgfmathresult}
    saveddimeniospacing{pgfmathsetlength[email protected]{%
            [email protected]}pgfmathresult}
    inheritbackgroundpath[from={rectangle}]
    [email protected]@[email protected][email protected]@[email protected]{%
        pgfmathloop%
        ifnumpgfmathcounter>blockinputsrelax%
        else%
        [email protected]{[email protected]@[email protected] pgfmathcounter}{%
            expandafterxdefcsname [email protected]@[email protected] %
            pgfmathcounterendcsname{noexpand%
                [email protected]@[email protected]@[email protected]{pgfmathcounter}%
            }%
        }{}
        repeatpgfmathloop%
        pgfmathloop%
        ifnumpgfmathcounter>blockoutputsrelax%
        else%
        [email protected]{[email protected]@[email protected] pgfmathcounter}{%
            expandafterxdefcsname [email protected]@[email protected] %
            pgfmathcounterendcsname{noexpand%
                [email protected]@[email protected]@[email protected]{pgfmathcounter}%
            }%
        }{}
        repeatpgfmathloop%
    }%
}

def[email protected]@[email protected]@[email protected]#1{%
    [email protected]{centerpoint}%
    [email protected]=[email protected]%
    ifnumblockflip=0relax%
        [email protected]{southwest}%
    else%
        [email protected]{northeast}%
    fi%
    pgfmathsetlength[email protected]{[email protected] + (0.5*(blockinputs+1)-#1)*iospacing}
}
def[email protected]@[email protected]@[email protected]#1{%
    [email protected]{centerpoint}%
    [email protected]=[email protected]%
    ifnumblockflip=0relax%
        [email protected]{northeast}%
    else%
        [email protected]{southwest}%
    fi%
    pgfmathsetlength[email protected]{[email protected] + (0.5*(blockoutputs+1)-#1)*iospacing}
}
makeatother

begin{document}
    begin{tikzpicture}
        node[block={outputs=3}](node1){};
        node[block={inputs=3,outputs=3},right=of node1](node2){};

        draw[<-] (node1.input 1) -- ++(-1,0);

        foreach i in {1,...,3}{
            draw[->] (node1.output i) -- (node2.input i);
            draw[->] (node2.output i) --++(1,0);
        }
    end{tikzpicture}
end{document}

Vous avez la possibilité de protéger notre travail en laissant un commentaire ou en laissant une note, nous vous souhaitons la bienvenue.



Utilisez notre moteur de recherche

Ricerca
Generic filters

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée.