2

I'm a newcomer to TikZ, but figure it's the easiest way to draw and include several of the following diagrams below in LaTeX.

diagrams

What code will generate the above diagrams? (Obviously not the text or arrows, I'm simply indicating that every row is equally vertically spaced, and the gaps between horizontal components are are half of this horizontal space.

Cheers.

  • Questions about how to draw specific graphics that just post an image of the desired result are really not reasonable questions to ask on the site. Please post a minimal compilable document showing that you've tried to produce the image and then people will be happy to help you with any specific problems you may have. See minimal working example (MWE) for what needs to go into such a document. – Tom Bombadil Nov 10 '15 at 8:08
  • As I said, I'm a complete newcomer, as in I have never used TikZ before. I didn't realize that to use this site people have to meet a benchmark in terms of subject knowledge. Another piece of information should hence be added in the box that comes up when you create a question: NOOBS NOT ALLOWED. – Cookie Monster Nov 10 '15 at 8:49
  • That's not the case at all. But if you for instance have a look at the TikZ manual, you'll see that it has a huge section with five tutorials that should get you started as it starts with easy things and gets more advanced. So my problem with your question is not that you are a beginner, but rather that your question appears to be of the "just do it for me, I wont put any effort in it" kind. The first tutorial alone should teach you how to draw straight and curved paths, as well as rectangles and nodes. – Tom Bombadil Nov 10 '15 at 9:26
  • I and many others here have no problem helping you with a specific issue, like drawing a curve wider or narrower, but I won't do the whole work for you. As an incentive for anyone to answer you should post an MWE, so that people can copy it to easily get started working on it. However, you might also be lucky as there's sometimes someone with some free time who will answer you question even in this state. Good luck with that, and have a nice day. – Tom Bombadil Nov 10 '15 at 9:32
  • Note that there has been quite a lot of discussion around this type of question on Meta (see e.g. How do I draw this.....? and linked questions). While many don't like them, they are very often answered, as there are several people around who like tinkering with TikZ and similar packages, so you may well get more answers. – Torbjørn T. Nov 10 '15 at 10:23
3

Here's a quick demonstration with TikZ, partly based on Thruston's Metapost answer.

Starting with the squares, the simples way of making those is TikZ' rectangle operation, which is used as

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

So simply specify the coordinates of two opposite corners, with the word rectangle between.

For the other shapes, you can draw the lines segment by segment, e.g.

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

which draws a line from (0,0) to (0,1) to (1,1).

To add the dots at the end of the line, you can use an arrow tip from the arrows.meta library. Hence, you need to add \usetikzlibrary{arrows.meta} to the preamble. Arrow tips are specified in the optional argument to a \draw command, generally as \draw [<name of arrow tip>-<name of arrow tip>] ...;. Arrow tips can be added to one or both ends of a path.

So to add a black dot at the ends of the line, use

\draw [{Circle[]}-{Circle[]}] (0,0) -- (0,1);

Circle[] is the name of the arrow tip. The reason for the brace pair ({}) around it is that you need to "hide" the brackets from the parser, so that the closing bracket (]) of the arrow tip isn't confused with the closing bracket for the optional argument(s) to \draw.

The arc can be drawn in several ways. In the code below I've used the syntax (x1,y1) .. controls (x2,y2) and (x3,y3) .. (x4,y4). This draws a curved line between (x1,y1) and (x4,y4). I can't explain how this works better than the first tutorial in TikZ' manual, so I'm quoting that, section 2.4:

For this, TikZ provides a special syntax. One or two “control points” are needed. The math behind them is not quite trivial, but here is the basic idea: Suppose you are at point x and the first control point is y. Then the curve will start “going in the direction of y at x,” that is, the tangent of the curve at x will point toward y. Next, suppose the curve should end at z and the second support point is w. Then the curve will, indeed, end at z and the tangent of the curve at point z will go through w.

The control points I've used are one unit to the right of the start and end point.

There are two other bits of syntax used in the code below that I added because they could be useful. The first is the scope environment, which allows you to add settings to all commands within. Here I've used

\begin{scope}[xshift=3cm]
...
\end{scope}

Unsurprisingly, what this will do is to move the coordinates of the paths within it 3cm to the right. (Could at this point mention that the default unit length is 1cm, so \draw (0,0) -- (0,1); will draw a line that is 1cm long.)

The other bit is the use of relative coordinates. For example, if you write \draw (1,1) -- +(1,0) -- +(1,1); this will draw a line from (1,1) to (1+1,1+0) = (2,1) to (1+1,1+1) = (2,2). With \draw (1,1) -- ++(1,0) -- ++(1,1); (note two plus signs) this will draw a line from (1,1) to (1+1,1+0) = (2,1) to (2+1,1+1) = (3,2). In other words, with one plus sign, the previous explicit coordinate will always be the reference point, but with two plus signs, the reference coordinate will be moved.

That said, going through the tutorials in the TikZ manual (chapters 2-6), as mentioned in the comments by Tom Bombadil, would probably have learned you all of this and a lot more.

enter image description here

\documentclass[border=4mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows.meta}
\begin{document}
\begin{tikzpicture}
\draw (0,0) rectangle (1,1);
\draw (0,1.5) rectangle +(1,1);
\draw [{Circle[]}-{Circle[]}] (2.5,0) -- (1.5,0) -- (1.5,1) -- (2.5,1);

\begin{scope}[xshift=3cm]
\draw [{Circle[]}-{Circle[]}] (0,0) -- ++(1,0) -- ++(0,1) -- ++(-1,0) -- 
     ++(0,1) -- ++(1,0) -- ++(0,1) -- ++(-1,0);

\draw [{Circle[]}-{Circle[]}] (2.5,0) -- (1.5,0) -- (1.5,1) -- 
     (2.5,1) .. controls (3.5,1) and (3.5,3) .. (2.5,3) -- 
     (1.5,3) -- (1.5,2) -- (2.5,2);
\end{scope}
\end{tikzpicture}
\end{document}
  • The arc could also be drawn by this command ... -- (2.5,1) arc (-90:90:1) -- (1.5,3) .... The syntax for the arc-command is start-angle:end-angle:radius – moospit Nov 11 '15 at 8:45
  • @moospit Thanks, I'm well aware of the arc command, I just thought one option was enough. (I actually tend to prefer the more verbose arc[start angle=-90,end angle=90,radius=1], but that's just a personal preference.) On trying, I also see that the arc command seems to interrupt the path somehow, so that the starting arrow tip is placed at the end of the arc. Hence, some rewriting would be required. – Torbjørn T. Nov 11 '15 at 8:51
  • Using \draw [{Circle[]}-{Circle[]}] (2.5,0) -- (1.5,0) -- (1.5,1) -- (2.5,1) arc (-90:90:1) -- (1.5,3) -- (1.5,2) -- (2.5,2); should do the trick. The arc is a bit wider than the one from the controls version. – moospit Nov 11 '15 at 9:05
3

Here's a simple approach in Metapost, that might get you started. Follow the link above for more details, and pointers to the excellent tutorials and manuals available.

enter image description here

prologues := 3;
outputtemplate := "%j%c.eps";

beginfig(1);

u := 1cm;

draw (0,0) -- (u,0) -- (u,u) -- (0,u) -- 
     (0,2u) -- (u,2u) -- (u,3u) -- (0,3u);

fill fullcircle scaled 5 shifted origin;
fill fullcircle scaled 5 shifted (0,3u);

draw (2.5u,0) -- (1.5u,0) -- (1.5u,u) -- 
     (2.5u,u) {right} .. (3.5u,2u) {up} .. {left} (2.5u,3u) -- 
     (1.5u,3u) -- (1.5u,2u) -- (2.5u,2u);

fill fullcircle scaled 5 shifted (2.5u,0);
fill fullcircle scaled 5 shifted (2.5u,2u);

endfig;
end.

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.