3

When I draw lines using TikZ, I always need to specify coordinates. For example

\draw (1,0) -- (0,0) -- (0,1);

It is getting tedious when you have a lots of draws.

My question: "Is there a way to simplify the coordinate parameters?"

For example, if the coordinate parameter (on x or y axis) are the same, we can just omit it. More specifically,

\draw (1,0) -- (0,0) -- (0,1);

can be written as

\draw (1,0) -- (0,-) -- (-,1);

(I use - to show the parameter in this position is the same as the one before)

4
  • 2
    I would use -| and |- for that purpose, instead of --. For example, in this case, I would use \draw (1,0) -| (0,1);.
    – user156344
    Commented Apr 11, 2019 at 7:08
  • What about more than 3 coordinates
    – wayne
    Commented Apr 11, 2019 at 7:12
  • 1
    Give me an example, and I will do it for you. A combination of --, -|, and |- will solve it all.
    – user156344
    Commented Apr 11, 2019 at 7:15
  • Yes, it is true you can use combinations of these to draw almost any line. In this case, my question seems trivial.
    – wayne
    Commented Apr 11, 2019 at 7:19

1 Answer 1

6

I agree with you that you should not have a command in which the x coordinate (or y coordinate) is useless in the relative positioning of the points.

However, it is not that easy to have such a command you requested. Nevertheless, TikZ already gives you a more brillant solution: -| and |-, which can handle named coordinates like (a), (b), etc. (and you will know that named coordinates are much prefered when drawing figures).

Some example:

\draw (x1,y1) -- (x1,y2) -- (x2,y2);

can be changed to

\draw (x1,y1) -| (x2,y2);

Or with five coordinates:

\draw (x1,y1) -- (x1,y2) -- (x2,y2) -- (x2,y3) -- (x3,y3) -- (x3,y4);

can be changed to

\draw (x1,y1) |- (x2,y2) -| (x3,y3) -- (x3,y4);

As I said, a combination of --, -| and |- can do anything related to this. At worst there can be a couple of replicated x (or y) coordinates, but that is not a great deal, especially when you mostly have to deal with named coordinates in the future.

Furthermore, you can specify relative coordinates by prepending + or ++ to the coordinates. Your example would be translated to

\draw (x1,y1) -- ++(0, dy1) -- ++(dx1, 0);

A single + leaves the reference point in place, while a ++ moves the reference point to the current location. This makes it very easy to move parts of a sketch around. You only have to edit the first coordinate.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .