3

In this question the OP needs to draw with a single line of code a series of arrows and labeled nodes.

Let's instead assume that a single uniform line from (0,0) to (10,0) has to be drawn; the dash arrow -| must be placed in positions (2,0), (7,0) and another type of arrow -> at the right end of the line. This is to realize a coordinate-x axis with a vertical dash only in the specified abscissa points.

\documentclass[tikz,border=5pt]{standalone}

\usepackage{tikz}
\usetikzlibrary{positioning}

\begin{document}

\begin{tikzpicture}

\node (a) at (0,0) {};
\node (b) at (2,0) {};
\node (c) at (7,0) {};
\node (d) at (10,0) {};

\draw [-|] (a) edge (b) (b) edge (c) (c) edge (d);

\end{tikzpicture}
\end{document}

This is the solution provided by the linked question: the line is stopped, with blank spaces, at every node.

Even before considering the ending arrow, ->, if I try

\draw [-|] (0,0) edge (2,0) edge (7,0) edge (10,0);

a single dash before the beginning of the line appears (zoom to see it) and it is undesired.

How to realize a continuous drawned line with -| at the specified positions, ending with ->, and possibly with a single line of code? (If a solution is available even without defining the nodes, it is ok: they are not strictly needed here).

  • For a straight horizontal/vertical line, you could always draw three separate arrows. More generally, use the decorations.markings library with an \arrow decoration, as in for example tex.stackexchange.com/questions/7458 or tex.stackexchange.com/questions/316 – Torbjørn T. Nov 23 '17 at 18:39
  • It would be great if you could show us what you have tried. Posting a minimal working example that indicates what you are trying to do makes it easier for people to understand what you want. It also makes it easier for people to help you, since they have some code to start from, and hence much more likely that some one will try to help you. Simply ask people to write code for you is generally frowned upon... – Andrew Nov 23 '17 at 19:30
  • @TorbjørnT. Thank you, I had seen those answers, but they use multiple lines of code. Preferrably, and if possible, I would like to use a single line. – BowPark Nov 24 '17 at 8:27
  • @Andrew I didn't post the code because it is essentially the one in the linked question, which doesn't fit for me. But following your advice, I pasted the code and specified other details, hoping to help and to be more clear. – BowPark Nov 24 '17 at 8:30
7

If you use \coordinate instead of \node, you won't get the gaps. And you can set different arrow tips for the different edges, so try

\draw (a) edge[-|] (b)  edge[-|] (c)  edge[->] (d);

I added a markings version as well, for reference.

output of code

\documentclass[tikz,border=5pt]{standalone}    
\usepackage{tikz}
\usetikzlibrary{decorations.markings}

\begin{document}

\begin{tikzpicture}

\coordinate (a) at (0,0);
\coordinate (b) at (2,0);
\coordinate (c) at (7,0);
\coordinate (d) at (10,0);

\draw (a) edge[-|] (b)  edge[-|] (c)  edge[->] (d);

\end{tikzpicture}

\begin{tikzpicture}[
 decoration={markings,
             mark=at position 0.2 with \arrow{|},
             mark=at position 0.7 with \arrow{|}}
]
\coordinate (a) at (0,0);
\coordinate (d) at (10,0);

\draw [->,postaction={decorate}] (0,0) -- (10,0);

\end{tikzpicture}
\end{document}
5

There is a feature of Metapost that lets you do this, but (as far as I know) there's no such thing in TikZ. The construction subpath (a,b) of p gives you a path from "time" a on path p to time b on path p. Time (more or less) equates to the distance along the path in terms of the number of points used to define it. So a single segment of path will have point 0 at one end and point 1 at the other. You can use it like this:

\RequirePackage{luatex85}
\documentclass[border=5mm]{standalone}
\usepackage{luamplib}
\begin{document}
\mplibtextextlabel{enable}
\begin{mplibcode}
beginfig(1);
    path a;
    a = origin -- (10cm, 0);
    draw a withcolor 3/4 white;
    drawarrow subpath (.15, .25) of a; 
    drawarrow subpath (.65, .75) of a;
    drawarrow subpath (.9, 1) of a;
endfig;
\end{mplibcode}
\end{document}

(Compile with lualatex or adapt for plain MP). This produces:

enter image description here

(I've only drawn the grey line to show you the path...)

The nice thing about this is that it works with arbitrary paths. So if you wanted a path with a bit of a wave, you could redefine the path like this:

a = origin {dir 15} .. {dir 15} (10cm, 0);

and then you would get the same arrows, but now following a curved path:

enter image description here

You can make the arrows as long as you need or even contiguous:

\RequirePackage{luatex85}
\documentclass[border=5mm]{standalone}
\usepackage{luamplib}
\begin{document}
\mplibtextextlabel{enable}
\begin{mplibcode}
beginfig(1);
    path a;
    a = origin {dir 15} .. {dir 15} (10cm, 0);
    drawarrow subpath (0, .2) of a; 
    drawarrow subpath (.2, .7) of a;
    drawarrow subpath (.7, 1) of a;
endfig;
\end{mplibcode}
\end{document}

enter image description here

  • It is definitely a wonderful solution and thank you for your work. Anyway, I asked for tikz because the line with arrows is included in another picture, where other tikz shapes and nodes are placed. For all the cases where a single line is needed, your solution is great. – BowPark Nov 24 '17 at 8:36
1

enter image description here

\documentclass[border=6pt]{standalone}
\usepackage{pgf,tikz}
\usetikzlibrary{arrows}
\begin{document}
\begin{tikzpicture}[line cap=round,line join=round,>=triangle 45,x=1cm,y=1cm]\
clip(-3,-10) rectangle (12,12);
\draw [->,line width=2pt] (0,0) -- (2,0);\draw [->,line width=2pt] (2,0) -- (4,0);
\draw [->,line width=2pt] (4,0) -- (6,0);
\end{tikzpicture}
\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.