2

In this question, I supplied a code to take an external node as a reference and I`ve got satisfying answers.

Using these answers to draw from an inline node I used the following code:

\begin{tikzpicture}
\draw [thick,-latex](0,0) -- (5,0);
\draw [thick,-latex](0,0) -- (0,4.);
\draw [thick, violet, dotted] (4,3) node (oo){} node [xshift=0cm, yshift=0cm] {oo} (oo.center-|0,0) node [xshift=0cm, yshift=0cm] {y} -| (oo.center|-0,0) node [xshift=0cm, yshift=0cm] {x};
\end{tikzpicture}

to get

enter image description here

When omitting naming the starting point node in this code

    \draw [thick, violet, dotted] (4,3) node [xshift=0cm, yshift=0cm] {o} |- (0,0) node [xshift=0cm, yshift=0cm] {y} -| (0,0) node [xshift=0cm, yshift=0cm] {x};

I got

enter image description here

How can this be fixed?

Here is the full code I use:

\documentclass{beamer}
\beamertemplatenavigationsymbolsempty
\usepackage{tikz}
\usepackage{pgfplots}
\begin{document}


\begin{tikzpicture}
\draw [thick,-latex](0,0) -- (5,0);
\draw [thick,-latex](0,0) -- (0,4.);
\draw [thick, violet, dotted] (4,3) node (oo){} node [xshift=0cm, yshift=0cm] {oo} (oo.center-|0,0) node [xshift=0cm, yshift=0cm] {y} -| (oo.center|-0,0) node [xshift=0cm, yshift=0cm] {x};
\end{tikzpicture}
\end{frame}

\begin{frame}[fragile,t]
\frametitle{}
\begin{tikzpicture}
\draw [thick,-latex](0,0) -- (5,0);
\draw [thick,-latex](0,0) -- (0,4.);
\draw [thick, violet, dotted] (4,3) node [xshift=0cm, yshift=0cm] {o} |- (0,0) node [xshift=0cm, yshift=0cm] {y} -| (0,0) node [xshift=0cm, yshift=0cm] {x};
\end{tikzpicture}
\end{document}
Hany
  • 4,709

3 Answers3

5

I suggest to use two draw commands instead of one and the command \currentcoordinate from this answer.

\documentclass{standalone}
\usepackage{tikz}
% Taken from https://tex.stackexchange.com/a/132926/110998
\makeatletter
\newcommand\currentcoordinate{\the\tikz@lastxsaved,\the\tikz@lastysaved}
\makeatother
\begin{document}
\begin{tikzpicture}
\draw [thick,-latex](0,0) -- (5,0);
\draw [thick,-latex](0,0) -- (0,4.);
\draw [thick, violet, dotted] (4,3) node {o} -- (\currentcoordinate-|0,0) node {y};
\draw [thick, violet, dotted] (4,3) -- (\currentcoordinate|-0,0) node {x};
\end{tikzpicture}
\end{document}

enter image description here

gernot
  • 49,614
  • Thank you for your answer, but it gives me the same result as my first code which is more simple using naming the node. What I want, if possible, is using the (0,0) method which does not need to define other coordinates than that of the node, without naming the node. – Hany Aug 12 '18 at 13:40
5

What you want can be achieved by using the calc library in combination with the let command.

You can use let \p1 = (<coordinate>) in somewhere in your path definition, to let \p1 correspond to that coordinate everywhere in the path definition. Additionally you can use the x and y coordinates as \x1 and \y1 which make the -| and |- notation unnecessary.

\documentclass{beamer}
\beamertemplatenavigationsymbolsempty
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}

\begin{frame}[t]
\begin{tikzpicture}
\draw [thick,-latex](0,0) -- (5,0);
\draw [thick,-latex](0,0) -- (0,4.);
\draw [thick, orange,dotted] let \p1 = (4,3) in {
    (\p1) -- node[above]{x} (0,\y1) 
    (\p1) -- node[right]{y} (\x1,0)
    (\p1) node[above right]{oo}};
\end{tikzpicture}
\end{frame}
\end{document}

Result:

enter image description here

The following example is to show that it works by only changing the coordinates for \p1.

\documentclass{beamer}
\beamertemplatenavigationsymbolsempty
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}

\foreach \t in {0,10,...,359}{
\begin{frame}[t]
\begin{tikzpicture}
\draw [thick,-latex](0,0) -- (5,0);
\draw [thick,-latex](0,0) -- (0,4.);
\draw [thick, orange,dotted] let \p1 = ({3+cos(\t)},{2+sin(\t)}) in {
    (\p1) -- node[above]{x} (0,\y1) 
    (\p1) -- node[right]{y} (\x1,0)
    (\p1) node[above right]{oo}};
\end{tikzpicture}
\end{frame}
}
\end{document}

Which results in

enter image description here

Let me conclude by saying that for this simple case the let command is probably overkill, but I cannot guess what you will use it for.

Max
  • 9,733
  • 3
  • 28
  • 35
  • Thank you for your answer, but it gives me the same result as my first code which is more simple using naming the node. What I want, if possible, is using the (0,0) method which does not need to define other coordinates than that of the node, without naming the node. – Hany Aug 12 '18 at 13:40
  • Thank you, but it uses two \draw commands. Can it be done using one \draw command. – Hany Aug 12 '18 at 14:34
  • Thank you. It is similar to my first method (with naming the node). What I want is using this method without naming the node. – Hany Aug 12 '18 at 16:54
  • Thank you all for your comments and answers. In my original code, my drawing is much more complicated with many nodes; and I want to avoid naming too many nodes. I think what I want is not possible. – Hany Aug 13 '18 at 05:32
  • The answer of J Leon V. answers my question and gives me what I want. Thank you all for your answers and comments. – Hany Aug 13 '18 at 05:55
  • Thank you, but this solution does not name the node, which I want to do; and it is very simple. – Hany Aug 13 '18 at 06:01
  • Just one more comment please. If I use many \drawing commands in the same tikzpicture, could they all have the same node name (P1) or should each one have a different name? Thank you. – Hany Aug 13 '18 at 06:12
5

In your code:

\draw[thick,violet,dotted](4,3)node[xshift=0cm, yshift=0cm]{o}|-(0,0) node[xshift=0cm, yshift=0cm]{y}-|(0,0)node[xshift=0cm, yshift=0cm]{x}

Is equivalent without nodes to:

\draw[draw_styles]
(4.3) %Initial path coordinate
    -|(0,0) % (4.3) Linked to (0,0) using cornered link (-|)
    -|(0,0); % (0.0) Linked to (0,0)

In the pevious code you:

\draw(4,3)node (oo){} node [xshift=0cm, yshift=0cm] {oo} (oo.center-|0,0) node [xshift=0cm, yshift=0cm] {y} -| (oo.center|-0,0) node [xshift=0cm, yshift=0cm] {x};

And its equvalent code without nodes is:

\draw[draw_styles]
(4.3) %Initial path coordinate <nothing is drawn, but you use to put a text named node>
(4.3 -| 0,0) % That gives a new coordinate (4,0) <4.3 is the equivalent of node_name.center.>
        -|(0,0 |- 4.3); % (4,0) Linked to (0,3) using cornered link (-|)

Both codes does not draw the respective lines from (4.3) point; the code that could do this is using edge and coordinate to not rewrite the coordinate when the intersection points using (A-|B) or (A|-B) are calculated.

The initial coordinate has a text node with the text {o}, but also has a label in position 45 degrees then the coordinate is asigned to P constant symbol, then two edges are drawn to points (P -| 0,0) and (P |- 0,0); the code to place text nodes in the lines by edge, is written between edge and the coordinate; but the position from 0 to 1 must be specified, being the extreme limits of the line, in the same way the text nodes can contain labels.

RESULT:

enter image description here

MWE:

\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows.meta}
\begin{document}
    \begin{tikzpicture}[
        font=\sffamily,
        >={Stealth[inset=0pt,length=6pt]},
        ]
    \draw [thick,->](0,0) -- (5,0);
    \draw [thick,->](0,0) -- (0,4.);
    %Option using edge
    \draw [thick, violet, dotted] (4,3) node[label=45:o,inner sep=-3pt]{o} coordinate (P)
        edge  node[pos=1,label=180:y,inner sep=-3pt]{y} (P -| 0,0) 
        edge node[pos=1,label=-90:x,inner sep=-3pt]{x}  (P |- 0,0) ;
    \end{tikzpicture}
\end{document}
J Leon V.
  • 11,533
  • 16
  • 47
  • Thank you very much for your answer and explanation. It gives me what I want. Just one comment, using label=180:y and {y} prints it twice. I will omit one of them. – Hany Aug 13 '18 at 05:53
  • Just one more comment please. If I use many \drawing commands in the same tikzpicture, could they all have the same coordinate (P) or should each one have a different name? – Hany Aug 13 '18 at 06:11
  • 1
    The constant assignments are even kept out of the tikzpicture environment, if you then write \draw (P)-(0,0) in another tikz picture environment in another frame; a line will be drawn from the same direction towards the origin, if you make another diferent drawing, it could change if you re asingn another value and change forward. – J Leon V. Aug 13 '18 at 13:54