4

I want to draw a straight line leaving the down left tip of my square D.

My code is the following

\documentclass{report}
\usepackage{tikz}
\begin{document}
\begin{figure}
    \begin{tikzpicture}[node distance=4cm]
        \usetikzlibrary{shapes}
        \node[regular polygon,regular polygon sides=4,scale=3,dashed,draw](D) {$D$};
        \node[draw, right of=D,yshift=1cm, node distance=6cm](G){G};
        \node[right of=D, yshift=1cm, node distance =3.5cm] (ygets) {$y \gets U_{2n}$};
        \node[right of=D, yshift=-1.22cm, node distance=2.5cm](vazio){};
        \node[above of=G, node distance=1cm] (k) {$k \gets U_n$};


        \path[draw,->] (G.south) |- (D.east);
        \path[draw,->] (k.south) -- (G.north);
        \path[draw,->] (ygets.south) |- node[near end, above]{$y$} (D.east);
        \path[draw,->] (D.south east) -- node[above] {b} (vazio);

    \end{tikzpicture}
\end{figure}
\end{document}

The resulting figure is

Arrow with letter b should be straight

Any tips or tricks on how to do this?

Marc Kees
  • 621

2 Answers2

4

You have two ways:

\draw[->] (D.south east) -- ++(2,0) node[above,midway] {b};

This one starts from D.south east and proceeds horizontally for 2 (or any number you want, it also takes measures, like 1cm) and 0 vertically, so it goes to the right. Then you have

\draw[->] (D.south east) -- (D.south east-|k);

This one starts at the same point and continues until it reaches the node k, but still going horizontally, and there it stops. Which solution you decide to adopt depends on the needs of the case. By the way, writing \draw instead of \path[draw] is easier, and the first command is actually the short version of the second one.

Alenanno
  • 37,338
3

There are other ways as well:

Instead of your existing definition of vazio, define it as

\coordinate[right=1.5cm of D.south east] (vazio);

The syntax right=<len> of ... requires the positioning library, so you have to add \usetikzlibrary{positioning} to the preamble. Note that right=of is recommended over right of=, see Difference between "right of=" and "right=of" in PGF/TikZ

A slightly more complicated approach, which would also work if D was rotated, is to load the calc library, and say

\draw[->] (D.south east) -- node[above] {b} ($(D.south west)!1.6!(D.south east)$);

The value 1.6 can be adjusted to what is useful. The coordinate ($(D.south west)!1.6!(D.south east)$) is the point on the line between the two bottom corners of D that is 1.6 times the distance between them, away from D.south west.

Output and complete code below. Both alternatives are added here, hence two arrows in the image.

enter image description here

\documentclass{report}
\usepackage{tikz}
\usetikzlibrary{positioning,calc}
\begin{document}
\begin{figure}
    \begin{tikzpicture}[node distance=4cm]
        \usetikzlibrary{shapes}
        \node[regular polygon,regular polygon sides=4,scale=3,dashed,draw](D) {$D$};
        \node[draw, right of=D,yshift=1cm, node distance=6cm](G){G};
        \node[right of=D, yshift=1cm, node distance =3.5cm] (ygets) {$y \gets U_{2n}$};
        % option 1
        \coordinate[right=1.5cm of D.south east] (vazio);

        \node[above of=G, node distance=1cm] (k) {$k \gets U_n$};


        \draw[->] (G.south) |- (D.east);
        \draw[->] (k.south) -- (G.north);
        \draw[->] (ygets.south) |- node[near end, above]{$y$} (D.east);
        \draw[->] (D.south east) -- node[above] {b1} (vazio);

        % option 2
        \draw[->] (D.south east) -- node[below] {b2} ($(D.south west)!1.6!(D.south east)$);
    \end{tikzpicture}
\end{figure}
\end{document}
Torbjørn T.
  • 206,688
  • I can see the utility of the second method, but the first method is unnecessarily long. I mean you could do the same using the first method I suggested doing \draw[->] (D.south east) -- ++(1.5cm,0) coordinate (vazio);, right? I can't think of any case where defining a coordinate separately, for the sole purpose of drawing a path, would be preferable. – Alenanno Jan 06 '16 at 11:20
  • @Alenanno The first method was merely an improved version of what the OP had tried already (\node[right of=D, yshift=-1.22cm, node distance=2.5cm](vazio){};, and drawing the line to vazio). – Torbjørn T. Jan 06 '16 at 11:57
  • Yes, I understand. I was just wondering if maybe I had missed something about it. :P – Alenanno Jan 06 '16 at 12:00